- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 02:32 PM
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!!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2017 10:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 07:19 PM
Its not working abhinay Please see my last reply.. that code is working partially..i am able to get the values but only if i use the actual key..not a variable.. Is there a function that could take in the key and get me the values?
Its not working if Line 10 is:
fields = json.parse(gs.getProperty("all.required.fields")).tablename; //variable
only if its:
fields = json.parse(gs.getProperty("all.required.fields")).u_table_name; //actual table name i.e. key

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 07:23 PM
Use the code I have given not the previous one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 10:35 AM
I did Abhinay, but that returned nothing. Just null.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 10:39 AM
I have tried on my end. It works.Post the script you are using
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 10:50 AM
Script Include:
var MandatoryFields = Class.create();
MandatoryFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getMandatoryFields: function(){
gs.log('Script Include function called');
var tablename = this.getParameter('sysparm_table');
gs.log('tablename: ' + tablename);
var fields=[];
var obj=JSON.parse(gs.getProperty("all.required.fields"));
Object.getOwnPropertyNames(obj).forEach(
function (val, idx, array) {
if(val==tablename){
fields=obj[val];
gs.log('obj[val]: ' + fields);
}
}
);
gs.log('fields before normalized: ' + fields);
return fields.join();
},
type: 'MandatoryFields'
});
UI ACtion:
function setSpecReady(){
alert('UI action client func called');
var ga = new GlideAjax('MandatoryFields');
ga.addParam('sysparm_name', 'getMandatoryFields');
ga.addParam('sysparm_table', g_form.getTableName());
ga.getXML(funcParse);
function funcParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
The last alert (line 11) in UI action shows null.
In Script Include, line 5 and line 5 logs are getting printed but nothing else. Have logs in lines 14 and 18.