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)

Update a field based on criteria between entities

(0) ShareShare
ReportReport
Posted on by 568

Hi everyone,

Hope you had a good day.

I have two entities namely A and B.now i want to check a field between 2 entities and if they both are same i want to pass the value of field in entity B to entity A..

Can you guys explain how to do it using plugin with some sample code..

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    To get the details/field of an entity, you need to know the Guid value so that you can retrieve the record. Also, plugins works on a trigger i.e. "Create, Update, delete" I am not sure as per your requirement what trigger you are looking for. Also, there are times when a requirement can be easily achieved using out of box workflow than writing a plugin. Further, if the values are same, why do you want to pass values?

    Regarding the sample code, you can refer the below for retrieve-

    ==================

    public class SamplePlugin2: IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               if (serviceProvider == null)

                   throw new ArgumentNullException("serviceProvider");

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

               var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               var service = serviceFactory.CreateOrganizationService(context.UserId);

               var adminService = serviceFactory.CreateOrganizationService(null);

               var trace = new Tracer(service, GetType().ToString(), tracingService);

               var message = context.MessageName.ToLowerInvariant();

               // Retrieve first entity

               var entityA = service.Retrieve("new_entityA", new Guid("7468F46B-B955-E911-A975-000D3AE0562F"), new ColumnSet(true));

               // Retrieve second entity

               var entityB = service.Retrieve("new_entityB", new Guid("AC1d91679fee507d4a1ee7f1cea7a5b5dc"), new ColumnSet(true));

               // Compare Field from both entity (assuming they are text fields

               if (entityA.GetAttributeValue<string>("new_fieldA") == entityB.GetAttributeValue<string>("new_fieldB"))

               {

                   // Update entity

                   var entityAUpdate = new Entity("new_entityA", new Guid("7468F46B-B955-E911-A975-000D3AE0562F"));

                   entityAUpdate["new_fieldA"] = entityB.GetAttributeValue<string>("new_fieldB");

                   service.Update(entityAUpdate);

               }

           }

       }

    ==================

    Hope this helps.

  • AXTechie2120 Profile Picture
    568 on at

    Hi thanks for the reply.

    Can you suggest how to do this with query.

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)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans