How to remove the Spaces between words , and need to get and copy in another fields

sasidhar3
Kilo Contributor

Hi ,

i have a requirement in which i need to get the values from response and paste it in corresponding fields

here is the response

[{"index":3159,"Problem ID":"IM1202872","Resolution":"12\/14\/2016 17:25:15 (KISHORE BUSI): APPLY RULE 15, FILE REPLAYED PER EB.","Original Description":"The following File Gateway routing event occurred Route Event Details: ============================================================================== Event Code: FG_0455

in this response i am able to get and paste it in corresponding fields except Problem Id as it is having space between Problem and ID , could any one please help me in this

Here is the code

var gr = new GlideRecord('incident');

gr.addActiveQuery();

gr.addQuery('sys_id',sysid);

gr.query();

if(gr.next())

{

gr.description = responseBody;//--------> this is the Response which i get as shown above

var testJSON = JSON.parse(responseBody);gs.addInfoMessage("Response is directly saved in Description Field " + gr.number);

gs.addInfoMessage(responseBody);

gs.addInfoMessage("testJSON.index :"+testJSON[0].Resolution);/// i am getting this values

//gr.u_test = testJSON[0].index;

Object.keys(testJSON).forEach(function(key) {

var replaced = key.replace(' ', '');

if (key !== replaced) {

testJSON[replaced] = testJSON[key];

delete testJSON[key];

}

});

//console.log(testJSON);

gr.u_resolution = testJSON[0].Resolution;

gs.addInfoMessage(testJSON[0].ProblemID);

gr.update();

Thanks in advance

1 ACCEPTED SOLUTION

Hi Sasidhar,



You are unable to get the values because of space. I checked at my side also so it was not retrieving.


Here is the updated code which retrieves all the values.



when trying to parse the space causes an issue. so i have declared a variable to hold that key and given it while parsing.


you can then pick individual variables and assign it in GlideRecord object.



Script:



var str = '[{"index":3159,"Problem ID":"IM1202872","Resolution":"12/14/2016 17:25:15 (KISHORE BUSI): APPLY RULE 15, FILE REPLAYED PER EB.","Original Description":"The following File Gateway routing event occurred Route Event Details: ============================================================================== Event Code: FG_0455"}]';



var parser = new JSONParser();


var parsedData = parser.parse(str);



var problemId = 'Problem ID';


var description = 'Original Description';



gs.print('Index is:'+parsedData[0].index);


gs.print('Problem ID is:'+parsedData[0][problemId]);


gs.print('Resolution is:'+parsedData[0].Resolution);


gs.print('Original Description is:'+parsedData[0][description]);



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Shishir Srivast
Mega Sage

There is space in Original Description as well, is that field works?


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sasidhar,



Are you able to populate value from "Original Description" key as well since it also has space in between those 2 words.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for the Response ,



Sorry for not mentioning it no i can not get that Original description Also


Hi Sasidhar,



You are unable to get the values because of space. I checked at my side also so it was not retrieving.


Here is the updated code which retrieves all the values.



when trying to parse the space causes an issue. so i have declared a variable to hold that key and given it while parsing.


you can then pick individual variables and assign it in GlideRecord object.



Script:



var str = '[{"index":3159,"Problem ID":"IM1202872","Resolution":"12/14/2016 17:25:15 (KISHORE BUSI): APPLY RULE 15, FILE REPLAYED PER EB.","Original Description":"The following File Gateway routing event occurred Route Event Details: ============================================================================== Event Code: FG_0455"}]';



var parser = new JSONParser();


var parsedData = parser.parse(str);



var problemId = 'Problem ID';


var description = 'Original Description';



gs.print('Index is:'+parsedData[0].index);


gs.print('Problem ID is:'+parsedData[0][problemId]);


gs.print('Resolution is:'+parsedData[0].Resolution);


gs.print('Original Description is:'+parsedData[0][description]);



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader