JSON String (Get value from string using Client Script + Ajax)

errabelly
Giga Contributor

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}]]}]

1 ACCEPTED SOLUTION

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


View solution in original post

9 REPLIES 9

I did one more test, to see what should be returned when I do what Chuck did.   Using the same object that was provided....


                          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}]]}]');


                          return complexArray[0].rows[0][2].value;




returned value is "All"


errabelly
Giga Contributor

Hi Jim,



If angular.forEach don't work on server side. Where should i try the code you wrote?


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


The plnkr where I proofed your data model.   http://plnkr.co/edit/Tv1rmD?p=info     Check out the EventsCtrl.js and find $scope.json.   This is an angularJS testing ground for me so the concepts might be more confusing but you can fork and do previews with auto refresh.


How can we return value only for selected field or label using loop(return complexArray[0].rows[0][2].value;)?

can we do this using Onchange script?