Wikis - Page

How to use C# scripting to modify certain array item

0 Likes

Using COPY FROM function modifies all array items. If you want to modify only a specific one, you need to use scripting. The approach shown below is to traverse the array and once correct item is identified, modify it.

Original response:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <memberSearchResponse xmlns="http://hp.com/SOAQ/ServiceSimulation/2010/demo/01">
      <memberSearchResult>
        <Member xmlns="http://schemas.datacontract.org/2004/07/HP.SOAQ.ServiceSimulation.Demo">
          <householdId>0</householdId>
          <memberId>1</memberId>
          <socialSecurityNumber>554-98-0001</socialSecurityNumber>
        </Member>
        <Member xmlns="http://schemas.datacontract.org/2004/07/HP.SOAQ.ServiceSimulation.Demo">
          <householdId>0</householdId>
          <memberId>11</memberId>
          <socialSecurityNumber>554-98-0011</socialSecurityNumber>
        </Member>
      </memberSearchResult>
    </memberSearchResponse>
  </s:Body>
</s:Envelope>

Code, which changes householdId of a Member with socialSecurityNumber 554-98-0011. Note that it does not matter on which array index the member is. The code is using socialSecurityNumber to find correct array item.

using HP.SV.CSharp;
using System.Collections.Generic;

namespace HP.SV {
    public class CSharpRule {
        public static void Execute(HpsvRootObject sv) {

            // get the array of members - note the type will be generic "object" type, so you'll need to cast it to a proper one later
            IEnumerable<object> memberArray = sv.Response.memberSearchResponse.memberSearchResult.Member;

            // traverse the array
            foreach (object memberObject in memberArray) {
                // cast the generic object to correct array item type. You can get correct type by checking what type indexer returns using Visual Studio Community
                Member1 member = memberObject as Member1;
                if (member != null && member.socialSecurityNumber == "554-98-0011") {
                    // we have a member with socialSecurityNumber we want, so let's modify householdId
                    member.householdId = 42;
                }
            }
        }
    }
}

The result:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <memberSearchResponse xmlns="http://hp.com/SOAQ/ServiceSimulation/2010/demo/01">
      <memberSearchResult>
        <Member xmlns="http://schemas.datacontract.org/2004/07/HP.SOAQ.ServiceSimulation.Demo">
          <householdId>0</householdId>
          <memberId>1</memberId>
          <socialSecurityNumber>554-98-0001</socialSecurityNumber>
        </Member>
        <Member xmlns="http://schemas.datacontract.org/2004/07/HP.SOAQ.ServiceSimulation.Demo">
          <householdId>42</householdId>
          <memberId>11</memberId>
          <socialSecurityNumber>554-98-0011</socialSecurityNumber>
        </Member>
      </memberSearchResult>
    </memberSearchResponse>
  </s:Body>
</s:Envelope>

Labels:

How To-Best Practice
Support Tips/Knowledge Docs
Comment List
Related
Recommended