Working with MultiSelect Lists

Working with MultiSelect Lists

For this example we use SugarCRM, where MultiSelect fields are supplied as a JSON string, i.e. [{"value1","value2"}

First, I check to make sure my origin value ('reg') and existing value in Sugar ('existing') are not empty, as either case is easy to resolve.

 if (string.IsNullOrEmpty(reg))
 {
  //Starfish.LogMessage("reg is empty");
  result = existing;
 }
 else if (string.IsNullOrEmpty(existing))
 {
  //Starfish.LogMessage("existing is empty");
  result = "[\""+reg+"\"]";
 }

Next, I check to see if the origin value is already selected in the existing Sugar value.

 else if (existing.Contains(reg))
 {
  //Starfish.LogMessage("existing already contains reg");
  result = existing;
 }

Now that all of the easy cases are out of the way, I deal with the case where I need to add the origin value to the existing Sugar value. In order for the data to be consistent for reporting, after adding the value, I convert the JSON string to an array, sort it, then convert it back to JSON format.

 else
 {
  //Starfish.LogMessage("Need to add reg to existing value");
  existing = existing.Replace(System.Environment.NewLine, "");
  existing = existing.Replace(" ","");
  string combined = existing.Replace("]","")+",\""+reg+"\"";
  combined = combined.Replace("[","");
  string[] combinedArray = combined.Split(',');
  Array.Sort(combinedArray);
  
  
  for (int j = 0; j <= combinedArray.GetUpperBound(0); j++)
  {
   //Starfish.LogMessage(combinedArray[j]+" ");
   result = result + combinedArray[j] + ",";

  }
  result = result.TrimEnd(',');
  result = "["+result+"]";
 }

    • Related Articles

    • Working with Cross References (Xref files)

      Working with Cross References (Xref files) Xref files servermany purposes. The most common case is as foreign key lookups. Another common case is to use Xrefs as pick list lookups. Tables can be created manually. They can be created by importing the ...
    • Working with Variables

      Variables can be used to store configurable information used by jobs and scripts. In the example below, several Variables have been defined. cc Use the following steps to walk through Viewing variables Selecting and changing a variable Navigate to ...
    • Look in many xref lists for an origin ID

      Function ScriptedField Dim newid Dim oldid oldid = "@@ORG:OLDID@@" newid = XrefRead("Accounts",oldid) If Len(newid) = 0 Then newid = XrefRead("Contacts",oldid) End If If Len(newid) = 0 Then newid = XrefRead("Opportunities",oldid) End If If Len(newid) ...
    • Active Directory Connector

      Origin There may be some trial and error with capitalization required to get this Origin working. For example, ldap://ad.xyz.com vs LDAP://ad.xyz.com or LDAP://ad.xyz.com vs LDAP://ad.XYZ.com. Parameter Description Access Key Connection String Active ...
    • Previewing Jobs

      Before running a job for the first time, you can check to make sure all your formulas and translations are working correctly by stepping through each row on the Preview tab. No data will be modified in your destination system, and you may click Log ...