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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to get Entity reference id in plugin

(0) ShareShare
ReportReport
Posted on by

if (entity.LogicalName == "account")
{
Entity Account = (Entity)service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
tracingService.Trace("1");
Guid ContactGuid = Account.Id;
tracingService.Trace("9");
//throw new Exception("Contact guid" + ContactGuid);
//objContact = service.Retrieve("contact", ContactGuid, new ColumnSet(new string[] { "new_account" }));
Entity objContact = (Entity)service.Retrieve("contact", ((EntityReference)Account.Attributes["new_account"]).Id, new ColumnSet(true));
tracingService.Trace("2");
string Updatename = Account.GetAttributeValue<string>("name");
tracingService.Trace("3");
objContact.Attributes["fullname"] = Updatename;
tracingService.Trace("4");
string Updateemail = Account.GetAttributeValue<string>("emailaddress1");
tracingService.Trace("5");
objContact.Attributes["emailaddress1"] = Updateemail;
tracingService.Trace("6");
string Updateaddress = Account.GetAttributeValue<string>("address1_composite");
tracingService.Trace("7");
objContact.Attributes["address1_composite"] = Updateaddress;

service.Update(objContact);
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    This is how you need to get the id of your target entity;

    namespace Plugins

    {
        public class PluginName : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = Connection.CRMDefault();
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    string id = entity.Id.ToString();
                }
            }
        }
    }

    Hope it helps
    Regards,

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans