Search code examples
dynamics-crmaction

How to create Action and Call action in MS Dynamics CRM?


How to create Action step by step and Call action in MS Dynamics CRM? How many ways to call action in MS Dynamics CRM? What the benefits of action instead of Workflow/plugin?


Solution

  • Actions

    Actions are a type of process in Microsoft Dynamics 365. You can invoke actions, including custom actions, directly from a workflow or dialog, without writing code! More information: Invoke custom actions from a workflow or dialog

    Actions can also be invoked by running custom code that uses the Microsoft Dynamics 365 Web services.

    You can call actions:

    It can be called from both client & server side, enabling the Single Point of Approach (Implement once, consume anywhere), for ex:- From code that executes within a plug-in, custom workflow and any C# code. From a command that is placed in the application and executes the operation using JavaScript code. Can receive input parameters and return output parameters in a straight forward manner, similar to an Organization level Web Service From an integration with another system that uses the Microsoft Dynamics 365 web services. From a custom client application that uses the Microsoft Dynamics 365 web services.

    Why use actions?

    Actions open a range of possibilities for composing business logic. Before Actions, the primary way to implement business processes was limited to plug-ins or custom workflow activities. With Actions, you can perform operations, such as Create, Update, Delete, Assign, or Perform Action. Internally, an action creates a custom Dynamics 365 message. With Actions you can create a custom message (for example: submitquote , leadtoax etc. Once an action is defined and activated, a developer can use that message like any of the other messages provided by the Microsoft Dynamics 365 platform.

    Suppose you have button on Quote form which sends information from CRM to another platform (for ex another platform is AX).

    Create and activate a Custom Action (Settings > Process)

    enter image description here

    Now you can call this Action(ofs_submitquotetoax) from JavaScript on some event (OnLoad, OnSave,etc). In this example I am calling the action from SUBMIT QUOTE button on Quote form which sending quote information to other system (AX).

    enter image description here

     // Call this below method from Button click event or from any event on the form
    
    // For Alert.showLoding method you can see another Alert.js library
    
    // For Process.callAction method you can see another Process.js library
    
    // You can download Alert.js & Process.js from this Path: https://drive.google.com/drive/folders/0B2CUbevE8v9YMkZlMEhUZ3NJc1U 
    
    
    
    function submitquote() {
    
        var actionName = "ofs_submitquoteax";
    
        var entityName = Xrm.Page.data.entity.getEntityName();
    
        var entityId = Xrm.Page.data.entity.getId();
    
    
    
        Alert.showLoading("Submitting...", 400, 150);
    
        var inputParams = [
    
                          {
    
                              key: "EntityRef", type: Process.Type.EntityReference,
    
                              value: new Process.EntityReference(entityName, entityId)
    
                          }
    
        ];
    
        // call process callection method
    
        Process.callAction(actionName, inputParams, cloneSuccessCallback, errorCallback);
    
    }
    
    
    
    function cloneSuccessCallback() {
    
    
    
        Alert.hide();
    
        Alert.show("Action Success", "", null, "SUCCESS");
    
        Alert.hide();
    
        var entityName = Xrm.Page.data.entity.getEntityName();
    
        var entityId = Xrm.Page.data.entity.getId();
    
        Xrm.Utility.openEntityForm(entityName, entityId);
    
        //Xrm.Page.data.refresh();
    
    }
    
    
    
    function errorCallback(error, trace) {
    
        alert(error);
    
        alert(alert(error));
    
    }
    

    e, for this event you can register & trigger a Plugin and receive the input-parameter (in our case we sending input-parameter key as EntityRef in which we are sending entityName and entityId.

    Parameters: Action Unique Name, Input Parameters (array), Success Callback (function), Error Callback (function), CRM Base URL (not required on forms/views)

    Each Input Parameter object should contain key, value, and type. Types are defined by the Process.Type enum. EntityReference values should be an object containing id and entityType.

    The Success Callback function should accept one argument which is an array of output parameters, each containing key, and value.

    You can write plugin in below way and access input parameter

    protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
    
        {   // Register the plugin in PreValidation stage as this plugin will trigger from Javascript (Action)
    
            if (localContext == null)
    
            {
    
                throw new InvalidPluginExecutionException("localContext");
    
            }
    
    
    
            IPluginExecutionContext context = localContext.PluginExecutionContext;
    
            if (context.Depth > 1) { return; }
    
    
    
            IOrganizationService service = localContext.OrganizationService;
    
            ITracingService trace = localContext.TracingService;
    
    
    
            Entity quoteEntity = null;
    
            EntityReference qEntity = null;
    
    
    
            if (context.InputParameters.Contains("EntityRef") && context.InputParameters["EntityRef"] is EntityReference)
    
            {
    
                //if (context.PrimaryEntityName.ToLower() != "quote") { return; }
    
                 qEntity = context.InputParameters["EntityRef"] as EntityReference;
    
    
    
                if (qEntity.LogicalName.ToLower().Equals("quote"))
    
                {                 
    
                    try
    
                    {
    
                        quoteEntity = service.Retrieve("quote", qEntity.Id, new ColumnSet("ofs_parentaccountid", "quotenumber", "revisionnumber", "ofs_well"));
    
                  // Execute Your logic
    
                    }
    
                    catch (Exception ex)
    
                    {
    
                        trace.Trace(string.Format("Exception Quote_Create_AXIntegration Plugin: {0}", new[] { ex.ToString() }));
    
                    }
    
                }
    
            }
    
            else { return; }
    
        }
    

    Register your plugin and then register the step in below way, you can notice your custom message name in below screen-shot "ofs_submitquoteax";

    enter image description here

    Ref:https://community.dynamics.com/crm/b/mylifemicrosoftdynamiccrm/archive/2017/04/17/microsoft-dynamics-crm-actions