How to execute an asynchronous function in Teamsware actions or any other expression configuration?

How to execute an asynchronous function in Teamsware actions or any other expression configuration?

Description

If you need to wait for callbacks in Teamsware actions or other expression configurations before processing further, it's recommended using jQuery.Deferred() object.

The following code snippet demonstrates how to achieve this:

var deferrer = jQuery.Deferred(); //new deferred object is created.
window.setTimeout(function () {
deferrer.resolve(); //Resolve the deferred. Then e.g. next predefined Rich Form's actions will start to execute.
}, 10000);
return deferrer.promise(); //returns promise object

 

Example

After a new item is added you want to update its permissions and allows only Approver to edit this item.

In order to achieve this, open a new form in solution studio and add new action Execute Script after Save Form action for Save button:

New Form

 

Javascript code for Execute Script (Update Permissions) action:

var deferrer = jQuery.Deferred();

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle("DemoList");
var item = list.getItemById([[ID]]);
var approver = web.ensureUser([[Approver.LoginName]]);

//Break permission inheritance and clear existing permission rules.
item.breakRoleInheritance(false,true);

var itemAssignments = item.get_roleAssignments();

//Set read-only permisisons for site default groups
var roleDefBindingsReader = SP.RoleDefinitionBindingCollection.newObject(ctx);
roleDefBindingsReader.add(web.get_roleDefinitions().getByType(SP.RoleType.reader));
itemAssignments.add(web.get_associatedVisitorGroup(), roleDefBindingsReader);
itemAssignments.add(web.get_associatedMemberGroup(), roleDefBindingsReader);
itemAssignments.add(web.get_associatedOwnerGroup(), roleDefBindingsReader);

//Allow Approver to edit the item
var roleDefBindingsApprover = SP.RoleDefinitionBindingCollection.newObject(ctx);
roleDefBindingsApprover.add(ctx.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));
itemAssignments.add(approver, roleDefBindingsApprover);

ctx.executeQueryAsync(function() {
deferrer.resolve();
}, function(sender, args){
deferrer.reject("Failed to update item permissions: " + args.get_message())
});

return deferrer.promise();

 

Execute Script (Update Permissions) action


    • Related Articles

    • Trigger SharePoint item update to triggered actions reliably

      Problem When you modify a SharePoint item using “Update Item” in Triggered Actions or Scheduled Actions, downstream Triggered Actions (that listen for changes on the same item) do not fire reliably. Reason: The standard update does not set all ...
    • Starting a Microsoft Flow from Teamsware Actions

      Microsoft Flow provides many features and integration options not available when using basic Teamsware actions. But you can start a Microsoft Flow / Power Automate from Teamsware action quite easily by using the Start Power Automate / Azure Function ...
    • Action: Execute script

      Execute script action allows you dynamically run JavaScript scripts. For more information about Action Builder read Action Builder introduction article. To configure the action additional properties should be specified: Configuration Property ...
    • Triggered Actions introduction

      What are Triggered Actions? Triggered Actions allow configuring actions that are applied to a list or library when an event occurs (e.g., add, update, or add/update) and a condition evaluates to true. Triggered Actions are a SharePoint Online ...
    • Teamsware Studio Update 29.01.2026

      Bug fixes & improvements Modern Forms Version 1.3.108 Improved Get items action with an option 'Use dynamic url to the target SharePoint list', allowing dynamic setting of the list URL for item operations Improved performance evaluation of ...