# Schedule the sync process

{% hint style="info" %}
User permission: Administration > Sync
{% endhint %}

You can automate the Sync process via a Windows scheduled task. Phocas uses the *Run a Program* option and defines the *PhocasCLI.exe* as the program and the Sync File with the run switch -r.

## Prerequisites <a href="#schedulethesyncprocess-schedulesync" id="schedulethesyncprocess-schedulesync"></a>

Before you schedule the sync process, think about the account that will be used for the Windows Task Scheduler and the sync timing.

### User account

Windows Task Scheduler requires a user account to run the Sync process. If you use a *real* user account for this, you run the risk of the schedule failing to launch (see the Troubleshooting section below). Therefore, we recommend you set up a dedicated (service) account to run the Windows Task Scheduler job. For example, an account called *PhocasScheduler*. This type of account satisfies the following requirements:

* It will never be disabled.
* Its password will never expire or be changed.
* It has the Local Security Policy “*Logon as a Batch Job*” permission.
* It has appropriate access to SQL, ODBC, file locations, and so on.

See the Microsoft [Service Accounts](https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flearn.microsoft.com%2Fen-us%2Fwindows-server%2Fidentity%2Fad-ds%2Fmanage%2Funderstand-service-accounts\&data=05%7C02%7Cdenise.mcgettigan%40phocassoftware.com%7C634511e5f1464547b37608dd419e53cb%7Cea0a54e7b7c74b9d9192c67bfb25e151%7C0%7C0%7C638738870453157019%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C\&sdata=jcc0EyuiYOPzf1deqUfSUswRnizwDSO3LucXaX3QwCE%3D\&reserved=0) page to learn more about such accounts. It’s up to your IT team to choose the appropriate account for your organization's security policy.

### Timing

Phocas generally syncs over night so that data is accurate as at close of business the day before. Consider the following points:

* When the data will be analyzed? Will it be analyzed or changed over the weekend?&#x20;
* If you use GETDATE() calculations, consider which side of midnight you are syncing.&#x20;
* If syncing multiple times per day, is there a time window required?&#x20;
* Are there any other processes running that might affect the sync at that time?

## Schedule sync <a href="#schedulethesyncprocess-schedulesync" id="schedulethesyncprocess-schedulesync"></a>

The steps below are a basic guide, but you might also want to look online for Microsoft's instructions on using Windows Task Scheduler. When you schedule Sync, other options become available, as outlined on this page.

1. Set up an account to use for the Windows Task Scheduler (see Prerequisites section above).
2. Create and save your Sync file.
3. Open Windows Task Scheduler, create a basic task, give it a name, and click **Next**.

   <div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/sPjr8iUEN1Nt8nzZt0Dx/3271884957.png" alt="" width="250"><figcaption></figcaption></figure></div>
4. For the trigger, select how frequently you want to run the task. In this example, it will run every day at 10.01am.

   <div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/wv0cPY3Ll0PipB6jES0m/3271884963.png" alt="" width="250"><figcaption></figcaption></figure></div>
5. For the action:
   1. Click **Start a program**, then click **Browse** and locate the PhocasSyncCLI.exe file.

      <div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/JDkYjAXweLJJAL6sXOLh/3271884969.png" alt="" width="250"><figcaption></figcaption></figure></div>
   2. In the **Add argument** box, enter the location of your sync file in quotation marks with **-r** at the beginning, for example:<br>

      ```
      -r "c:\phocas\sync\sales\yourfilename.sync"
      ```

{% hint style="info" %}
To prevent the task scheduler from failing:

* Avoid using spaces in your file and folder names. In the example above, the **-r** attempts to mitigate this risk.
* Ensure you use straight quotes (" ") instead of curly quotes (“ ”).
  {% endhint %}

<div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/K8VASjwqfZC7C9XduTRp/3271884975.png" alt="" width="250"><figcaption></figcaption></figure></div>

6. Click **Next** and **Finish**. Your sync will now run on a schedule.
7. Ensure that the task runs even when you are signed out of Phocas: Locate the task in the list, right-click on the task and click **Properties**, then on the **General** tab, select **Run whether user is logged on or not** and click **OK**. &#x20;

   <div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/MmNd8DUw7URcXPIfkBQx/3271328250.png" alt="" width="403"><figcaption></figcaption></figure></div>
8. (Optional) [Test the new sync process](https://docs.phocassoftware.com/administration/sync/test-the-sync-process).

## Set an alert on Sync failure <a href="#schedulethesyncprocess-setanalertonsyncfailure" id="schedulethesyncprocess-setanalertonsyncfailure"></a>

To receive an email alert if a Sync fails, go to your [account settings](https://app.gitbook.com/s/7pj8v25BOyqZTdG5mdD1/getting-started/account-settings) and select **Sync failures** under **Email notifications**.&#x20;

<div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/wb4xaWnrnuR4KjqsNtRb/3271852104.png" alt="" width="550"><figcaption></figcaption></figure></div>

See the Troubleshooting section below for information about failures.

## Set Designer builds to be based on scheduled Sync data <a href="#schedulethesyncprocess-setdesignerbuildstobebasedonscheduledsyncdata" id="schedulethesyncprocess-setdesignerbuildstobebasedonscheduledsyncdata"></a>

If data has been scheduled to sync regularly (for example each night), typically users expect the database at the other end to re-build almost immediately with this new data. For this to occur, a database must be set to build when a sync occurs.

There are two schedule frequency options applicable to Sync: **After sync of any source in this design** and **After sync of all selected sources**.

<div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/rwSZYGQ6co8z5OKVzcDA/3272902529.png" alt="" width="279"><figcaption></figcaption></figure></div>

See [Build a database](https://docs.phocassoftware.com/administration/designer/build-a-database) for more information.

## Only sync data items used in a database <a href="#schedulethesyncprocess-onlysyncdataitemsusedinadatabase" id="schedulethesyncprocess-onlysyncdataitemsusedinadatabase"></a>

After the initial sync of data, you can choose to only sync data items that are used in a Phocas database design in Designer (and exclude unused items). For example, if a Sync file has ten data items listed in the **Data Items** tab but only three have been used in a database, the unused seven data items will not be synced.

In Sync, select the **Configuration** tab and select the **Only Sync Data Items…** checkbox.

<div align="left"><figure><img src="https://content.gitbook.com/content/vQCmtGTR7MsjcI62Vdki/blobs/EgFI8T6PUIigQ9jHb2dO/3271328268.png" alt="" width="483"><figcaption></figcaption></figure></div>

***

## Troubleshooting

The most common reasons for sync failing to run are related to the account that's used for the Windows Task Scheduler:

* The password is changed or expired.
* The user leaves the organization and their account is disabled or deleted.
* The security policy is changed to exclude the Local Security Policy “*Logon as a Batch Job*” permission.

If you discover that there are suddenly no Sync Logs in Phocas, check the Windows Task Scheduler. You can see the name of the account (Windows Credentials) being used on the **General** tab. The **History** tab will contain Launch Failure messages, assuming the history function is enabled. The error value for the message depends on the issue. For example, value 2147943731 means the account is disabled.

To relaunch the schedule, you need to resolve the issue with the account. For example, enter in the new password. To avoid such issues, use a dedicated account for the Windows Task Scheduler (see Prerequisites section above).
