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)

Plugin: Update current entity in post-operation

(0) ShareShare
ReportReport
Posted on by 65

Hello

I have the code below which works fine in pre-operation and pre-image. But I need it to work in post-operation. Any ideas on how to adjust the code?

using System;
using Microsoft.Xrm.Sdk;

namespace CalculateScorePlugin
{
    public class CalculateScore : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);
            {
                if (context.InputParameters.Contains("Target"))
                {
                    if (context.InputParameters["Target"] is Entity)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        if (entity.LogicalName == "contact")
                        {
                            if (entity.Attributes.Contains("crmn_ourdescription"))
                            {
                                try
                                {
                                    Entity image = context.PreEntityImages["image"];
                                    /*
                                    int sumFakturert = image.GetAttributeValue<int>("crmn_invoiceamount");
                                    int antTimerOrdinarTid = image.GetAttributeValue<int>("crmn_workinghoursordinary");
                                    int antOppdrag = image.GetAttributeValue<int>("crmn_assignmentinvolvements");
                                    */
                                    int kundelikes = image.GetAttributeValue<int>("new_kundelikes");
                                    String forsteVurdering = image.FormattedValues["new_frstegangsvurdering"].ToString();

                                    int score = kundelikes * 100;
                                    

                                    entity.Attributes["description"] = score.ToString();
                                    //entity.Attributes.Add("description", test);
                                    //client.Update(entity);
                                }
                                catch (Exception ex)
                                {
                                    throw new InvalidPluginExecutionException("Howdy! An error occured in the plugin:", ex);
                                }
                            }

                        }
                        else return;
                    }
                }
            }

        }
    }
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi k3000,

    just change:

    context.PreEntityImages["image"]; --> context.PostEntityImages["image"]; // remember to register post entity image

    A good practice is to use a new "clean" entity to update current entity to avoid updating the same values, like:

    Entity updateentity = new Entity(entity.LogicalName);

    updateentity.Id = entity.Id;

    updateentity["new_mychangeattribute"] = mychangedvalue;

    client.Update(updateentity);

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Reinforcing Francesco's comment:

    Use a separate entity to update the Current record:

    Entity contact = new Entity("contact");

    contact.Id = entity.Id;

    contact["description"] = score.ToString();

    service.Update(contact);

    Hope this helps.

  • k3000 Profile Picture
    65 on at

    perfect! I knew about the PostEntityImages part, but i couldn't figure out how to avoid ending up in a loop. Now it works. Thank you!

  • Suggested answer
    Community Member Profile Picture
    on at

    Code with required changes:

    using System;

    using Microsoft.Xrm.Sdk;

    namespace CalculateScorePlugin

    {

       public class CalculateScore : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

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

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

               IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);

               {

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

                   {                    

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

                           if (entity.LogicalName == "contact")

                           {

                               if (entity.Attributes.Contains("crmn_ourdescription"))

                               {

                                   try

                                   {

                                       Entity image = context.PostEntityImages["image"]; // Change the PreEntityImages as PostEntityImages and register post image "image" with related plugin step

    Entity contactTemp = new Entity(entity.LogicalName);

    contactTemp.Id = entity.Id;

                                       int kundelikes = image.GetAttributeValue<int>("new_kundelikes");

                                       String forsteVurdering = image.FormattedValues["new_frstegangsvurdering"].ToString();

                                       int score = kundelikes * 100;

                                       contactTemp.Attributes["description"] = score.ToString();

                                       client.Update(entity);

                                   }

                                   catch (Exception ex)

                                   {

                                       throw new InvalidPluginExecutionException("Howdy! An error occured in the plugin:", ex);

                                   }

                               }

                           }

                           else return;                  

                   }

               }

           }

       }

    }

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