How to make onsubmit client script just run once or whenever condition changed to true

omsa1
Kilo Guru

Hi All,

I have a client script that will generate a change task when a communication checkbox is checked.But It is creating duplicate tasks each time user update something in the change request. How can i change this to make just create a task ticket when users check u_communication script. It should check if there is any exiting task for communication and create one if none, when user checked the u_communication.

function onSubmit() {

    //Type appropriate comment here, and begin script below

   

  var comm = g_form.getValue('u_communication');

  var ct = new GlideRecord("change_task");  

    if (comm == 'true') {

    ct.initialize();  

    ct.short_description = 'Process Communication Mail Out request';

    ct.description = g_form.getValue('description');

    ct.assignment_group = "244690cf37b1d600b8f3c97a43000ef8";

    ct.change_request = g_form.getUniqueValue();  

    ct.insert();

  }

}

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Sarasaamani,



Any reason you want to do this at client side? It's not a best practice to make   I would recommend you to create an AFTER business rule and set the filter conditions to trigger it only when communication field is modified. Please refer the below script and modify it as per your req.


var ct = new GlideRecord("change_task");


ct.addQuery('change_request', current.sys_id);


ct.addQuery('short_description', 'Process Communication Mail Out request')


ct.query();


if(!ct.next())


  {


  ct.initialize();


  ct.short_description = 'Process Communication Mail Out request';


  ct.description = current.getValue('description');


  ct.assignment_group = "244690cf37b1d600b8f3c97a43000ef8";


  ct.change_request = current.sys_id;


  ct.insert();


}


https://wiki.servicenow.com/?title=GlideRecord


View solution in original post

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Sarasaamani,



Any reason you want to do this at client side? It's not a best practice to make   I would recommend you to create an AFTER business rule and set the filter conditions to trigger it only when communication field is modified. Please refer the below script and modify it as per your req.


var ct = new GlideRecord("change_task");


ct.addQuery('change_request', current.sys_id);


ct.addQuery('short_description', 'Process Communication Mail Out request')


ct.query();


if(!ct.next())


  {


  ct.initialize();


  ct.short_description = 'Process Communication Mail Out request';


  ct.description = current.getValue('description');


  ct.assignment_group = "244690cf37b1d600b8f3c97a43000ef8";


  ct.change_request = current.sys_id;


  ct.insert();


}


https://wiki.servicenow.com/?title=GlideRecord


Hi Pradeep,



I used BR earlier and it didn't work for me.Therefore, i tried with client script, might be something wrong with my script, your script working well, Thank you.