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 :
Customer experience | Sales, Customer Insights,...
Answered

Plugin to populate lookup record field on current entity.

(0) ShareShare
ReportReport
Posted on by 255

Hello All,

I am not much familier on writing pligin code for the CRM. I dont have much practice so need one help for plugin code.

scenario : i have  two entity "A" and "B" . Entity "A" has fields  "name"(lookup field of entity B) and "id"(text field). i need to populate the lookup entity record's "Caseid"(entity BB) field on "id " of A entity.

can anybody share code with me.

I have the same question (0)
  • Saad Kabarousse Profile Picture
    734 on at

    Hello,

    If i correctly Understand, in your Entity A you have two fields :

    Field1 is lookup on EntityB

    Field2 is Simple line text (That contains the id of the record in the Field1)

    Field1 is already filled , and you want to fill Field2 in all records of Entity A ?

  • Mona Chavan Profile Picture
    255 on at

    Thanks for the reply.

    you considered it as reverse

    Let consider ,

    Entity: A and B

    A: has two fields

    1) "Name" lookup of entity B

    2)Id : text field

    B:

    1)Case id (text field)

    so when i change the lookup value of "Name" in entity "A" its related  entity B "Case id" data should be populate in Entity A "Id" text field.

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at

    public void Execute(IServiceProvider serviceProvider)

           {

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

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

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

               try

               {

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

                       return;

                   var entityA = (EntityReference)context.InputParameters["Target"];

                   if (entityA.LogicalName != "Case") // if entity A si Case

                       return;

    Entity entityA = service.Retrieve("entityA_name", entityA.Id, new ColumnSet("field1_name"));

    Guid EntityB_ID =  ( EntityA.GetAttributeValue<EntityReference>("field1_name")).Id; // get Guid of Record from Lookup on entity B

    Entity EntityB = service.Retrieve("entityB_name", EntityB_ID, new ColumnSet(true));

    EntityB["Caseid"] = entityA.ID;

    service.Update(EntityB);

               }

               catch (InvalidPluginExecutionException e)

                   {

                   throw new InvalidPluginExecutionException(e.Message);

               }

           }

    Please Mark as Verified  if this Helps :)

  • Mona Chavan Profile Picture
    255 on at

    Thanks.

    correct me if i am wrong.

    "service.Update(EntityB);"

    For above line i think we should write service.Update(EntityA) because lookup which is going to update is present on Entity A.

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at

    No EntityB because this is a post operation on update of your EntityA record, it means that after you change lookup on the recordA the plugin will change field "id" in entityB and update the record.

    If this is what you want?

  • Mona Chavan Profile Picture
    255 on at

    for more clarification please see below.

    Entity A: 

    Client field is lookup of Entity B.

    pastedimage1571371745655v2.png

    Entity B:

    pastedimage1571371801514v3.png

    when i select Entity B record from Entity A.

    CIS key/ANG Reg fields value should get populate in "Client id" field of Entity A.

  • Mona Chavan Profile Picture
    255 on at

    no i want to  update the Entity "A" record "Clent id " based on value present in Entity B "Cis key/ANG ref" field as shared in above screenshot.

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at

    Hello,

    Okey i see it clearly now, well you can just use Dynamics CRM Workflows to do this without Plugin if you want to (it's recommended to do so).

    Or just using Javascript with some thing like this (no need for plugin):

      function getClienid(){

       if (Xrm.Page.data.entity.attributes.get("lookup_field_schema_name").getValue() != null) {

    var CustomerId = Xrm.Page.data.entity.attributes.get("lookup_field_schema_name").getValue()[0].id; //CustomerId GET GUID of the EntityB record that is associated in the Lookup

    var guid = CustomerId.replace("{", "").replace("}", "");

    XrmServiceToolkit.Rest.Retrieve(guid, "EntityBSet", "CIS_key_ANG_Reg_schema_name", null, function(result) {// get CIS_key_ANG_Reg from EntityB

    var CIS_key_ANG_Reg = result.CIS_key_ANG_Reg; //CIS_key_ANG_Reg contains the CIS_key / ANG_Reg

    Xrm.Page.getAttribute("ClientID_schema_name").setValue(CIS_key_ANG_Reg); //  Setting the value of CIS_key_ANG_Reg in ClientID

    }, function(error) {

    Xrm.Utility.alertDialog(error.message);

    }, true);

    }

      }

    To Make this code work :

    1- Add XrmServiceToolkit.js to your form (because u are uding Dynamics 2011)

    2- Associate this function to onChange of your field

    pastedimage1571386909180v1.png

    3- That' All :)

    Try it and give me feed back

  • Mona Chavan Profile Picture
    255 on at

    Thanks but can you just help me with plugin.

  • Suggested answer
    DynamicsAlex Profile Picture
    25 on at

    // Register this in PRE-UPDATE of EntityA, set FILTERING ATTRIBUTES to only include "NAME" field.

    // UPPER-CASE STRINGS NEED TO BE REPLACED WITH THE CORRECT NAMES OF ENTITIES AND FIELDS

    public void Execute(IServiceProvider serviceProvider)

    {

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

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

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

           // grab EntityA record being updated

    var entityA_Record = context.InputParameters["Target"] as Entity ?? throw new InvalidPluginExecutionException("Target");

           // grab the content of "Name" field

    var entityB_reference = entityA_Record.GetAttributeValue<EntityReference>("ENTITYA_NAME");

           // if "Name" is empty, clear "Id"

    if(entityB_reference == null)

    {

       entityA_Record["ENTITYA_ID"] = null;

    }

    else

    {

               // if "Name" is not empty, grab the EntityB record and put "CaseId" in "Id"

       var entityB_record = service.Retrieve(entityB_reference.LogicalName, entityB_reference.Id, new ColumnSet("CASEID"));

    var caseId = entityB_record.GetAttributeValue<string>("CASEID");

       entityA_Record["ENTITYA_ID"] = caseId;

    }

       // I can't remember if the following line is needed

       // put back the data of EntityA where you got it to make it so the fields are updated properly

       context.InputParameters["Target"] = entityA_Record;

    }

    Note that in recent versions of CRM this can be achieved by out-of-the-box calculated fields, I'd consider upgrading.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans