locked
How to edit then update an entity after you've searched for it? RRS feed

  • Question

  • Is there some sample code somewhere on how to edit then update an entity after you've searched for it? I keep getting all kinds of error messages, telling me to use the context (which I am) when updating an entity. All the samples I seem to find involve updating an entity straight after you've just created it. I want to update an entity later on, after I've had to go and find it again. Surely this is possible? Here's how I'm finding it:

    string connStr = "Data Source=mydbserver.win;Initial Catalog=myserver;Integrated Security=True;";
    CrmConnection crmConn = CrmConnection.Parse(connStr);
    OrganizationService orgSvc = new OrganizationService(crmConn);
    
    using (OrganizationServiceContext svc = new OrganizationServiceContext(orgSvc))
    {
        var e = svc.CreateQuery("product").First(
            p => (string)p["productnumber"] == "12345"
        );
    
        if (e != null)
        {
        	e["something"] = "whatever";
    
        	svc.UpdateObject(e);
            svc.SaveChanges();
        }
    }

    This results in the following error:

    {"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. The InnerException message was 'Error in line 1 position 10126. Element 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data from a type that maps to the name 'http://schemas.microsoft.com/xrm/7.1/Contracts:ConcurrencyBehavior'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'ConcurrencyBehavior' and namespace 'http://schemas.microsoft.com/xrm/7.1/Contracts'.'.  Please see InnerException for more details."}

    I could go on showing variations I've tried, but that would get us nowhere. 

    Is there an example somewhere showing how to update an entity that hasn't just been created in the same code block?

    Thanks.

    Tuesday, October 6, 2015 10:24 PM

Answers

  • I found a way to update a product after having found its entity. I seem to have to create a new entity and retrieve details into it that I'm interested in, then update that. CRM just looks lovely to me :-) I should write everything it from now on!! (sarcasm):

    string connStr = "Data Source=mydbserver.win;Initial Catalog=myserver;Integrated Security=True;";
    CrmConnection crmConn = CrmConnection.Parse(connStr);
    OrganizationService orgSvc = new OrganizationService(crmConn);
    
    using (OrganizationServiceContext svc = new OrganizationServiceContext(orgSvc))
    {
        var e = svc.CreateQuery("product").First(
            p => (string)p["productnumber"] == "12345"
        );
    
        if (e != null)
        {
    		Entity p = new Entity("product");
    
    		ColumnSet attributes = new ColumnSet(new string[] { 
    		    "whatever", "something_else_perhaps"
    		});
    
    		p["whatever"] = "it works!";
    		p = orgSvc.Retrieve(p.LogicalName, e.Id, attributes);
    
    		orgSvc.Update(p);
        }
    }

    • Marked as answer by mfearby Thursday, October 8, 2015 3:42 AM
    Thursday, October 8, 2015 3:42 AM

All replies

  • I found a way to update a product after having found its entity. I seem to have to create a new entity and retrieve details into it that I'm interested in, then update that. CRM just looks lovely to me :-) I should write everything it from now on!! (sarcasm):

    string connStr = "Data Source=mydbserver.win;Initial Catalog=myserver;Integrated Security=True;";
    CrmConnection crmConn = CrmConnection.Parse(connStr);
    OrganizationService orgSvc = new OrganizationService(crmConn);
    
    using (OrganizationServiceContext svc = new OrganizationServiceContext(orgSvc))
    {
        var e = svc.CreateQuery("product").First(
            p => (string)p["productnumber"] == "12345"
        );
    
        if (e != null)
        {
    		Entity p = new Entity("product");
    
    		ColumnSet attributes = new ColumnSet(new string[] { 
    		    "whatever", "something_else_perhaps"
    		});
    
    		p["whatever"] = "it works!";
    		p = orgSvc.Retrieve(p.LogicalName, e.Id, attributes);
    
    		orgSvc.Update(p);
        }
    }

    • Marked as answer by mfearby Thursday, October 8, 2015 3:42 AM
    Thursday, October 8, 2015 3:42 AM
  • using (OrganizationServiceContext svc = new OrganizationServiceContext(orgSvc)) { var e = svc.CreateQuery("product").First( p => (string)p["productnumber"] == "12345" ); if (e != null) { Entity p = new Entity("product");
    p.Id = e.Id;
    p["whatever"] = "it works!";
    orgSvc.Update(p);

    } }



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

    Thursday, October 8, 2015 3:59 AM