Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Update field with plugin

(0) ShareShare
ReportReport
Posted on by 65

I am new to plugins. I need to create a plugin that does the following on contact entity:

Retrieve value from custom_field_A

Retrieve value from custom_field_B

Retrieve value from custom_field_C

Do some mathematical calculations with the fieldvalues

Put the result into custom_field_D

Can anyone help me with this code? I am guessing it's quite simple, but being new to this, it would be nice to get some starting help.

*This post is locked for comments

  • k3000 Profile Picture
    k3000 65 on at
    RE: Update field with plugin

    ok. It might have been another plugin causing this error.

    OK thanks for your help. I will keep working on it:)

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Update field with plugin

    Arpit,

    Check your code before you post it. It just doesn't work. Recheck your Update scenario... It doesn't work at all.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Update field with plugin

    Your plugin can't cause that exception based on the code you posted.

    But your code will work properly only in "Create" scenario. For Update Scenario - it will use only previous values (what if field was changed - you have to take it from Target and not from Image) also to put it again - use Target, not Image.

    Good luck.

  • k3000 Profile Picture
    k3000 65 on at
    RE: Update field with plugin

    I know about calculated fields. The problem is that we have reached our limit of 10 and cannot create anymore fields of this type. Another thing is that the calculation we are going to do in the future, seems more complex than calculated fields can handle.

  • k3000 Profile Picture
    k3000 65 on at
    RE: Update field with plugin

    Here is the code:

    using Microsoft.Xrm.Sdk;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace ContactCalculator

    {

       public class ContactCalculatorPlugin : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               // Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracingService =

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

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)

               serviceProvider.GetService(typeof(IPluginExecutionContext));

               // Obtain the organization service reference.

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

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

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

               {

                   // Obtain the target entity from the input parameters.

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

                   Entity contactimage = context.PreEntityImages["Target"];

                   string ourdescription = string.Empty;

                   string total = string.Empty;

                   string custom_field_A = string.Empty;

                   string custom_field_B = string.Empty;

                   string custom_field_C = string.Empty;

                   string custom_field_D = string.Empty;

                   if (context.MessageName.ToUpper() == "CREATE")

                   {

                       if (contact.Attributes.Contains("custom_field_A"))

                       {

                           custom_field_A = contact["custom_field_A"].ToString();

                       }

                       if (contact.Attributes.Contains("custom_field_B"))

                       {

                           custom_field_B = contact["custom_field_B"].ToString();

                       }

                       if (contact.Attributes.Contains("custom_field_C"))

                       {

                           custom_field_C = contact["custom_field_C"].ToString();

                       }

                       // Do ur calculation as per ur requirement and store it in custom_field_D variable

                       custom_field_D = custom_field_A + custom_field_B + custom_field_C;

                       contact["custom_field_D"] = custom_field_D;

                   }

                   if (context.MessageName.ToUpper() == "UPDATE")

                   {

                       if (contactimage.Attributes.Contains("crmn_ourcandidaterating"))

                       {

                           ourdescription = contactimage["crmn_ourcandidaterating"].ToString();

                       }

                       if (contactimage.Attributes.Contains("custom_field_A"))

                       {

                           custom_field_A = contactimage["custom_field_A"].ToString();

                       }

                       if (contactimage.Attributes.Contains("custom_field_B"))

                       {

                           custom_field_B = contactimage["custom_field_B"].ToString();

                       }

                       if (contactimage.Attributes.Contains("custom_field_C"))

                       {

                           custom_field_C = contactimage["custom_field_C"].ToString();

                       }

                       // Do ur calculation as per ur requirement and store it in custom_field_D variable

                       total = ourdescription;

                       contactimage["description"] = "testing 123";

                   }

                   // Write this plugin on Pre-Operations Stage to avoid one additional transaction to update the value in DB

                   // can write this plugin on Create as well as on Update

                   // Can avoid below line by reg plug-in in Pre Operation Stage

                   // Service.update(contact);

               }

           }

       }

    }

  • Suggested answer
    manojd  Profile Picture
    manojd 1,397 on at
    RE: Update field with plugin

    Hi,

    If all this custom fields are in contact entity only & field type is number then you don’t need plugin

    What you can do is create new custom field as calculated field type & do all your mathematical calculations of field a,b & c

    So as a result whenever user changes  any field of a,b & c your cacluted field will be auto updated with desired result

    Hope it will solve your problem & remember to mark this as answer

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Update field with plugin

    Can you please provide code of your plugin?

  • k3000 Profile Picture
    k3000 65 on at
    RE: Update field with plugin

    Thank you. One step closer. The plugin launches now, but it fails. When I look at the systemjob in Dynamics it gives us this message:

    The token provider was unable to provide a security token while accessing 'losangmdm-sb.accesscontrol.windows.net/.../&;. Token provider returned message: 'Error:Code:401:SubCode:T0:Detail:ACS50008: SAML token is invalid.:TraceID:4298b45f-8c1a-473a-9fb1-d297b61bbf5d:TimeStamp:2017-12-01 15:22:57Z'.

    Any ideas what this might be?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Update field with plugin

    Hello,

    Have you registered image in plugin registration tool? You have to do that.

  • k3000 Profile Picture
    k3000 65 on at
    RE: Update field with plugin

    Thanks for the code. I tried this, but I am getting an error. When I ran the debugger tool it pointed to

    Entity contactimage = (Entity)context.PreEntityImages["Target"];

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans