Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Reg Update field between two entities.

$
0
0

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;
                        }
                    }

                }
            }
        }
    }
}









Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>