- 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:11 PM
Hi,
first you would parse that propery value like this
var Obj = JSON.parse(gs.getProperty(propertyname));
then access the particular table.
var tablefields = Obj.tablename1;
now you have got the array of fields. continue with your logic.
note: you can also use new JSON.decode() instead of JSON.parse(). later one being native JavaScript method.
(please mark helpful/like/correct if it helps)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 04:16 PM
Hi rushit!
Thanks for the reply.
When i used decode() it returned undefined. and when i used parse() it didn't even go to the next line. Not sure where I'm going wrong.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 03:29 PM
Here you go
Use this in the system property
{"tablename1": ["field1", "field2", "field3", "field4"],"tablename2": ["field1", "field2"] }
In your UI action use this
var fields=JSON.parse(json string goes here).tablename1;
for(i=0; i<fields.length; i++){
g_form.setMandatory(fields[i],true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 03:55 PM
Hi Abhinay!
Thanks for the reply!! Here is the code I'm using in Script Include (since UI action's client side function cant process gs.property())
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();
fields = JSON.parse(gs.getProperty("all.required.fields")).tablename;
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'
});
However, the log with tablename (line 7) is the only one getting printed. the next two messages are not logging at all! Can you see anything wrong with this code? The sysparm_table is being passes from the UI action code.
This is the 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);
var fields = ga.getAnswer();
function funcParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}