locked
CRM 365: REST API request needs to trigger plugin logic RRS feed

  • Question

  • Hi,

    I have a entity where I have a button in the ribbon. When the User click this button, the status should change from "Active" to "inactive".
    To solved this I am doing a REST call, in a JavaScript method "SaveAsCompleted":

    This works. The entity's status changes. But: I have a lot of validation logic in plugin steps on this entity.
    How do I trigger these plugin steps?

    regards,

    Marius Holm Enerud

    Thursday, February 2, 2017 9:51 PM

All replies

  • What Message are you capturing to write you logic. When you update the State or Status of the entity you should capture "Set State" and "SetStateDynamicEntity" messages.

    MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview | CRM2011 Lookup Attribute Mapping | CRM2011 TreeView Control (Beta) | CRM2011 N:N Entity View (Beta) | CRM 2011 Global Quick Search (Beta)

    Friday, February 3, 2017 8:27 PM
  • Hi Makarand!

    I have checked that the following plugins are enabled:

    In the "PreValidateSetStateDynamic"-step I'm also triggering a exception at the start of the code in order to see that it is triggering:


    But I can't see this exception after I'm doing the web api call...

    The web api are returning a 204-status and the workorder is changing status (after I refresh the page). But I was excpecting to trigger the "PreValidateSetStateDynamic"-step.
    How do I trigger "PreValidateSetStateDynamic"-step?

     


    • Edited by morris80 Saturday, February 4, 2017 10:41 AM
    Saturday, February 4, 2017 10:40 AM
  • Saturday, February 4, 2017 7:45 PM
  • I registered setState-step (post operation) but nothing seems to trigger..

    Do we know that SetState-plugin should trigger when update state via WEB API?

    Maybe some of you could try my JavaScript-code and see if it triggers plugins?


    • Edited by morris80 Saturday, February 4, 2017 9:55 PM
    Saturday, February 4, 2017 9:51 PM
  • Saturday, February 4, 2017 11:38 PM
  • I made the workorder plugin assembly run in Sandbox-mode, to be able to do "plugin trace log".
    When I ran the WEB API call again I saw that the PreValidateUpdate-step was triggered:

    So that means that the SetState-steps are not being triggered, only the Update-steps.
    Is this the correct behavour?

    regards,

    Marius H. Enerud

    Sunday, February 5, 2017 9:07 PM
  • I just tried with sample Plug-in message on SetState and SetStateDynamicEntity on account entity and it did work for me. I received following exception pop-up which my plug-in code threw. There might be some minor this missing in your case.

    


    MaKeer|<a href="http://wp.me/1AkRL" target="_blank">myencounterwithcrm.wordpress.com </a>

    Monday, February 6, 2017 3:03 AM
  • Ok. But did your code also trigger Update-steps ?
    Monday, February 6, 2017 7:21 AM
  • No, I just deactivated account from CRM Form. I will try with WebAPI code and let you know 

    I think since you are doing entity Update with StateCode, its treating it normal update and not a SetState message. 

    There seems to be one more issue with WebAPI State change behavior, refer this https://community.dynamics.com/crm/f/117/t/215495


    MaKeer|<a href="http://wp.me/1AkRL" target="_blank">myencounterwithcrm.wordpress.com </a>

    Monday, February 6, 2017 4:45 PM
  • Hi MaKeer,

    Did you try the webAPI? :-)

    regards,
    Marius

    Thursday, February 9, 2017 12:09 PM
  • I haven't got chance to try it, however what I understood by reading elsewhere is now since WebAPI is allowing State and Status changes using "Update" call, in Plug-n also it execute Update Message and not SetState or SetStateDynamic Plugin. Thats what also happening in your case. Hence I think you should register Update message to catch Plug In. On the safer side also register SetState and SetStateDynamic.

    MaKeer

    • Proposed as answer by Makarand Keer Tuesday, February 14, 2017 10:18 PM
    Thursday, February 9, 2017 4:43 PM
  • Another way to achieve this is:

    1. Create an action.

    2. Register your plugin on that action.

    3. In your javascript on ribbon button after changing the status call that action. It will trigger your plugin.

    You can call an action from javascript like this

    WW.TriggerGenerateDocumentsAction = function () {
        var currentRecordIdString = Xrm.Page.data.entity.getId();
        var currentRecordId = currentRecordIdString.replace("{", '').replace("}", '');
        var query = "log_workorderses(" + currentRecordId + ")/Microsoft.Dynamics.CRM.youractionname";
        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query, false);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState == 4) {
                req.onreadystatechange = null;
                if (this.status == 204) {
                }
                else {
                    var error = JSON.parse(this.response).error;
                    Xrm.Utility.alertDialog(error.message);
                }
            }
        };
        req.send();
    }
    You replace youractionname with the unique name of the action you created.


    Sachith Chandrasiri

    Friday, February 10, 2017 2:49 AM