locked
how to set value to lookup value in crm 2016 using c# RRS feed

  • Question

  • I have created two fields.1.textcountry,2.lookupcountry on account.

    when textcountry value changed ,I need to set textcountry val to lookupcountry .How can I set?

    Friday, September 23, 2016 6:27 AM

Answers

  • this code is for Post Operation of update step, my code will work for pre operation of update step, in your code you are again updating the entity using service.update, but my code will not need that update, Both is correct, just that your code have another service call to update the same entity.

    Also Please mark my comment as answer if it solves problem


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Friday, September 23, 2016 7:34 AM

All replies

  • using c# or javascript?? if using c# write a plugin to retrieve countries where text matches the country. If JS write a function on change of the field to retrieve and set the value.

    For C# use retrieve multiple functionality and for JS use the WEB API method to retrieve.


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Friday, September 23, 2016 6:40 AM
  • I did using c#.But not updated.?Please find below code

     if (entity.Attributes.Contains("address1_county") && entity.Attributes.Contains("address1_stateorprovince"))
                                {
                                    
                                    string countryVal = (string)entity.Attributes["address1_county"];
                                    QueryExpression query = new QueryExpression("new_country");
                                    query.ColumnSet.AddColumns("new_countryid");
                                    query.Criteria.AddCondition("new_name", ConditionOperator.Equal, countryVal);
                                    EntityCollection result=service.RetrieveMultiple(query);
                                   
                                  Guid id= result[0].GetAttributeValue<Guid>("new_countryid");
                                  //Guid counid = result[0].Id;
                                  //entity.Attributes["new_testcountry"] = new EntityReference("new_country", new Guid("2907dcfc-e87e-e611-80e0-c4346bad4380")); 
                                  // new EntityReference("new_country", new Guid("2907dcfc-e87e-e611-80e0-c4346bad4380")); 

                                  entity["new_testcountry"] = new EntityReference("new_country", id); 
                                       service.Update(entity);
                                       
                                        
                              }

    Friday, September 23, 2016 6:41 AM
  • Entity entity = context.InputParameters["Target"] as Entity;
    if (entity.Attributes.Contains("address1_county")) {
        string countryVal = (string) entity.Attributes["address1_county"];
        QueryExpression query = new QueryExpression("new_country");
        query.ColumnSet.AddColumns("new_countryid");
        query.Criteria.AddCondition("new_name", ConditionOperator.Equal, countryVal);
        EntityCollection result = service.RetrieveMultiple(query);
        if (result.Entities.Count > 0) {
            entity["new_testcountry"] = new EntityReference("new_country", result.Entities[0].Id);
        }
    }

    kindly try this code in Pre-operation of update of the field. Also note that the name you enter should exactly match the country


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Friday, September 23, 2016 6:47 AM
  • I tried u r code  .But not updated.
    Friday, September 23, 2016 7:09 AM
  • Can you please post the plugin code complete and also the plugin step registration screenshot

    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Friday, September 23, 2016 7:24 AM
  • Hi Hari .

    Thank you very much .Now It is updated .I did  small modification of your code.Please find below code.

    string countryVal = (string)entity.Attributes["address1_county"];
                                    QueryExpression query = new QueryExpression("new_country");
                                    query.ColumnSet.AddColumns("new_countryid");
                                    query.Criteria.AddCondition("new_name", ConditionOperator.Equal, countryVal);
                                    EntityCollection result=service.RetrieveMultiple(query); 
                                            if (result.Entities.Count > 0) {
                                                Entity acc = new Entity("account");
                                                acc.Id = entity.Id;
                                                  acc["new_testcountry"] = new EntityReference("new_country", result.Entities[0].Id);
                                                  service.Update(acc);
                                            }

    Friday, September 23, 2016 7:28 AM
  • this code is for Post Operation of update step, my code will work for pre operation of update step, in your code you are again updating the entity using service.update, but my code will not need that update, Both is correct, just that your code have another service call to update the same entity.

    Also Please mark my comment as answer if it solves problem


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Friday, September 23, 2016 7:34 AM