locked
CRM late bound update record RRS feed

  • Question

  • Hi,

    I created a webservice that creates opportunities, accounts, ... in CRM.

    But, when a account already exists, i need to update it with the webservice.
    To check if an account exists, I use a QueryExpression.

    QueryExpression nqe = new QueryExpression();
    nqe.EntityName = "account";
    nqe.ColumnSet = new ColumnSet();
    nqe.ColumnSet.Columns.Add("accountid");
    nqe.Criteria = new FilterExpression();
    nqe.Criteria.AddCondition("accountid", ConditionOperator.Equal, AccId);
    EntityCollection nec = service.RetrieveMultiple(nqe);

    But how can I now update the retrieved account?

    If i use " service.update(Opportunity);" , it says it needs an EntityID.
    How can i tell what record it needs to update?

    If i create a record i use following line of code:

    AccId = service.Create(account);

    Thanks

    Thursday, September 3, 2015 9:50 AM

Answers

  • Hi Jochen,

    your code is not correct. When a Retrieve is performed if the id of the record is not valid an exception is thrown, so you can't check if the Id property is an empty guid or not (because you will not arrive at that line and ins case the guid is valid the entity will never have and empty guid after the retrieve.

    Also is not a good practice to retrieve all the columns in order to update just an attribute.

    a more correct code (assuming the user want to check if the accountid is valid before performing the update) will be

    try { Entity existingAccount = service.Retrieve("account", accountid, new ColumnSet(false)); // account exists, update it existingAccount["attributetoupdate"] = "attributevalue"; service.Update(existingAccount); } catch (Exception ex)

    { // account doesn't exist, do other stuff }



    My blog: www.crmanswers.net - CRM Theme Generator

    • Marked as answer by Alexander_DM Thursday, September 3, 2015 12:21 PM
    Thursday, September 3, 2015 10:40 AM
  • To change the owner of a record you need to use the assign request: https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.assignrequest.aspx?f=255&MSPPError=-2147217396 , the owner is a special lookup value as it can be a user or a team.

    // Create the Request Object and Set the Request Object's Properties
    AssignRequest assign = new AssignRequest
        {
            Assignee = new EntityReference("systemuser",
                userid),
            Target = new EntityReference("opportunity",
                opportunityid)
        };
    
    
    // Execute the Request
    _service.Execute(assign);
    Kind Regards

    • Marked as answer by Alexander_DM Thursday, September 3, 2015 2:25 PM
    Thursday, September 3, 2015 1:19 PM

All replies

  • Hi Jochen,

    your code is not correct. When a Retrieve is performed if the id of the record is not valid an exception is thrown, so you can't check if the Id property is an empty guid or not (because you will not arrive at that line and ins case the guid is valid the entity will never have and empty guid after the retrieve.

    Also is not a good practice to retrieve all the columns in order to update just an attribute.

    a more correct code (assuming the user want to check if the accountid is valid before performing the update) will be

    try { Entity existingAccount = service.Retrieve("account", accountid, new ColumnSet(false)); // account exists, update it existingAccount["attributetoupdate"] = "attributevalue"; service.Update(existingAccount); } catch (Exception ex)

    { // account doesn't exist, do other stuff }



    My blog: www.crmanswers.net - CRM Theme Generator

    • Marked as answer by Alexander_DM Thursday, September 3, 2015 12:21 PM
    Thursday, September 3, 2015 10:40 AM
  • I knew something was wrong but I had no time to test it. They should include this in the MSDN section of the Retrieve Method!

    Thanks!

    /J

    Thursday, September 3, 2015 10:47 AM
  • Thanks for the quick reply Guido and Jochen!

    Thursday, September 3, 2015 12:22 PM
  • I have one more question.

    The update statement works with "normal" text fields, but with user lookups nothing happens.

    I use this line of code:

    Entity existingOpportunity = service.Retrieve("opportunity", opportunityId, new ColumnSet(false));
    existingOpportunity["ownerid"] = new EntityReference("systemuser", userid);
    service.Update(existingOpportunity);

    When I insert breakpoints and check the values, UserId and OpportunityId contains the correct values (checked this in the DB). What could be the issue here?

    Thanks.

    Thursday, September 3, 2015 1:07 PM
  • To change the owner of a record you need to use the assign request: https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.assignrequest.aspx?f=255&MSPPError=-2147217396 , the owner is a special lookup value as it can be a user or a team.

    // Create the Request Object and Set the Request Object's Properties
    AssignRequest assign = new AssignRequest
        {
            Assignee = new EntityReference("systemuser",
                userid),
            Target = new EntityReference("opportunity",
                opportunityid)
        };
    
    
    // Execute the Request
    _service.Execute(assign);
    Kind Regards

    • Marked as answer by Alexander_DM Thursday, September 3, 2015 2:25 PM
    Thursday, September 3, 2015 1:19 PM
  • Thank you!! Works perfect!

    Thursday, September 3, 2015 2:26 PM