- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2016 04:25 PM
Script Include:
var NewGetRequestedItemVariablesAjaxJSON = Class.create();
NewGetRequestedItemVariablesAjaxJSON.prototype = Object.extendsObject(AbstractAjaxProcessor, {
NewgetUsersValues: function() {
// var NItems = [];
var cat_id = this.getParameter('sysparm_cat_id');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request',cat_id);
gr.query();
while(gr.next()){
gs.log(gr.number);
var array = {};
array = gr.variables.definition_table_cognos_result.toString();
gs.log('array value = ' +array);
}
var json = new JSON();
return json.encode(array);
},
type: 'NewGetRequestedItemVariablesAjaxJSON'
});
Client Script: (OnChange on request field)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('NewGetRequestedItemVariablesAjaxJSON');
ga.addParam('sysparm_name','NewgetUsersValues');
ga.addParam('sysparm_cat_id',g_form.getValue('request'));
ga.getXML(ajaxResponse);
function ajaxResponse(response){
var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
g_form.addInfoMessage(answer);
// for (var i = 0; i < answer.length; i++) {
// var answer = response.responseXML.documentElement.getAttribute('answer');
// answer = answer.evalJSON();
g_form.setValue('subject_area.1.1', answer);
// }
}
}
The return from script include is a JSON String. By using above code, I am able to set value of subject_area.1.1 field with complete JSON String. But I want to set the subject_area.1.1 field with particular values only(which are bold in below json string).
Result JSON String:
array value = [{"hdr":"","forElement":"","preFields":[],"rows":[[{"field":"subject_area","label":"Subject Area","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"IT Services","display":"IT Services","relatedTable":"","disabled":false},{"field":"table","label":"Table","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"ITS Table","display":"ITS Table","relatedTable":"","disabled":false},{"field":"column","label":"Column","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"All","display":"All","relatedTable":"","disabled":false},{"field":"description","label":"Description","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"Get desc of ITS.","display":"Get desc of ITS.","relatedTable":"","disabled":false}]]}]
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2016 09:41 AM
Just replace the angular.forEach with native javascript for...
Like this.
for (var i = 0; i < innerMostArray.length; i++) {
myString = myString + ' || ' + innerMostArray[i].value;
}
Now the control you are setting value to does matter but you have not added information. If you are setting value of a textbox this should work fine but if you are adding options to a select box then you will need to use addOption. You can find how to work with page controls on the wiki... GlideForm (g form) - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2016 04:32 PM
Hi Rohith,
When you decode the JSON, you are left with an object. The trick is going to be how to dig it out of there.
Something like answer[0].rows[0][2].value
I don't think that's exactly it because I'm not certain with all the missing subscript, but you get the idea. In a little more readible format...
[
{
"hdr":"",
"forElement":"",
"preFields":[ ],
"rows":[
[
{
"field":"subject_area",
"label":"Subject Area",
"mandatory":"",
"type":"text",
"giveFocus":"",
"reference":"",
"choiceOptions":null,
"refQual":"",
"onChangeFunc":"",
"cellCSS":"",
"labelCSS":"",
"show":"always",
"imageSrc":"",
"value":"IT Services",
"display":"IT Services",
"relatedTable":"",
"disabled":false
},
{
"field":"table",
"label":"Table",
"mandatory":"",
"type":"text",
"giveFocus":"",
"reference":"",
"choiceOptions":null,
"refQual":"",
"onChangeFunc":"",
"cellCSS":"",
"labelCSS":"",
"show":"always",
"imageSrc":"",
"value":"ITS Table",
"display":"ITS Table",
"relatedTable":"",
"disabled":false
},
{
"field":"column",
"label":"Column",
"mandatory":"",
"type":"text",
"giveFocus":"",
"reference":"",
"choiceOptions":null,
"refQual":"",
"onChangeFunc":"",
"cellCSS":"",
"labelCSS":"",
"show":"always",
"imageSrc":"",
"value":"All",
"display":"All",
"relatedTable":"",
"disabled":false
},
{
"field":"description",
"label":"Description",
"mandatory":"",
"type":"text",
"giveFocus":"",
"reference":"",
"choiceOptions":null,
"refQual":"",
"onChangeFunc":"",
"cellCSS":"",
"labelCSS":"",
"show":"always",
"imageSrc":"",
"value":"Get desc of ITS.",
"display":"Get desc of ITS.",
"relatedTable":"",
"disabled":false
}
]
]
}
]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2016 04:41 PM
Hi ctomasi,
In the above client script, I changed g_form.setValue('subject_area.1.1', answer[0].rows[0][2].value);.
After changing, the subject_area.1.1 is empty.
Do I need to make any other changes in the code??
How decode the JSON string?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2016 04:43 PM
Hi Rohith,
I don't know for certain that's the right element of the JSON object you got back. I took a shot at it and obviously missed. With a little debugging (e.g. alert messages) you should be able to drill around and find out what the exact recipe is, or someone else can look at the listing I posted and tell you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2016 09:34 PM
I've never sent anything this complex to the client, you may want to clean it up on the server first. It appears you are attempting to set 4 different values from 4 different objects in an array of objects to a single value. I can only guess that you want these values concatenated...? I hopped into plnkr.co to play with it a bit and this is what I came out with. (not sure if the angular.forEach will work on the client but I know it won't work on the server. Convert to a classic javascript for if not or if you decide to do this in the SI first. Hope it helps...
- $scope.json = function(){
- var complexArray =
JSON.parse('[{"hdr":"","forElement":"","preFields":[],"rows":[[{"field":"subject_area","label":"Subject
Area","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"IT
Services","display":"IT
Services","relatedTable":"","disabled":false},{"field":"table","label":"Table","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"ITS
Table","display":"ITS
Table","relatedTable":"","disabled":false},{"field":"column","label":"Column","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"All","display":"All","relatedTable":"","disabled":false},{"field":"description","label":"Description","mandatory":"","type":"text","giveFocus":"","reference":"","choiceOptions":null,"refQual":"","onChangeFunc":"","cellCSS":"","labelCSS":"","show":"always","imageSrc":"","value":"Get
desc of ITS.","display":"Get desc of
ITS.","relatedTable":"","disabled":false}]]}]'); - var innerMostArray = complexArray[0].rows[0];
- var myString = "";
- angular.forEach(innerMostArray,function(item){
- myString = myString + ' || ' + item.value;
- });
- return myString;
- };
myString returns " || IT Services || ITS Table || All || Get desc of ITS."