cancel
Showing results for 
Search instead for 
Did you mean: 

How to update entities over loop items of a read result in MDK?

former_member291921
Discoverer
0 Kudos

I’m trying to do an update entity action based on a loop over a read result. However, the update action can’t get the bindings I set in the rule. The log shows that @data.readlink is null so that the update action can't be executed correctly. How should I send @odata.readLink of each loop item to the update action?

export default function myFunction(clientAPI) {

var readPromise = clientAPI.read('MyOdataS.service', 'MyEntity',[],`$filter=is_deleted ne true`);

return readPromise.then((result) => {

for (var i = 0; i < result.length; i++) {

var item = result.getItem(i);

let picBinding = {

'@odata.readLink': item['@odata.readLink']

};

let pageProxy = clientAPI.getPageProxy();

pageProxy.setActionBinding(picBinding);

clientAPI.executeAction('MyUpdateEntity_D.action');

}

return;

});

}

//MyUpdateEntity_D.action

{

"_Type": "Action.Type.ODataService.UpdateEntity",

"Target": {

"Service": "MyOdataS.service",

"EntitySet": "MyEntity2",

"QueryOptions": "",

"ReadLink": "{@odata.readLink}"

},

"Properties": {

"is_deleted": true

}

}

Accepted Solutions (1)

Accepted Solutions (1)

mingkho
Advisor
Advisor
0 Kudos

Hi Shawn

There are a couple of issues:

1st Issue:

That’s because you didn’t wait for the action execution in your loop. Action execution is an async operation. See this doc here for details (check the Notes).

So when you execute your actions in a loop as in your code, it essentially means you are trying to execute them all in parallel asynchronously + immediately return (as void) from the rule, so MDK will just complete the rule and destroy the action binding before your actions are fully executed, and when you actions are finally getting executed they can no longer find the binding.

Also note that one copy of the action binding when you call setActionBinding, so when you execute all actions parallel in a loop, they will all referring to the same copy action binding, that’s why you need to make sure to only execute them one at a time.

You can refer to this guide on more details about action binding.

Best approach is to use ‘await’ keyword to wait for each execution in the loop.

You need to change this 2 lines:

From:

    return readPromise.then((result) => {<br>

to

    return readPromise.then(async (result) => {<br>

and

clientAPI.executeAction('/FriMartApp/Actions/Seller/Pictures_UpdateEntity_Delete.action');<br>

to:

await clientAPI.executeAction('/FriMartApp/Actions/Seller/Pictures_UpdateEntity_Delete.action');<br>

This way you will wait for each action to complete before execute another one.

2nd Issue:

Your action definition is broken:

"ReadLink": {@odata.id}<br>

Should be:

"ReadLink": "{@odata.id}"<br>

(values must be inside double quotes)

3rd issue:

Not really an issue but

pageProxy.setActionBinding(picBinding);

pageProxy.setActionBinding(idBinding);<br>

the first setActionBinding call is redundant because you can only have 1 action binding object, so the 2nd setActionBinding will replace the first one.

Hope this helps. Do let me know if you still encounter any issue.

Regards

Ming

Answers (0)