
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 01:56 PM
I have a two line string, tab separated copied from another system (not ServiceNow). This string is pasted into a field in SN, and I am trying to get the trying parsed into Key / Value pairs and the values populated into Fields in the Case form.
ISSUE: The source system, sometimes changes the order / number of fields. I needs to be able to refer to the array elements by name, not by number.
The script below fails with the error:
onChange script error: ReferenceError: ArrayUtil is not defined function () { [native code] }
Any suggestions are appreciated.
Here is Sample Data:
Field1 Field2 Field3 Field4 Field5 Field6 Field7 Field8 Field9 Field10 Field11 Field12 Field13 Field14
Value1 Value2 Value3 Value4 Value5 Value6 Value7 Value6 Value9 Value10 Value11 Value12 Value13 Value 14
Here is the Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ToParse = g_form.getValue("u_info"); //Get the pasted data
var ParsedPara = ToParse.split("\n"); //Create an Array of the two lines
var PKey = ParsedPara[0].split("\t"); //Create an Array of the Keys (field names)
var PValues = ParsedPara[1].split("\t"); //Create an Array of the Values (field values)
var result = PValues.reduce(function(result, field, index) { //reduce the two arrays into one
result[PKey[index]] = field;
return result;
}, {});
var Resultarr = JSON.stringify(result); //Convert the resulting Object to an array
//alert('Result: ' + JSON.stringify(result));
//alert('Result: ' + JSON.stringify(Resultarr));
var ref = ArrUtil.indexOf(result,"Contact Ref.");
g_form.setValue("u_contact_ref", ref);
// g_form.setValue("u_contact_name", ArrayUtil.indexOf(result,"Contact Name",0));
// g_form.setValue("u_contact_segment", ArrayUtil.indexOf(Resultarr,"Contact Segment",0));
// g_form.setValue("u_contact_person", ArrayUtil.indexOf(Resultarr,"Contact Perrson",0));
// g_form.setValue("u_address_line_1", ArrayUtil.indexOf(Resultarr,"Address Line 1",0));
// g_form.setValue("u_postal_code", ArrayUtil.indexOf(Resultarr,"Postal Code",0));
// g_form.setValue("u_producer_office", ArrayUtil.indexOf(Resultarr,"Producer Office",0));
// g_form.setValue("u_csr", ArrayUtil.indexOf(Resultarr,"Customer Service Representative",0));
}
Here is the Case form:
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 04:54 PM
Not sure of the reason to Stringify.
1. Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
//Type appropriate comment here, and begin script below
var ToParse = g_form.getValue("u_info"); //Get the pasted data
var ParsedPara = ToParse.split("\n"); //Create an Array of the two lines
var PKey = ParsedPara[0].split("\t"); //Create an Array of the Keys (field names)
var PValues = ParsedPara[1].split("\t"); //Create an Array of the Values (field values)
var result = PValues.reduce(function(result, field, index) { //reduce the two arrays into one
result[PKey[index]] = field;
return result;
}, {});
g_form.setValue("u_contact_ref", result['Contact Ref.']);
g_form.setValue("u_contact_name", result['Contact Name']);
g_form.setValue("u_contact_segment", result['Contact Segment']);
g_form.setValue("u_contact_person", result['Contact Person']);
// g_form.setValue("u_address_line_1", ArrayUtil.indexOf(Resultarr,"Address Line 1",0));
// g_form.setValue("u_postal_code", ArrayUtil.indexOf(Resultarr,"Postal Code",0));
// g_form.setValue("u_producer_office", ArrayUtil.indexOf(Resultarr,"Producer Office",0));
// g_form.setValue("u_csr", ArrayUtil.indexOf(Resultarr,"Customer Service Representative",0));
// g_form.setValue("u_contact_name", ArrayUtil.indexOf(result,"Contact Name",0));
// g_form.setValue("u_contact_segment", ArrayUtil.indexOf(Resultarr,"Contact Segment",0));
// g_form.setValue("u_contact_person", ArrayUtil.indexOf(Resultarr,"Contact Perrson",0));
// g_form.setValue("u_address_line_1", ArrayUtil.indexOf(Resultarr,"Address Line 1",0));
// g_form.setValue("u_postal_code", ArrayUtil.indexOf(Resultarr,"Postal Code",0));
// g_form.setValue("u_producer_office", ArrayUtil.indexOf(Resultarr,"Producer Office",0));
// g_form.setValue("u_csr", ArrayUtil.indexOf(Resultarr,"Customer Service Representative",0));
} catch (e) {
alert(e.message());
}
}
2. Input Data:(tab delimited)
Contact Ref. Contact Name Contact Segment Contact Person Field5 Field6 Field7 Field8 Field9 Field10 Field11 Field12 Field13 Field14
Value1 Value2 Value3 Value4 Value5 Value6 Value7 Value6 Value9 Value10 Value11 Value12 Value13 Value14
3. Execution result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 02:46 PM
Right, because result is your Object, and ResultArr is the actual array.
var ref = ResultArr.indexOf('Contact Ref.');
Just as an aside, variables shouldn't start with a capital letter, as that is reserved for Classes in javascript. Sorry. Pet peeve.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 02:59 PM
Thanks for your help.
The output of ResultArr is:
Result: "{\"Access\":\"Full Access\",\"Contact Ref.\":\"00000000\",\"Contact Name\":\"Clarke, Adam\",\"Contact Segment\":\"Personal\",\"Contact Person\":\"Clarke, Kevin\",\"Address Line 1\":\"#123 AnySt\",\"Postal Code\":\"H0H 0H0\",\"Producer Office\":\"New Westminster\",\"Producer\":\"Account, House\",\"Customer Service Representative\":\"Sally, Sue\",\"Contact Type\":\"Individual\",\"Contact Status\":\"Live\",\"Contact Access\":\"Full Access\",\"On Hold\":\"\"}"
Here is the revised code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ToParse = g_form.getValue("u_info"); //Get the data in Info field
var ParsedPara = ToParse.split("\n"); //Create an Array of the two lines
var PKey = ParsedPara[0].split("\t"); //Create an Array fo the Keys (field names)
var PValues = ParsedPara[1].split("\t"); //Create an Array of the Values (field values)
var result = PValues.reduce(function(result, field, index) { //reduce the two arrays into one
result[PKey[index]] = field;
return result;
}, {});
var Resultarr = JSON.stringify(result); //Convert the resulting Object to an array
//alert('Result: ' + JSON.stringify(result));
alert('Result: ' + JSON.stringify(Resultarr));
g_form.setValue("u_contact_ref", Resultarr.indexOf("Contact Ref."));
g_form.setValue("u_contact_name", Resultarr.indexOf("Contact Name"));
g_form.setValue("u_contact_segment", Resultarr.indexOf("Contact Segment"));
g_form.setValue("u_contact_person", Resultarr.indexOf("Contact Perrson"));
g_form.setValue("u_address_line_1", Resultarr.indexOf("Address Line 1"));
g_form.setValue("u_postal_code", Resultarr.indexOf("Postal Code"));
g_form.setValue("u_producer_office", Resultarr.indexOf("Producer Office"));
g_form.setValue("u_csr", Resultarr.indexOf("Customer Service Representative"));
}
Here is the Form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 03:11 PM
.indexof returns a numeric value.
g_form.setValue("u_contact_ref", Resultarr[Resultarr.indexOf("Contact Ref.")]);
You are plugging the index into the setValue. Use the above code to plug the value at that index.
I am also guessing there is a typo in "Contact Perrson" which is why the index returned as -1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 04:20 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 02:30 PM
Last I checked ArrayUtil was a server side script include only. What you are doing looks like a client script.