Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Trigger plugins using Ribbon workbench button

(0) ShareShare
ReportReport
Posted on by 40

I want to call plugin using java script

this function send entityrefernce only 

function CallAction() {

var currentRecordIdString = Xrm.Page.data.entity.getId();
var currentRecordId = currentRecordIdString.replace("{", '').replace("}", '');

Process.callAction("new_Trigger",
[{
key: "Target",
type: Process.Type.EntityReference,
value: new Process.EntityReference("contact", Xrm.Page.data.entity.getId())
}],
function (params) {
// Success
alert("success");
},
function (e, t) {
// Error
alert("error");
});
}

Plugin code is given exception than entityreference can't convert to entity

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace testplugin
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{

Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));


if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{

Entity entity = (Entity)context.InputParameters["Target"];

if (entity.LogicalName == "contact")
{
if (entity.Attributes.Contains("jobtitle") == false)
{
entity.Attributes.Add("jobtitle", "MPS");
}
else
{
throw new InvalidPluginExecutionException("The description can only be set by the system.");
}
}
}
}
}
}

*This post is locked for comments

  • Shadendra Profile Picture
    Shadendra 40 on at
    RE: Trigger plugins using Ribbon workbench button

    its working thanx

    Rawish

  • Shadendra Profile Picture
    Shadendra 40 on at
    RE: Trigger plugins using Ribbon workbench button

    entertheinputargumentfromactionhere  no argument in my action

  • Verified answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Trigger plugins using Ribbon workbench button

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

    namespace testplugin
    {
    public class Class1 : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {

    var contactentity = (EntityReference)context.InputParameters["entertheinputargumentfromactionhere"];
    var 

    if (contactentity.LogicalName == "contact")
    {
    var contact = new Entity("contact");
    contact.Id = contactentity.Id; //retrieve entity id from action
    contact["jobtitle"] = "MPS";
    service.Update(contact);
    }

    else
    {
    throw new InvalidPluginExecutionException("The description can only be set by the system.");
    }
    }
    }
    }
    }

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Trigger plugins using Ribbon workbench button

    Yeah thats what i said , you will get id ,you can then use that id to update the field like below:

    var contact= new Entity("contact");

    contact.Id = entity.Id;           //retrieve entity id from action                                    

    contact["jobtitle"] = "MPS";

    service.Update(contact);

    using above you will be able to update the record.

  • Shadendra Profile Picture
    Shadendra 40 on at
    RE: Trigger plugins using Ribbon workbench button

    System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'.'

    it,s not contain attributes only id and name it contains

    I want to update text field using entity

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Trigger plugins using Ribbon workbench button

    You have to actually take the input parameter as EntityReference, so it would be :

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

    //then access the properties like this.

    var guid = entity.id;

    string logicalname = entity.LogicalName

    Using above you can then retrieve the field from above entity since you have id and logical name by doing service.retrieve.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans