4. Call the plugin using JavaScript and set the approved status

 

User Story: Call the plugin using JavaScript and set the approved status

Requirement: JavaScript, Action and Plugin

1.       Create a custom button with the name of Plugin approve by using XRM Tool box

2.       Create one Status field with option set datatype

3.       Create an action for student entity without arguments and steps

4.       Generate a code from Rest Builder using created action

5.       Write JavaScript function to trigger action and place it in web resource

6.       Call JavaScript function from Plugin approve button using XRM Toolbox

7.       Write a plugin code to update status field as approved

·         By using Entity Reference retrieve the entity

·         Set the value of Status reason as Approval.

·         By using service.Update  update the status Reason Value.

8.       In plugin registration tool

·         Create a new assembly

·         For that assembly create a new step

·         Message: Action Name

·         Primary Entity: crb76_Student

·         Execute Pipeline: Post Operation

·         Execution Mode: Synchronous

Create a custom button with the name of Plugin approve by using XRM Tool box and Create one Status field with option set datatype 



 Create an action without arguments and steps



 

Generate a code using Rest Builder

var parameters = {};

var entity = {};

entity.id = "";

entity.entityType = "crb67_student";

parameters.entity = entity;

 

var new_EffActionforpluginRequest = {

    entity: parameters.entity,

 

    getMetadata: function() {

        return {

            boundParameter: "entity",

            parameterTypes: {

                "entity": {

                    "typeName": "mscrm.crb67_student",

                    "structuralProperty": 5

                }

            },

            operationType: 0,

            operationName: "new_EffActionforplugin"

        };

    }

};

 

Xrm.WebApi.online.execute(new_EffActionforpluginRequest).then(

    function success(result) {

        if (result.ok) {

            //Success - No Return Data - Do Something

        }

    },

    function(error) {

        Xrm.Utility.alertDialog(error.message);

    }

);

Write JavaScript function to trigger action and place it in web resource 

function Action_plugin(primaryControl)

{

    var formContext = primaryControl;     

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

    var parameters = {};

    var entity = {};

 

    entity.id = id;

    entity.entityType = "crb67_student";

    parameters.entity = entity;

 

    var new_EffActionforpluginRequest = {

        entity: parameters.entity,

        getMetadata: function () {

            return {

                boundParameter: "entity",

                parameterTypes: {

                    "entity": {

                        "typeName": "mscrm.crb67_student",

                        "structuralProperty": 5

                    }

                },

                operationType: 0,

                operationName: "new_EffActionforplugin"

            };

        }

    };

    Xrm.WebApi.online.execute(new_EffActionforpluginRequest).then(

        function success(result) {

            if (result.ok) {

                Xrm.Utility.alertDialog("Success");

            }

        },

        function (error) {

            Xrm.Utility.alertDialog(error.message);

        }

    );

} 

Call JavaScript function from Plugin approve button using XRM Toolbox



Write a plugin code to update status field as approved

using Microsoft.Xrm.Sdk;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

 

namespace Student

{

    public class Plugin_class : 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)

            {

                try

                {

                    EntityReference entityReference=(EntityReference)context.InputParameters["Target"];

                    Entity entity = new Entity(entityReference.LogicalName);

                    entity.Id = entityReference.Id;

                    entity["crb67_statuss"] = new OptionSetValue(518320000);

                    service.Update(entity);

                }

                catch (Exception ex)

                {

                    throw new InvalidPluginExecutionException(ex.Message);

                }              

            }

        }

    }

}

 

Go to Plugin Registration Tool:

Click on Register and choose Register New Assembly



 For that assembly create a new step and give action name in Message



Out Put:



Comments

Popular posts from this blog

2. On Click of case Reject button, show popup with reject reason text, once filled and submitted, reject reason field should get filled and case should be in read only

1. In Case Entity, based on Incident Type selection, filter the case category using the category-incident master entity

8. On selection of asset in case entity, read the project in asset, project start date and end date from project entity, set the dates on case entity from as project start date and end dates