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)

Change entered value before save

(0) ShareShare
ReportReport
Posted on by 120

Hi Folks,

I have a Little problem while writing a Plugin...

Basically I want to change a value I entered depending on Information from a different Entity right after I entered it in the Textfield. The problem is, that when I want to Access this value in my plugin and modify it, it gives me an KeyNotFoundExeption, because this all happens before saving the form.

Is there any way, to get the current value via C#?

I tried to use QueryExpression, but am facing the same problem because the value is not saved in the Database yet.

I am using the Update Message atm but tried a few others already but every time it's the same error.

Dr. Google does not help me this time, so I thought that some experts here could give me a few tips how to tackle this Task.

*This post is locked for comments

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

    i guess you should trigger the plugin i asynchronous or synchronous mode  ....i am not sure might be either option other than default while triggering the plugin

  • R.Seiffert Profile Picture
    120 on at

    Hi Guido.

    The Code is:

    protected void ExecuteUpdatePhoneNumber(LocalPluginContext localContext)

           {

               if (localContext == null)

               {

                   throw new ArgumentNullException("localContext");

               }

               else

               {

                   IPluginExecutionContext     context =   (IPluginExecutionContext)localContext.PluginExecutionContext;

                   IOrganizationService        service =   (IOrganizationService)localContext.OrganizationService;

                   ITracingService             trace   =   (ITracingService)localContext.TracingService;

                   Entity                      entity  =   null;

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

                   {

                       //Load entity

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

                       var GuID = entity.Id;

                       //QUERYEXPRESSION

                       QueryExpression query = new QueryExpression();

                       query.EntityName = "contact";

                       ColumnSet col = new ColumnSet("new_land","telephone1");

                       query.ColumnSet = col;

                       query.Criteria.AddCondition("contactid", ConditionOperator.Equal, GuID);

                       EntityCollection contactscollection = service.RetrieveMultiple(query);

                       foreach (Entity e in contactscollection.Entities)

                       {

                           var name = e.LogicalName;

                           //var land = e.GetAttributeValue<"new_land">

                           EntityReference landlookup = (EntityReference)e.Attributes["new_land"];

                           var landString = landlookup.Name;

                           var phoneNumber = e.Attributes["telephone1"].ToString();

                           EntityReference phonelookup = (EntityReference)e.Attributes["telephone1"];

                           var phonestring = phonelookup.Name;

                       }

                   }

               }

           }

    While debuggin via Plugin Registration Tool I can see that at the Moment of the execution there are only 2 Attributes of the Entity filled. "new_land" and the "contactid".

  • R.Seiffert Profile Picture
    120 on at

    Hi Nithin,

    I tried both ways and every time I get the KeyNotFoundExeption.

  • Community Member Profile Picture
    on at

    i guess one the field is empty which u try to fetch

  • R.Seiffert Profile Picture
    120 on at

    Hi Nithin,

    yes exactly. The phone number is not stored in the database yet. That's why my question was if there is any way to get the entered value before the form is saved. My Goal is to check if the phone number starts with a 0 and if so, i want to Access the Land Entity to get the right prefix (+49 for Germany etc.) and then replace the 0 with this value before saving everything.

  • hellosuresh Profile Picture
    539 on at

    Create a another entity which contains contactId, and phone attributes.

    onchangeevent of phone number,  try to do ajax call and create/update the record with contactId.  In the plugin, then query the this entity by contactid and get the phone value.

    This is one workaroud. Note this works only when phone control lost focus the event will trigger.

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