Client Script: Parsing an Array and populating fields

Kevin
Tera Expert

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:

find_real_file.png

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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:

find_real_file.png

View solution in original post

13 REPLIES 13

Correct, Client script. Any suggestions of the code in a client script that that return a named value of array?

 

Hitoshi Ozawa
Giga Sage
Giga Sage

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:

find_real_file.png

Your solution worked! My lack of programming experience was the problem.

g_form.setValue("u_contact_ref", result['Contact Ref.']);

Thank you! 

DrewW
Mega Sage
Mega Sage

This is how I would go about it because its easier to update for new key/value pairs.  Please note that this is setup to run as a background script for testing.  You will have to mode it for what you need.

var ToParse = "Field 1\tField 2\tField 3\tField4\tField 5\tField6\tField7\tField8\tField9\tField10\tField11\tField12\tField13\tField14\nValue1\tValue2\tValue3\tValue4\tValue5\tValue6\tValue7\tValue6\tValue9\tValue10\tValue11\tValue12\tValue13\tValue 14"
var fieldMap = {
    "Field 1": "Form Field Name 1",
    "Field 2": "Form Field Name 2",
    "Field 3": "Form Field Name 3",
    "Field4": "Form Field Name 4",
    "Field 5": "Form Field Name 5",
    "Field6": "Form Field Name 6",
    "Field7": "Form Field Name 7",
    "Field8": "Form Field Name 8",
    "Field9": "Form Field Name 9",
    "Field10": "Form Field Name 10",
    "Field11": "Form Field Name 11",
    "Field12": "Form Field Name 12",
    "Field13": "Form Field Name 13",
    "Field14": "Form Field Name 14"
}
gs.print(ToParse);
if(!ToParse)
    throw "No data Error"
var ParsedPara = ToParse.split("\n");
//Cannot run the code past this point if length is not 2, if its more than 2 then there is a format issue.
if(ParsedPara.length != 2)
    throw "Incorrect Format Error"
var PKey = ParsedPara[0].split("\t");
//There are no keys so don't continue.
if(PKey.length < 1)
    throw "Incorrect Format Error"
var PValues = ParsedPara[1].split("\t");
//There are no values or the number of values does not match the keys so there is a format issue.
if(PValues.length < 1 || PKey.length != PValues.length)
    throw "Incorrect Format Error"
gs.print(PKey); // For testing
gs.print(PValues); // For testing
PKey.forEach(function(data, index){
    //g_form.setValue(fieldMap[data], PValues[index]); // This is for the client side
    gs.print(fieldMap[data] + " : " + PValues[index]);  // This is just for testing.
});