Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Updating some form fields in plugins is not working.

Posted on by 145

Hi , i have a form in the Account entity which contains an email field and two option field called "Email Approve", when i update the email field it doesn't work, but when i update the "Email Approve" it works. The plugin is raised when an update happens to another "Email Approve" field in Case entity on Post Operation. Here is the code:

public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;
            IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            IOrganizationServiceFactory serviceFactory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
            IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.UserId);
            tracingService.Trace("context message:{0}",context.MessageName);
            if (context.MessageName.ToLower() == "update")
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity currentCaseEntity = context.InputParameters["Target"] as Entity;
                    tracingService.Trace("entity logical name is :{0}", currentCaseEntity.LogicalName);
                    if (currentCaseEntity.LogicalName == "incident")
                    {
                        try
                        {
                            if (currentCaseEntity.Attributes.Contains("new_emailapprove"))
                            {
                                bool emailAprrove = currentCaseEntity.GetAttributeValue<bool>("new_emailapprove");
                                tracingService.Trace("email approve value :{0}", emailAprrove);
                                if (emailAprrove)
                                {
                                    Entity preImageCaseEntity = context.PreEntityImages["preImageCaseEntity"];
                                    EntityReference associatedAccountReference =
                                        preImageCaseEntity.GetAttributeValue<EntityReference>("customerid");
                                    Entity associatedAccount = organizationService.Retrieve(
                                        associatedAccountReference.LogicalName,
                                        associatedAccountReference.Id,
                                        new ColumnSet(true));
                                    tracingService.Trace("account name : {0}",associatedAccount["name"]);
                                    associatedAccount["new_emailapprove"] = true;
                                    associatedAccount["new_email"] = "someemail@test.com";
                                    tracingService.Trace("account email after updated: {0}",associatedAccount["new_email"]);
                                    organizationService.Update(associatedAccount);
                                    
                                }
                            }
                           
                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                   
                }
            }
        }


  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Updating some form fields in plugins is not working.

    Hi,

    When you work on update plugins, do remember that in the tager entity you will find only those attributes which are changed. The below statement means that you want to execute your code/logic only when your target entity (currentCaseEntity) contains the attribute new_emailapprove. Now, cause this is update plugin, you will get new_emailapprove in the entity only when you change this field. You won't find this when you check any other fields. This is why you plugin works when you update the "Email Approve" but not other email fields.

    if (currentCaseEntity.Attributes.Contains("new_emailapprove"))

    You need to use plugin images in order to get the data of the entity which is not changed. You can read more about this here:

    crmbook.powerobjects.com/.../plug-in-images-pre-vs-post

    community.dynamics.com/.../214611

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Updating some form fields in plugins is not working.

    Hi ,

    When you said post operation,  You should use post image to get the changes.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans