How to save a record from client side script

Som8
Tera Contributor

Dear Folks,

Is there any way to save a field value from client side script.I was using the below script :  

g_form.setValue("u_risk_assessment_check",'false');

  g_form.save();

but is not saving the value. The value is getting set but it vanishes while the form is reloaded.

function invokeAssessment() {

  g_form.hideAllFieldMsgs();

  g_form.setValue("u_risk_assessment_check",'false');

  g_form.save();

  var id = g_form.getUniqueValue();

  //For risk assessment question "Has a recent incident been raised against the Assignment or Affected CI's?"

  var v_ga = new GlideAjax("jnjChgRiskAssessment");

  v_ga.addParam("sysparm_name", "riskQuestions");

  v_ga.addParam("sysparm_id1", id);

  v_ga.addParam("sysparm_class1", "change_request");

  v_ga.getXMLAnswer(function (res1){

  res2=res1;

  });

7 REPLIES 7

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Must be something with your setValue. I just did a quick test with this in the JavaScript Executor(Shift+Ctrl+Alt+J in windows)


g_form.setValue("short_description",'testing');


g_form.save();



And it worked just as intended.



How about you clean your script and only do:



g_form.setValue("short_description",'testing');



Does that work?



//Göran


Hi Goran,



If we only use g_form.setValue("u_risk_assessment_check",'false'); the value has been populated. But the value is disappearing when the form is loading.


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Then I would probably go for what Midhun writes down below. script gets messy when you try to save in the middle and there is still code to run.. try put the save in last.



I would also think of perhaps having this as a onChange perhaps instead so it does the check when the user types in the value and aint waiting to check until they try to save it.



//Göran


Midhun1
Giga Guru

Hi,



First thing:   g_form.setValue("u_risk_assessment_check",false);



I guess, it is setting the value and again due to glide ajax it is waiting for the response. Both are conflicting.


Always better to use save method() at the end of the script.