locked
How to call plugin/workflow/action on opening the form RRS feed

  • Question

  • Hi,

    I want to call plugin/workflow/action on page load or while opening the form. 

    Please guide me.

    Thanks

    Tuesday, April 22, 2014 12:57 PM

All replies

  • Hi,

    May you write some details?

    Thanks

    Al.


    Alessandro Graps

    Tuesday, April 22, 2014 1:04 PM
  • Hi Alessandro,

    I have written some server side code which should run from page load or while opening the form. This server side code return some value and hide in to that form without saving the form. On form, we have button when we click on that button it get data from third party and display it the data on to the form. 

    Tuesday, April 22, 2014 1:15 PM
  • Hi,

    you can create a new plugin in post entity creation and post entity update.when you click save, you can get the data from third party and display it :

    protected void ExecutePostEntityServiceCreate(LocalPluginContext localContext)
            {

                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }            

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                //ITracingService tracingService = localContext.TracingService;

                if (context.Depth > 1)
                    return;

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        //Get data
                        //..........
                        //..........
                        
                        //update form
                        Entity newService = new Entity();                    
                        newService.value1 = value1;
                        service.Update(newService);

                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }                
                }
            }

    If you want use an alternative button, you need call the third part data with javascript.. for example if you have a web service, you can call it and aftert you can use sdk to display it on form

    Thanks

    Al.


    Alessandro Graps

    Tuesday, April 22, 2014 1:42 PM
  • Hi,

    I have to run that plugin before created the record.

    Tuesday, April 22, 2014 2:42 PM
  • With a plugin you cannot execute code on form_load. you can execute code before an event like CREATE, UPDATE, etc.

    If you want to make it in the form load, you need javascript.. you can use script# to convert c# code in javascript.


    Alessandro Graps


    Tuesday, April 22, 2014 3:40 PM
  • Use global custom action, plugin and JavaScript combination.

    Create a global custom action that returns values

    Register a Pre operation plugin on that global action for none entity

    Create a JavaScript web resource. Call this custom action synchronously from a function registered for form's On Load event.

    Use the values returned in your to store in your form.

    Actions in CRM 2013

    • Proposed as answer by Ahmad Pirani Thursday, April 24, 2014 1:35 AM
    Tuesday, April 22, 2014 3:40 PM
  • Hi Ahmad,

    I have created JavaScript webresource which is call on form's On Load event.

    But unable to Call custom action from JavaScript webresource on Pre Operation.

    Please guide.

    Below is the code for call global custom action:

    function ExecuteActionCreateProject(requestName) {
        // Creating the request XML for calling the Action
        alert(requestName);
        var requestXML = ""
        requestXML += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestXML += "  <s:Body>";
        requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema- instance\">";
        requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";

        requestXML += "        <a:RequestId i:nil=\"true\" />";
        requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";
        requestXML += "      </request>";
        requestXML += "    </Execute>";
        requestXML += "  </s:Body>";
        requestXML += "</s:Envelope>";
        var req = new XMLHttpRequest();
        req.open("POST", GetClientUrl(), false)
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.send(requestXML);
        //Get the Resonse from the CRM Execute method
        debugger;
        alert(req.responseXML);
        var response = req.responseXML.xml;
        alert(response);
    }

    Thanks 

    Tuesday, April 22, 2014 7:06 PM
  • Did you create a global custom action? If yes, create a new plugin, go to the plugin registration tool (you might have to close the tool and run it again for the custom actions to reflect). Go to your plugin in the tool, register a step by selecting your global action name in the message.
    Tuesday, April 22, 2014 11:03 PM
  • Hi,

    Firstly, I have created global custom action with name i.e. new_actionperform.

    After that create a new plugin and registered in plugin registration tool with global action name in the message. But plugin doesn't trigger when I am request to custom action from JavaScript Webresource.

    One more thing, some time I am getting this response:

    "<s:Body xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services"><ExecuteResult xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ResponseName>new_actionperform</a:ResponseName><a:Results xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"><a:KeyValuePairOfstringanyType><b:key>Output</b:key><b:value i:nil="true"/></a:KeyValuePairOfstringanyType></a:Results></ExecuteResult></ExecuteResponse></s:Body>"

     

    and some time

    <s:Body xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Fault><faultcode>s:Client</faultcode><faultstring xml:lang="en-US" xmlns:xml="http://www.w3.org/XML/1998/namespace">Output</faultstring><detail><OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ErrorCode>-2147220891</ErrorCode><ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic"><KeyValuePairOfstringanyType><a:key>OperationStatus</a:key><a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:int">0</a:value></KeyValuePairOfstringanyType><KeyValuePairOfstringanyType><a:key>SubErrorCode</a:key><a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:int">-2146233088</a:value></KeyValuePairOfstringanyType></ErrorDetails><Message>output</Message><Timestamp>2014-04-23T05:57:51.7743577Z</Timestamp><InnerFault i:nil="true"/><TraceText>

    [Demoplugin: Demoplugin.demoaction]
    [ab351833-62c5-e311-8bb1-d89d67764d74: Demoplugin.demoaction: new_actionperform of  any Entity]

    </TraceText></OrganizationServiceFault></detail></s:Fault></s:Body>"


    Wednesday, April 23, 2014 4:21 AM
  • Hi,

    I have created one output parameter in custom action.

    How to assign value to that output parameter from plugin. 

    Wednesday, April 23, 2014 9:05 AM
  • In the plugin, pluginContext.OutputParameters["yourparametername"] = yourValue
    Wednesday, April 23, 2014 5:33 PM