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

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.

@EYDavid  I'm getting closer - Now the fields now are populated, but not with the Value, instead it seems they are being populated with the Position of the Key in the String. (See the details below)

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

find_real_file.png

.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

@EYDavid When I use the code

g_form.setValue("u_contact_ref", Resultarr[Resultarr.indexOf("Contact Ref.")]);

The fields are populated with the first letter of the 'Key' string. 

find_real_file.png

DrewW
Mega Sage
Mega Sage

Last I checked ArrayUtil was a server side script include only.  What you are doing looks like a client script.