how to traverse through the values of a name-value type field

Divya95
Tera Contributor

Hi ,

We have a name value type field ,which has multiple values as below .As per the requirement ,in a modify request ,we may have to modify either one or two name value pair out of all .

For ex ,we may get request to change the value of Geolocation2 to 120 from 80.

find_real_file.png

Currently ,the code is as below .

var jsonString = request.body.dataString; // Storing the JSON input sent with the request
var parser = new JSONParser();
var parsed = parser.parse(jsonString);

var rel = new GlideRecord ('incident'); 

rel.name_value= JSON.stringify(parsed.name_value);

 

As per the existing code , it expects to send all the available name-value pairs along with modified one .

name-value: {"Geolocation1": "60","Geolocation2": "120","Last Ditch Internet": "NewConnection"}.

But if only modified one is sent , remaining gets deleted and only {"Geolocation2": "120"} remains in the record.

But the expectation is , they will send only name-value:{"Geolocation2": "120"} and the remaining pairs should remain the same .

 

Any suggestions on how can we traverse through these name value pairs.

Thanks.

1 ACCEPTED SOLUTION

ScienceSoft
Tera Guru

Hi Divya,

The following operation

rel.name_value= JSON.stringify(parsed.name_value);

will always replace full name-value list with a new one.

To save non-updated pairs, following script could be applied:

for(var name in rel.name_value) {
   if (parsed.name_value[name]!=null && 
      parsed.name_value[name]!=undefined) {
       rel.name_value[name] = parsed.name_value[name];
   }
}

View solution in original post

2 REPLIES 2

ScienceSoft
Tera Guru

Hi Divya,

The following operation

rel.name_value= JSON.stringify(parsed.name_value);

will always replace full name-value list with a new one.

To save non-updated pairs, following script could be applied:

for(var name in rel.name_value) {
   if (parsed.name_value[name]!=null && 
      parsed.name_value[name]!=undefined) {
       rel.name_value[name] = parsed.name_value[name];
   }
}

Thanks ScienteSoft .This has helped us .

Another query if there is a new name-value received ,how can we update that.

Thanks.