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)

Update a text field from a lookup

(0) ShareShare
ReportReport
Posted on by 795

Hello,

I am working on Plugins in Online Dynamics 365 and i want to update a text field from lookup's GUID value on create of a  record. 

Example: I have a job running each hour which brings records in CRM and as soon as record is created for that entity, it should pick up the lookup's id and get it saved in another text field in Plugin. Currently, i am using concept of "PostImage" on create message. But, no luck.

Can anyone provide any suggestions please?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    If you trigger your plugin on create all the attributes will be present during creation. Even for update, the lookup value will be available in the target entity.

    So, you don't need plugin images to copy a lookup guid to a text field. You can simply create a plugin which checks if the target contains the lookup field, if yes then get the id and saves it in the text field. I would suggest register the plugin in pre stage. This way you don't need to issue an additional update request to update the value on the text field.

    Hope this helps.

  • CRM Beginner  Profile Picture
    795 on at

    Yes Ravi, thanks alot.. I resolved it earlier itself and verified with your suggestion.. I did right.. Thanks

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    You can get all attributes in Target entity on create message hence do not use post image.

    Copy lookup id to text field and add this property in target entity as shown below:

    if(targetEntity.Contains("Text field name"))

    {

    targetEntity["Text field name"]="Guid of lookup";

    }

    else

    {

    targetEntity.Attributes.Add("Text field name","Guid of lookup");

    }

    No need to update entity.

  • CRM Beginner  Profile Picture
    795 on at

    I am getting an issue :

    try

               {

                   // The InputParameters collection contains all the data passed

                   // in the message request.

                   if (context.InputParameters.Contains("Target") &&

                       context.InputParameters["Target"] is Entity)

                   {

                       // Obtain the target entity from the Input Parameters.

                       tracingService.Trace("Target entity is goldencustomermapping");

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

                       tracingService.Trace(entity.LogicalName);

                       if (entity.LogicalName != "new_goldencustomermapping")

                           return;

                       //bool goldenExistence = entity.Attributes.Contains("new_goldenid");

                       //if (goldenExistence)

                       //{

                           var goldenRecord = (Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["new_goldenid"]);

                           Guid goldenID = goldenRecord.Id;

                           entity["new_goldencustomerid"] = goldenID.ToString();

                           service.Update(entity);

                       }

      First time it worked well, but if i manually deleted the records from CRM, next tym it throws error : "entity with the specific ID doesn't exist".. I don't get it why this issue is coming?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Your plugin will not trigger on delete record unless you register the step for delete. It must be some other plugin. You can verify this by disabling your plugin and then reproducing the issue.

    Share plugin registration details.

    Hope this helps.

  • Suggested answer
    Pramod M Profile Picture
    1,445 on at

    Hi,

    As per me you shold not do this action in post plugin. Pre Create Plugin is the best place to udpate the field. In pre create plugin you only need to do the following.

                         var goldenRecord = (Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["new_goldenid"]);

    if( goldenRecord  != null)

    {

                          var goldenID = goldenRecord.Id;

                          target["new_goldencustomerid"] = goldenID.ToString();

    }

  • CRM Beginner  Profile Picture
    795 on at

    Hi,

    Thank you all for your reply. Just a little more help, i used a combination of two fields as an alternate key in this particular entity. Do i need to mention anything in plugin code or else it will work automatically as it works in case of  single alternate key ?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    It should work as is? You don't need to do anything special on plugin for this i.e. if there is a duplicate error, you will get an error and your plugin will fail.

    Hope this helps.

  • CRM Beginner  Profile Picture
    795 on at

    Okay, then you mean to say that i simply update the text field with lookup's id .. That's it . And rest combination of alternate keys is in active state and everything will work as is to prevent duplicate records. Right?

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Yes right. The restriction of duplicate record creation based on alternate keys happens on the database level and in your plugin you are creaing records which will hit the database post creation, it the db instertion fails then the plugin will fail and the operation which triggers operation will also fail.

    Plugin and alternate keys are not something you should compare/ relate or think about. Regardless of how you are creating & updating the records, the alternate keys will be checked and error will be thrown.

    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

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