Parse JSON Object from Properties.

veena_kvkk88
Mega Guru

Hi all,

This might be straight forward but I'm not an expert in dealing with JSON objects, so bear with me!!

There's a UI action on a table, which when clicked, should make certain fields mandatory. This is supposed to be working on multiple tables with different fields being made mandatory. but ultimate result being the same.

So this is what we came up with: Store all the fields that should be made mandatory for each table in a system property like this:

{

      "tablename1": ["field1", "field2", "field3", "field4"],

      "tablename2": ["field1", "field2"]  

}

Next, in a Script Include, we call the property (using gs.getProperty(propertyname)), get the value corresponding to the tablename and return all the corresponding values for the given key(table)

Now, in a UI action (in the client function), we call the Script Include using GlideAjax, which retrieves all the field names in the form of an Array and then make each of them mandatory using g_form.setMandatory().

All this sounds good, but I'm getting stuck in the Script Include. I'm not sure how to get the values corresponding to a particular key..which functions to use etc. Hope I was clear enough! Any help is greatly appreciated!

ctomasi, abhinay, pradeepksharma, any thoughts? Thanks in advance!!

1 ACCEPTED SOLUTION

veena_kvkk88
Mega Guru

Hi Abhinay!



Sorry for the late update! The script with the forEach function did not work, but the one I had before did, with a minor change.



I was trying to get the value from the property to a specific key (passed through a variable) the same way we call it using a direct string.



Example (with actual string as key): fields.u_table_name


Example (with variable key): fields[tablename]



That did the trick!



Script Include:



var MandatoryFields = Class.create();


MandatoryFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getMandatoryFields: function(){


  var tablename = this.getParameter('sysparm_table');


  var fields, fieldArray = new ArrayUtil();


  var json = new JSONParser();


  fields = json.parse(gs.getProperty("all.required.fields"));


  return fields[tablename].toString();


  },



  type: 'MandatoryFields'


});



UI Action:



function setSpecReady(){


  var ga = new GlideAjax('MandatoryFields');


  ga.addParam('sysparm_name', 'getMandatoryFields');


  ga.addParam('sysparm_table', g_form.getTableName().toString());


  ga.getXML(funcParse);


  var reqFields;



  function funcParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  reqFields = answer.split(',');


  for(var i=0; i<reqFields.length; i++){


  g_form.setMandatory(reqFields[i], true);


  }



  //Call the UI Action and skip the 'onclick' function


  gsftSubmit(null, g_form.getFormElement(), 'ready');


  }


}



I appreciate your help! Thank you so much


View solution in original post

19 REPLIES 19

How does your json look in the system property? Post that one too


On Fri, Mar 10, 2017 at 12:50 veena.kvkk88 <


Type : String



Value:



{


      "u_job_request": ["u_document_id", "u_data", "u_depth", "u_date_field", "u_field_name",


                                                                                                                  "u_revisions", "u_comments",


                                                                                                                    "u_input_type", "u_output_type", "u_hidden_text",


                                                                                                                    "u_print_info", "u_encoding", "u_key"],




      "u_job_relation": ["u_record_count", "u_file_count"],  




      "u_child_request": [ "u_word_depth", "u_field_name", "u_hidden_text", "u_revisions"]



}




u_job_request, u_job_relation and u_child_request being the table names and the ones on the right being the field names.


It is working as expected. I have tested with a system property and same contents. Can you make sure you are getting correct table name in line 7. If you can replicate this on your personal developer instance, I can look in to it.


Abhinay Erra
Giga Sage

veena.kvkk88



Did it work? If so, close the thread by marking it as correct


veena_kvkk88
Mega Guru

Hi Abhinay!



Sorry for the late update! The script with the forEach function did not work, but the one I had before did, with a minor change.



I was trying to get the value from the property to a specific key (passed through a variable) the same way we call it using a direct string.



Example (with actual string as key): fields.u_table_name


Example (with variable key): fields[tablename]



That did the trick!



Script Include:



var MandatoryFields = Class.create();


MandatoryFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getMandatoryFields: function(){


  var tablename = this.getParameter('sysparm_table');


  var fields, fieldArray = new ArrayUtil();


  var json = new JSONParser();


  fields = json.parse(gs.getProperty("all.required.fields"));


  return fields[tablename].toString();


  },



  type: 'MandatoryFields'


});



UI Action:



function setSpecReady(){


  var ga = new GlideAjax('MandatoryFields');


  ga.addParam('sysparm_name', 'getMandatoryFields');


  ga.addParam('sysparm_table', g_form.getTableName().toString());


  ga.getXML(funcParse);


  var reqFields;



  function funcParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  reqFields = answer.split(',');


  for(var i=0; i<reqFields.length; i++){


  g_form.setMandatory(reqFields[i], true);


  }



  //Call the UI Action and skip the 'onclick' function


  gsftSubmit(null, g_form.getFormElement(), 'ready');


  }


}



I appreciate your help! Thank you so much