web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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.

  • Shadendra Profile Picture
    40 on at

    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
    13,758 on at

    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.

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    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.");
    }
    }
    }
    }
    }

  • Shadendra Profile Picture
    40 on at

    entertheinputargumentfromactionhere  no argument in my action

  • Shadendra Profile Picture
    40 on at

    its working thanx

    Rawish

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans