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 auto populate one entity field value to other entity field value?

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    antc Profile Picture
    2,909 on at

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

                    }
                }
            }
        }

  • ggghj Profile Picture
    160 on at

    Can we use this code for creation entity field value.

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

    service.Create(ee);

  • ggghj Profile Picture
    160 on at

  • ggghj Profile Picture
    160 on at

    Hi ,

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

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    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

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