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

ProcessMatters
Giga Guru

You have this line:

var ref = ArrUtil.indexOf(result,"Contact Ref.");

Which is probably causing it. use ArrayUtil.indexOf instead.

Thanks @EYDavid for your prompt reply.

I have tried to use 'ArrayUtil.IndexOf', I receive the same error.

I don't think I can use ArrayUtil in a Client Script.

Right, but we don't need to, as 

var ref = result.indexOf('Contact Ref.');

should do the same thing.

When I replace the code as suggested, I get the error:

onChange script error: TypeError: result.indexOf is not a function function () { [native code] }

 

Here is the revised code

var ref = result.indexOf("Contact Ref.");
g_form.setValue("u_contact_ref", ref);