Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to auto populate one entity field value to other entity field value?

Posted on by 160

Hi,

Both entity are in a relation and field value is same for both entity.If i insert manually all field value in one entity field then the related entity field value auto populate through plugin.

*This post is locked for comments

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    Hemant Kumar Sahu 1,825 on at
    RE: How to auto populate one entity field value to other entity field value?

    Hi, 

    It will be better if you use  quick view form.

    Otherwise on update of entity write a plugin and get all the related entity and update.

    Thanks
    Hemant

  • ggghj Profile Picture
    ggghj 160 on at
    RE: How to auto populate one entity field value to other entity field value?

    Hi ,

    At the time of registration a plugin i used: message type::-create and post operation

  • ggghj Profile Picture
    ggghj 160 on at
    RE: How to auto populate one entity field value to other entity field value?

  • ggghj Profile Picture
    ggghj 160 on at
    RE: How to auto populate one entity field value to other entity field value?

    Can we use this code for creation entity field value.

    Entity ee = new Entity("entity name");

    service.Create(ee);

  • Suggested answer
    antc Profile Picture
    antc 2,909 on at
    RE: How to auto populate one entity field value to other entity field value?

    Depends on the relationship 1:1 or 1:N etc

    If 1:1 and only update in 1 entity you can update with process.

    The problem is if you update in either you can start looping although checking pre and post image will stop the plugin looping.

    If 1:N use a plugin:

     public class Update : 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);
                var ServiceContext = new OrganizationServiceContext(service);
                if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity))
                {
                    Entity entity1 = (Entity)context.InputParameters["Target"];
                    Entity preContact = context.PreEntityImages["Image"];
                    Entity postContact = context.PostEntityImages["PostImage"];
                    string origfield = preContact.GetAttributeValue<string>("entity1field");
                    string difffield = postContact.GetAttributeValue<string>("entity1field");


                    if (origfield != difffield)
                    {
                        try
                        {
                            //Create query to get the related field
                            var res = from c in ServiceContext.CreateQuery("entity2")
                                      where c["field"].Equals(entity1PK.Id)
                                      select c;

                            foreach (var c in res)
                            {
                                Entity e = (Entity)c;
                                e["entity2field"] = difffield;

                                //ServiceContext.Attach(e);
                                ServiceContext.UpdateObject(e);
                            }

                            ServiceContext.SaveChanges();
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.");


                        }
                        catch (Exception ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }

                    }
                }
            }
        }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans