Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Trying to create ExecuteMultipleResponse in unit test but Responses is null

(0) ShareShare
ReportReport
Posted on by 70

I am creating a unit test for some older code and need to instantiate and return the ExecuteMultipleResponse object. While I'm able to get the object created, the internal Responses is null and I can't return any failure info for the request. How can I properly create ExecuteMultipleResponse?

*This post is locked for comments

  • CU02060244-0 Profile Picture
    CU02060244-0 2 on at
    Trying to create ExecuteMultipleResponse in unit test but Responses is null
    To create an ExecuteMultipleResponse in a unit test, you need to initialize and assign a value to the Responses property. Here are a few ways you can do this:
    Create a list of responses with rice purity test (List) and assign it to the Responses property of the ExecuteMultipleResponse.
    Create an array of ExecuteResponse objects and assign it to the Responses property.
    Initialize the Responses with one or more ExecuteResponse instances.
    Example:

    var responses = new List<ExecuteResponse>();
    // Add the ExecuteResponse instances to the responses list
    var executeMultipleResponse = new ExecuteMultipleResponse
    {
        Responses = responses
    };
    By doing this, you can then inspect and handle any error information within the ExecuteMultipleResponse. 
  • Verified answer
    TrevorK Profile Picture
    TrevorK 70 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    figured this out and updating to share the solution. The Responses property is just looking at Results for the key "Responses".

    From Microsoft.Xrm.Sdk.Messages -

                   if (!base.Results.Contains("Responses"))

                   {

                       return null;

                   }

                   return (ExecuteMultipleResponseItemCollection)base.Results["Responses"]

    So to make this work, you only need to create the response collection (ExecuteMultipleResponseItemCollection) and then add it to the Results with the key "Responses"

     ExecuteMultipleResponse response = new ExecuteMultipleResponse();

     ExecuteMultipleResponseItemCollection responseItemCollection =

       new ExecuteMultipleResponseItemCollection();

     response.Results.Add("Responses", responseItemCollection);

  • TrevorK Profile Picture
    TrevorK 70 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    Thanks Scott. I'm using VS Fakes and I'm trying to do this from my ExecuteOrganizationRequest method within the fake. I check if the request is an ExecuteMultipleRequest and then look at the contents of the requests in the Requests collection. Now I'm trying to create the Responses collection but that doesn't have a setter. I tried to just add to the Responses collection (Responses.Add()) but since the collection is null, that doesn't work. Seems when I call new on ExecuteMultipleResponse, it doesn't create the full object.

    Below is the concept. Here I was calling clear instead (just in case I was adding the wrong type and causing an issue that way). Since Responses is null, still won't work.

                   if (r is ExecuteMultipleRequest)

                   {

                       ExecuteMultipleResponse response = new ExecuteMultipleResponse();

                       response.Responses.Clear();

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 19 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    You will need to use a mocking framework such as Rhino Mocks or Fakes in Visual studio.

    See www.develop1.net/.../Unit-Testing-Dynamics-CRM2011-Pipeline-Plugins-using-Rhino-Mocks.aspx

    Hope this helps,

    Scott

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans