MobileCRM.UI.EntityForm.tryEnterUiTransaction - Resconet/JSBridge GitHub Wiki
[v18.0] Provides means to control concurrent access to data by form logic and background sync. This method obtains a clearance to perform data manipulation. The clearance will be obtained when background sync is not running, or form sync behavior is configured as 'No action'. If background sync is running and form sync behavior is configured to 'Block save during sync' or 'Block save during sync and prevent sync while form is open'", the clearance is not obtained. Each obtained clearance must be dutifully released using leaveUiTransaction(). It is possible to call this method multiple times, but each call reserves its own internal clearance, so there must be the same number of calls to leaveUiTransaction to properly release clearance and allow background sync.
This example demonstrates how to use tryEnterUiTransaction method to ensure that new child account is not created while sync is running.It is expected that there is custom command 'custom_createChildAccount' configured in Woodford and that option 'Background Sync Behavior' of the form is configured at least to 'Block save during sync'.
MobileCRM.UI.EntityForm.onCommand(
"custom_createChildAccount",
async function (entityForm) {
const account = entityForm.entity;
const childAccount = new MobileCRM.DynamicEntity.createNew("account");
childAccount.properties.name = account.properties.name + " Child";
childAccount.properties.parentaccountid = new MobileCRM.Reference(account.entityName, account.id, account.primaryName);
if (await MobileCRM.UI.EntityForm.tryEnterUiTransaction()) {
try {
await childAccount.saveAsync();
}
finally {
await MobileCRM.UI.EntityForm.leaveUiTransaction();
}
}
else {
await MobileCRM.UI.MessageBox.sayText("Cannot create parent account because sync is currently running.");
}
},
true
);