IOrganizationService correct way to update entities
Asked Answered
T

2

1

I'm having a look at the best way of updating/retrieve entities from within C#. I've had a read through the MSDN documentation but unsure which way to go/when to use either method.

So, my question:

Should I be using:

  • IOrganizationService.Update() and update the entity directly; or

  • IOrganization.Execute() and create an update request

    And if the answer is 'it depends', what situation warrants which method?

Thanks

Toffey answered 30/1, 2015 at 22:28 Comment(0)
B
4

First of all both Update and Execute of an UpdateRequest produce the same result.

The main difference is that an UpdateRequest can be batched using the ExecuteMultipleRequest

Bastardize answered 30/1, 2015 at 23:26 Comment(5)
I agree. There are equivalent ways of doing a lot of things with the Execute method that you can do with more specific methods. In order to retrieve records, you might use RetrieveMultiple or execute a RetrieveMultipleRequestProfligate
I would add that your SHOULD use Update not Execute to keep your code cleaner and easier to maintain. Everything can be done via Execute but, as a CRM developer best practice, if something can be done thorough a more narrowly tailored API call it should be done that way because - as I said earlier - it will lead to cleaner and easier to maintain code.Anciently
@Anciently The way I was thinking was that I'll have a ExecuteMultipleRequests which will hold all my creates and updates during a plugin or process so that when I'm done I'll call Execute and only call the web service the once. I'm looking at a sort of IRepository pattern so I can do Create/Update on Entities and then Commit (Execute) when I'm done .Toffey
If you are in a plugin or workflow you will not be calling a web service, unlike client side code. The 'IOrganizationService' will be provided by the platform and can interact directly with the platform since yiur code is running on a CRM server. I'm not sure what performance benefit you would see using it in a plugin or workflow - but I assume it would be minimal. Regardless, you can still use Update with a ExecuteMultipleRequests call.Anciently
@Adringer it sounds like you should be using the early bound Linq context, if you're already operating within a plugin or workflowPsychologize
E
1

With the CreateRequest as well as the UpdateRequest you can switch duplicate detection, as in the following example:

public Guid CreateTest(Entity account, IOrganizationService service)
{
    var request = new CreateRequest { Target = account };
    request.Parameters.Add("SuppressDuplicateDetection", false);
    var response = service.Execute(request) as CreateResponse;
    return response.id;
}

You cannot do this using the Create and Update methods.

And, of course, you can feed Request objects to the ExecuteMultipleRequest to throttle the number of roundtrips to the OrganizationService.

I expect the Create and Update methods to be slightly more efficient, but I doubt if it would be measurable.

Extravagancy answered 2/2, 2015 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.