Max value from array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 03:14 AM
Hi,
I have script to calculate risk form.I have a weights for each field stored in separate table.I have used onSubmit client script and script include.I am able to fetch the values form the table but not sure how to find the max of the value that we are getting.Any alternate solution please do let me know.
server script
Calculate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculate: function(){
var f=this.getParameter('sysparm_field');
gs.log('hello123'+f);
var g=new GlideRecord('u_weights');
g.addQuery('u_name',f);
g.query();
var count= 0;
while(g.next()){
count+=g.u_wieights;
}
return count;
},
Client script
function onSubmit() {
//Type appropriate comment here, and begin script below
//alert(arrFields);
var fields = g_form.getEditableFields();
//alert(fields);
var b ={};
var max =0 ;
//Getting the score of all selected checkbox in section G
for(var i=0;i<fields.length;i++){
var str = fields[i];
if(g_form.getValue(fields[i])=='true' && str.substring(0,3)=='u_g'){
alert('field'+str.substring(0,3));
//alert(fields[i]);
var bs=new GlideAjax('calculate');
bs.addParam('sysparm_name','calculate');
bs.addParam('sysparm_field',fields[i]);
bs.getXML(HelloWorldParse);
}
}
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
b+=answer;
alert(b);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 03:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 03:55 AM
Hi,
I tried below script in my instance and it sowed correct result.
var numbers = [1, 2, 3, 4];
gs.print(Math.max.apply(null, numbers));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 04:14 AM
Hey SG,
var array = [];// push your elements into the array
var largest= 0;
for (i=0; i<=largest;i++){
if (array[i]>largest) {
var largest=array[i];
}
}
alert(largest);
post me your feedback
Please Hit ✅Correct, âÂ��Helpful, or ��Like depending on the impact of the response
Have a lovely day ahead
Regards,
Divya Mishra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 05:07 AM
None of the solution worked.