RS1
RS1

Reputation: 31

How can know when the plugin ends in CRM 2016?

I have a plugin Code C# in CRM 2016. I want to activate a Javascript Code when plugin ends. I try to update a field by this plugin and when this field changes, to trigger the event of the change with the javaScript code. I see ,that the field changes, but it does not activate the function that occurs when changing the field. But when change this field manually and not through the plug-in, the function works good.

Maybe there's an idea for how know when the plug-in ends?

Upvotes: 2

Views: 294

Answers (3)

We cannot execute some Javascript function code block in client side after completing the execution of Server side Plugin.

But by extending your approach, you are updating a dummy field in Plugin, so refresh the form to see latest changes, on form load - firing the onchange() method of that dummy field should work nicely.

Upvotes: 0

Michael Blackburn
Michael Blackburn

Reputation: 3229

If the plugin is being called from a save on the form, and is registered synchronously, you can run script on return from the save. You'll need to use the asynchronous save method: Xrm.Page.data.save().then(_success, fail); This is different from the older Xrm.Page.data.entity.save(saveType) operation, which was synchronous, with no ability to trap the return.

If you need this plugin to fire on every save, then trap the OnSave Event, cancel the default action and use this asynchronous save method. Here is a page which discusses cancelling the default save action: https://neilparkhurst.com/2015/11/11/javascript-prevent-save/ You can use that to create this "resumable" save operation instead. In your then() success method, you can get the attribute that the plugin changed and call your fireOnChange() method.

Here is the XRM documentation for this newer operation: https://msdn.microsoft.com/en-us/library/dn481607.aspx?f=255&MSPPError=-2147217396

Upvotes: 1

Michael Blackburn
Michael Blackburn

Reputation: 3229

This is a separate answer, because it's a solution for a different situation. You'll do either this or my other answer, but probably not both, and I didn't want to confuse by putting them into one answer.

If this "plugin" is incidental, as in it only happens occasionally, not on every save, it's probably a good time to use Actions. Before actions, we would often have "control fields" -- fields on an entity that didn't have any data value, we just set them when saving to make the plugin behave in a special way. If this is the situation, I'd suggest refactoring the code into an custom activity (same as if you made custom code for a workflow) and calling the action directly, rather than in a plugin on a save. In that case, you don't need to "run javascript after a save" because you're just calling an action and running after that.

Upvotes: 1

Related Questions