Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Updated step not updating a filed with a value

Posted on by Microsoft Employee

Hi Everyone,

Writing a plugin to update a field based on certain situations.

I nearly have it working but have one hang up.

The plugin step is a post operation update

Once I update field A want to update Field B automatically and remove or add the value that was added or removed to field A.

It works if I change the value of field A except when i remove the value from field A the value is still in field B.

It gives no error which is annoying and if i add service.update it crashes with an error saying the reference was not in the given dictionary 

else if (!postMessageImage.Contains("codec_primaryprofessionalgroup"))
{
pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
string newGroup = oldProGroups.Replace(pretopic, "");
work["codec_professionalgroups"] = newGroup ;

}

Any ideas?

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updated step not updating a filed with a value

    Hi David,

    yes that is on the preMessage. Below is the full code

    public class UpdateContactProfessionalGroup : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

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

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

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

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

               switch (context.MessageName)

               {

                   case "Create":

                       {

                           // create target entity and post image entity

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

                           Entity postMeImage = (Entity)context.PostEntityImages["postImage"];

                           // create string to store the post image and then add it to the codec_professionalgroups field

                           string postGroup = string.Empty;

                           postGroup = ((EntityReference)postMeImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                           createEntity["codec_professionalgroups"] = postGroup;

                           if (context.Depth <= 1)

                           {

                               service.Update(createEntity);

                           }

                           break;

                       }

                   case "Update":

                       {

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

                           {

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

                               Entity postMessageImage = (Entity)context.PostEntityImages["Image"];

                               Entity preMessageImage = (Entity)context.PreEntityImages["Image"];

                               string posttopic, pretopic = string.Empty;

                               string oldProGroups = "";

                                   oldProGroups = (String)preMessageImage["codec_professionalgroups"];

                               if (postMessageImage.Contains("codec_primaryprofessionalgroup"))

                               {

                                   posttopic = ((EntityReference)postMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                                   if (preMessageImage.Contains("codec_primaryprofessionalgroup"))

                                   {

                                       pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                                       string newGroup = oldProGroups.Replace(pretopic, "");

                                       string Progroup = newGroup + posttopic;

                                       work["codec_professionalgroups"] = Progroup;

                                       if (context.Depth <= 1)

                                       {

                                           service.Update(work);

                                       }

                                   }

                                   else {

                                       string Progroups = oldProGroups + posttopic;

                                       work["codec_professionalgroups"] = Progroups;

                                       if (context.Depth <= 1)

                                       {

                                           service.Update(work);

                                       }

                                   }

                               }

                               else if (!postMessageImage.Contains("codec_primaryprofessionalgroup"))

                               {  

                                   pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                                   string newGroup = oldProGroups.Replace(pretopic, "");

                                   work["codec_professionalgroups"] = newGroup ;

                                   if (context.Depth <= 1)

                                   {

                                       service.Update(work);

                                   }

                               }

                               else

                               {

                                   posttopic = ((EntityReference)postMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                                   pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

                                   string newGroups = oldProGroups + posttopic;

                                   work["codec_professionalgroups"] = newGroups;

                               }

                           }

                           break;

                       }

                   default:

                       break;

               }

           }

       }

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Updated step not updating a filed with a value

    Of the code you've posted, the only line that could give a 'reference was not in the given dictionary' error is

    pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;

    This would occur if codec_primaryprofessionalgroup is not in the preMessageImage. I don't know if you're testing for this, or maybe you've not included the attribute in the preImage configuration.

    Can you post the whole code, as the error may be elsewhere

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans