Task - 4

 Call the Plugin using JavaScript and set the Approved Status

Call the plugin using JavaScript and set the approved status.

User Story: -

When we click on the Approve button in the student Entity record then the status field value in the student record should become Approved.

Sudo code: -

  • Create an Approve button for custom entity using Ribbon work bench in XRM Toolbox. 
    • Create a solution for custom entity ribbon work bench. 
    • Add the required entity meta data to the solution. 
    • Use this solution in Ribbon workbench to create buttons and publish it in the entity.
  • Create an Action to use it as a Message in Plugin.
  • Create a plugin.
  • In the plugin retrieve the EntityReference to a variable.
  • Define Entity variable by getting values of Entity Id and Logical name from EntityReference.
  • null check the entity.
  • Set the OptionSet Attribute value to the Approved value.
  • Use the service.Update(Entity) to update in the server.
  • Register the Plugin using the Plugin registration tool and set the Message name as the Action name.
  • Now we have to create a JavaScript file for calling the Action, this will automatically call the plugin.
  • We can use a Web API execute method to call the Action using Javascript, you can get this by using the CRM REST Builder.
  • Create a web resource using this JavaScript code.
  • Use the Ribbon work bench in XRM Toolbox to add this JavaScript code to the Approve button as Action code.

JavaScript code: -

function CallActionToTriggerPlugin(primaryControl) {
    var formContext = primaryControl;
    var id = formContext.data.entity.getId().replace("{", "").replace("}", "");

    var parameters = {};
    var entity = {};
    entity.id = id;
    entity.entityType = "naree_student";
    parameters.entity = entity;

    var new_EffiCallActiontotriggerPluginRequest = {
        entity: parameters.entity,

        getMetadata: function () {
            return {
                boundParameter: "entity",
                parameterTypes: {
                    "entity": {
                        "typeName": "mscrm.naree_student",
                        "structuralProperty": 5
                    }
                },
                operationType: 0,
                operationName: "new_EffiCallActiontotriggerPlugin"
            };
        }
    };

    Xrm.WebApi.online.execute(new_EffiCallActiontotriggerPluginRequest).then(
        function success(result) {
            if (result.ok) {
                //Success - No Return Data - Do Something
                console.log("Success");
            }
        },
        function (error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
}



Plug-in code: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;

namespace Plugin1
{
    public class CallPluginFromActionAndJavaScript: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
            {
                EntityReference StudentRef = (EntityReference)context.InputParameters["Target"];
                Entity Student = service.Retrieve(entityName: StudentRef.LogicalName,
                id: StudentRef.Id,
                columnSet: new ColumnSet("naree_status___"));


                if (Student != null)
                {
                   
                    Student.Attributes["naree_status___"] = new OptionSetValue(737110000);
                    service.Update(Student);
                   
                }
            }

        }
    }
}




Comments

Popular posts from this blog

Secure and Unsecured Configurations in Plugin in Dynamics 365

Calculated field and Rollup field in Dynamics 365

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application