- 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 03:56 PM
I haven't properly written the UI action code yet. All i'm trying to do for now is call the Script Include and get the fields.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 04:00 PM
I am not infront of my computer right now. Will gwt back to you with the
correct code shortly. But I see few errors. You are using Asynchronous
glide ajax. You should not use getAnswer(). See the documentation for
glideajax.
On Wed, Mar 8, 2017 at 17:56 veena.kvkk88 <community-no-reply@servicenow.com>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 04:03 PM
Sure! Thank you so much. And that function was written before I made a lot of changes. However, the log messages are in the Script Include. So once it's called, they all should be printed and the issue should be when it comes back to the UI action right? But the Script include itself is being interrupted.
Anyway, I'll make the changes to the Glideajax meanwhile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 04:27 PM
OMG its partially working
This is the script in 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, fieldArray = new ArrayUtil();
var json = new JSONParser();
fields = json.parse(gs.getProperty("lhe.required.fields")).u_table_name; //Here, I used actual table name, instead of the variable
gs.log('fields before normalized: ' + fields);
fields = fieldArray.ensureArray(fields);
for(i=0; i<fields.length; i++){
gs.log('Mandatory fields:' + fields[i]);
}
return fields;
},
type: 'MandatoryFields'
});
Only problem is, the parse() function (line 10) is only working when I use it with the actual table name. But the whole point of this is to make it dynamic. the 'tablename' variable from line 6 is not working. is there a way around this? If not, this is kinda pointless.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 05:57 PM
Here is your 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('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);
}
}