The Zurich release has arrived! Interested in new features and functionalities? Click here for more

set value is not working on Submit client script

sgp
Kilo Contributor

Hi,

We have client script and script include which I am using to calculate the risk.I getting the value from the weight table but I am unable to set the value to field.it is disappearing .

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 =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=b + parseInt(answer);

alert('now1'+parseInt(answer)+"And Total: "+d);

g_form.setValue('risk',b);

}

1 ACCEPTED SOLUTION

Dubz
Mega Sage

I don't really understand the use case for setting a value of a field on the form based on other fields on the form. A before insert/update business rule will do the same thing.



You can make you glideajax synchronous with getXMLWait() but you make your users hang around while the client script completes. If you use a business rule they can be off about their other work whilst the BR runs.


View solution in original post

4 REPLIES 4

sushant007
Kilo Guru

not sure if your setValue got executed because of previous line


'alert('now1'+parseInt(answer)+"And Total: "+d);'



where d is not defined


sushant007
Kilo Guru

Also you may want to check if "Risk" is integer field or string field and parse it as per need before setting the value.


Gurpreet07
Mega Sage

This is because you are using asynchronous GlideAjax. At the time your call back function is called, form is already submitted.


Dubz
Mega Sage

I don't really understand the use case for setting a value of a field on the form based on other fields on the form. A before insert/update business rule will do the same thing.



You can make you glideajax synchronous with getXMLWait() but you make your users hang around while the client script completes. If you use a business rule they can be off about their other work whilst the BR runs.