locked
updating record values through C# RRS feed

  • Question

  • Hi,

    I am still learning how to code in CRM.

    I am working on CRM 2013 on prem. I am trying to update a record in CRM(I have two applications as source and target. So updating target from source) through a custom app but I am seeing this error:

    "Entity Id must be specified".

    Can someone please clarify how to set the entity id and why do I have to specify entity id as it is a unique id and I don't want to update it.

    This is the code snippet that I am working on:

     Entity existingContact=new Entity("new_lap");
      existingContact["new_interpretercontactid"]=Row.newAutoContactID;// this is a unique Id column which is same in both applications. Row contains the Source data.
    
    if(!Row.FirstName_IsNull)
    {
     existingContact["new_firstname"]=Row.FirstName;
    }
    //same as above I am assigning all the other fields
    _orgService.Update(existingContact);

    That's all I am doing and it is asking me to specify Entity Id value. When I debug, exisitngContact has EntityId as all zeros. How do I bring the Entity Id and get rid of the error? Someone please explain how this works.

    Thanks for any help.


    • Edited by ReignFan Friday, April 17, 2015 2:50 PM
    Friday, April 17, 2015 2:49 PM

Answers

  • you need to know the primary Guid of the contact in order to update, you can't update the record knowing only a field value.

    So what you need to do is to retrieve the record first using a QueryExpression like:

    QueryExpression query = new QueryExpression();

    query.EntityName = "new_lap";

    query.ColumnSet = new ColumnSet(true);

    query.Criteria.AddCondition("new_interpretercontactid", ConditionOperator.Equal, Row.newAutoContactID);

    EntityCollection result = _orgService.RetrieveMultiple(query);

    if (result.Entities.Count == 1) {

    // now update the record

     Entity existingContact=new Entity("new_lap");

    existingContact.Id = result.Entities[0].Id;

    if(!Row.FirstName_IsNull)
    {
     existingContact["new_firstname"]=Row.FirstName;
    }
    //same as above I am assigning all the other fields
    _orgService.Update(existingContact);

    }



    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by ReignFan Wednesday, April 22, 2015 3:36 PM
    Friday, April 17, 2015 3:07 PM

All replies

  • you need to know the primary Guid of the contact in order to update, you can't update the record knowing only a field value.

    So what you need to do is to retrieve the record first using a QueryExpression like:

    QueryExpression query = new QueryExpression();

    query.EntityName = "new_lap";

    query.ColumnSet = new ColumnSet(true);

    query.Criteria.AddCondition("new_interpretercontactid", ConditionOperator.Equal, Row.newAutoContactID);

    EntityCollection result = _orgService.RetrieveMultiple(query);

    if (result.Entities.Count == 1) {

    // now update the record

     Entity existingContact=new Entity("new_lap");

    existingContact.Id = result.Entities[0].Id;

    if(!Row.FirstName_IsNull)
    {
     existingContact["new_firstname"]=Row.FirstName;
    }
    //same as above I am assigning all the other fields
    _orgService.Update(existingContact);

    }



    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by ReignFan Wednesday, April 22, 2015 3:36 PM
    Friday, April 17, 2015 3:07 PM
  • Thanks Guido. I tried using queryexpression and update the records but it was throwing this error: "Given key was not present"

    RetrieveMultiple returns only non null values right? In my case I have to return both null and non null field values. How do I check for not null values and retrieve them as well?

    Thank you.

    Friday, April 17, 2015 3:13 PM
  • You specify the columns you want to retrieve in your QueryExpression. If they contain null values, it will show.

    In Guidos example he sets query.ColumnSet = new ColumnSet(true);. This will retrieve all columns for the entity.


    Monday, April 20, 2015 8:30 AM
  • Hi,

    If you don't have the data in CRM there's a "null" which is not returned and can be set so you don't need the data, you do need to know which fields are available.

    How does your code look now and which line is giving the key is not present error?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Tuesday, April 21, 2015 7:06 AM