Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

address field values retained even pass empty value

Posted on by 524

Hi,

I have created a plugin such that when the address fields in account entity are updated, i have update the corresponding contact addresses also. I registered in Update account in postoperation.

entityContact["address1_city"] = accountEntity.Attributes.Contains("address1_city") ? accountEntity["address1_city"].ToString() : string.Empty;

We can get the fields in the context if only we update them right ? If we are not updating the city field in account entity, then the corresponding city field in contact entity should assign the empty value as per the above code... But, eventhough i haven't updated the fields in account, the existing values are retained in the contact. Can you please explain how it happens ?

*This post is locked for comments

  • Anandhcrm Profile Picture
    Anandhcrm 524 on at
    RE: address field values retained even pass empty value

    Hi Ravi,

    I have not did anything on filtering attributes... by default it was all selected.

    This is my code...

    public void Execute(IServiceProvider _serviceProvider)

           {

               try

               {

                   IPluginExecutionContext context = (IPluginExecutionContext)_serviceProvider.GetService(typeof(IPluginExecutionContext));

                   IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)_serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);

                   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                   {

                       Entity accountEntity = (Entity)context.InputParameters["Target"];

                       QueryExpression query = new QueryExpression();

                       query.EntityName = "contact";

                       query.ColumnSet.AllColumns = true;

                       query.Criteria.Conditions.Add(new ConditionExpression("parentcustomerid", ConditionOperator.Equal, accountEntity.Id));

                       EntityCollection entityCollection = _service.RetrieveMultiple(query);

                       foreach (Entity entityContact in entityCollection.Entities)

                       {

                           if (entityContact.LogicalName == "contact")

                           {

                               entityContact["address1_city"] = accountEntity.Attributes.Contains("address1_city") ? accountEntity["address1_city"].ToString() : string.Empty;

                               entityContact["address1_line1"] = accountEntity.Attributes.Contains("address1_line1") ? accountEntity["address1_line1"].ToString() : string.Empty;

                               entityContact["address1_line2"] = accountEntity.Attributes.Contains("address1_line2") ? accountEntity["address1_line2"].ToString() : string.Empty;

                               entityContact["address1_line3"] = accountEntity.Attributes.Contains("address1_line3") ? accountEntity["address1_line3"].ToString() : string.Empty;

                               entityContact["address1_country"] = accountEntity.Attributes.Contains("address1_country") ? accountEntity["address1_country"].ToString() : string.Empty;

                               entityContact["address1_stateorprovince"] = accountEntity.Attributes.Contains("address1_stateorprovince") ? accountEntity["address1_stateorprovince"].ToString() : string.Empty;

                               _service.Update(entityContact);

                           }

                       }

                   }

               }

               catch (Exception ex)

               {

                   throw ex;

               }

           }

  • Suggested answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: address field values retained even pass empty value

    Hi Anand,

    Your code seems fine. Please check what is your trigger point of plugin? As suggested by Ravi also, If you want to map Account's Address to all Contact's Address, then Plugin should be triggered on all account address field's update. (Select all address field under Filtered Attribute dropdown list in Plugin Registration Tool).

    Hope it helps

    If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'. It would be helpful to the other community members seeking to resolve a similar issue.

    Cheers

    Arpit

    arpitmscrmhunt.blogspot.in

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: address field values retained even pass empty value

    Hi,

    Yes it should as suggested by Ravi, You  share your comple updated code here, with registration details for checking

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: address field values retained even pass empty value

    Hi,

    Yes, if the fields are not updated, it won't be appear in the plugin's Target entity

    1) Could you please check if you have set any attribute filtering to execute this plugin. If yes then the plugin won't trigger unless you change the field values mentioned in the attribute filter.

    2) Also, accountEntity is retrieved from the plugin target property?

    Entity target = (Entity)context.InputParameters["Target"];

    If accountEntity  is an image then it will contains those field if they have value in it.

    Hope this helps.

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,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