Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Reg Update field between two entities.

(0) ShareShare
ReportReport
Posted on by 560

Hi everyone,

I have  simple task using plugin to update the field in contact entity from the account entity based on some condition.

Up to to now I have competed fetching the corresponding value to update from the account entity and when I try to update in the contact entity it throws some error.

I have seen there is no GUID for the contact.That's why its throwing error I think so.

I have attached the code below for your reference.

Please advise.

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


namespace Mobile_Update
{
    public class Class1 : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
              (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

            IOrganizationServiceFactory factory =
               (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationServiceFactory servicefactory =
               (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly. 

                if (entity.LogicalName == "account")
                {
                    var id = entity.Id;//.ToString();
                    ColumnSet myAttributes = new ColumnSet(new String[] { "primarycontactid" });
                    Entity entity1 = client.Retrieve("account", id, myAttributes);
                    var myLookupId = ((Microsoft.Xrm.Sdk.EntityReference)(entity1.Attributes["primarycontactid"])).Name;
                    if (myLookupId != null)
                    {
                        var mobile = client.Retrieve("account", id, new ColumnSet(new String[] { "new_mobile_bharath" }));
                        var mobile_num = mobile.Attributes.Values.Select(m => m).ElementAt(0);
                        try
                        {
                            Entity updateaccount = new Entity("contact");
                            updateaccount["new_mobile_contact"] = mobile_num;
                            client.Update(updateaccount);
                        }
                        catch (Exception e)
                        {
                            throw;
                        }
                    }

                }
            }
        }
    }
}








*This post is locked for comments

  • AXTechie2120 Profile Picture
    AXTechie2120 560 on at
    RE: Reg Update field between two entities.

    Thanks SreeValli,

    I figured it out with your help.

    Thanks

  • AXTechie2120 Profile Picture
    AXTechie2120 560 on at
    RE: Reg Update field between two entities.

    Hi,

    I tried your solution but it's throwing the below error,

    "Entity Id must be specified for Update'"

     


    Please advise on this.

  • Verified answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Reg Update field between two entities.

    Hi,

    you are right, we have to add "Id" to the entity object. See the highlighted changes 

    var myLookupId = ((Microsoft.Xrm.Sdk.EntityReference)(entity1.Attributes["primarycontactid"])).Id;
                        if (myLookupId != null)
                        {
                            var mobile = client.Retrieve("account", id, new ColumnSet(new String[] { "new_mobile_bharath" }));
                            var mobile_num = mobile.Attributes.Values.Select(m => m).ElementAt(0);
                            try
                            {
                                Entity updateaccount = new Entity("contact");
    							updateaccount.Id = myLookupId;
    							updateaccount["new_mobile_contact"] = mobile_num; client.Update(updateaccount); 
    						} 
    						catch (Exception e) { throw; } 
    					} 
    } 



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