Parse Name Value Pairs

Sam Santos
Tera Contributor

When you parse Name Value Pairs, does it come out as a String?

 

sys_created_on: string = sys_created_on
sys_created_by: string = sys_created_by
parent: string = parent
parent.u_ci_unique_id: string = parent.u_ci_unique_id
parent.sys_class_name: string = parent.sys_class_name
u_number_of_relationships: string = u_number_of_relationships
type: string = type
child: string = child
child.u_ci_unique_id: string = child.u_ci_unique_id
child.sys_class_name: string = child.sys_class_name

 

I think parent.u_ci_unique_id is treated as a literal string which is failing dotwalking.

1 ACCEPTED SOLUTION

Perhaps another split and for loop will help you in that case.
For example I've simply added a "fieldList" variable that hold a comma separated list of variables and I've then turned it into an array with split(",")
Then inside the for loop for it we just need to add the previous code and change fieldNames to equal fieldList[x] so that it would basically work exactly like before.

var gr = new GlideRecord('incident');
gr.get("27a3cd6421f411004f8b9558edc753a0");
var fieldList = "assigned_to.name,assigned_to.location".split(",");
for(var x in fieldList){

	var fieldNames = fieldList[x];
	fieldNames = fieldNames.split(".");
	var value = gr;

	for (var i in fieldNames){
		var fieldName = fieldNames[i];
		if(value.getElement(fieldName) == 'reference'){
			var refValue = value.getRefRecord(fieldName);
			if(refValue){
				value = refValue;
			}else{
				value = value[fieldName];
			}
		}else{
			value = value[fieldName];
		}

	}

	gs.info(value);

}

 

View solution in original post

5 REPLIES 5

Weird
Mega Sage

Could you clarify in what scenario. There's no context so it's hard to give an answer, but generally no, if the value is not a string.

You can create a JSON object with the values also being objects.
For example "parent":current.parent would contain the current records parent object.
You could then log <jsonObj>.parent.number and you'd get the parent records number.
It would still be considered an object though and you'd have to add .toString() to have a string instead.

In your case parent.u_ci_unique_id works if parent is a reference object that you have access to.
For example a gliderecord object.

Sam Santos
Tera Contributor

Hi Joni,

 

Thank you for your response. Basically I have a GET API which iterates through a table for the column names. These column names work fine when they are not dotwalking. I have attached my code below. The columns that are dotwalking shows null values however I verified that the column names are correct. I think its something how NPV is parsed as a string when calling keys. Any ideas?

 

2023-03-28_12-50-39.png2023-03-28_12-51-12.png2023-03-28_13-15-12.png

Ah, I see.
I think the issue is that you're saying gr[parent.u_ci_unique_id] instead of gr[parent][u_ci_unique_id].
You could try to do something like this to get it working:

 

var gr = new GlideRecord('incident');
gr.get("27a3cd6421f411004f8b9558edc753a0");
var fieldNames = "assigned_to.name";
fieldNames = fieldNames.split(".");
var value = gr;

for (var i in fieldNames){
var fieldName = fieldNames[i];
if(value.getElement(fieldName) == 'reference'){
var refValue = value.getRefRecord(fieldName);
if(refValue){
value = refValue;
}else{
value = value[fieldName];
}
}else{
value = value[fieldName];
}

}

gs.info(value);


This is an example where I've defined the field path as assigned_to and their name.
I split it based on "." and then loop through the elements to build up the value.
Eventually I'll get the correct value.

 



I did it quite quickly so it might be a bit messed up, but it should give an idea of what to do.

Sam Santos
Tera Contributor

Thank you so much Joni! Just another question though. What if the fieldNames on your code come in with multiple values? (e.g. var fieldNames = "assigned_to, assigned_to.name, assigned_to.location";)