Business rule not updating on insert

agarc657
Giga Contributor

Hello everyone,

Im still new to Servicenow and have an issue with a Business rule not updating on insert

Capture.PNG

with the following script attached

Capture2.PNG

So this script works on Update but not on Insert . Through my testing it seems that on insert the record fields are blank so this is nothing to put into the field. Does anyone know how I

would work around this?

Thanks for everything in advance guys and gals

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Hi Andrew,


Sys ids are generated after insertion. You can simplify your code. You dont need sys_id. Try the below code and it should work.



(function executeRule(current, previous /*null when async*/) {


if (current.sys_class_name == 'incident')


{


  current.u_requester = current.caller;


}


else if (current.sys_class_name == 'sc_req_item')


{


  current.u_requester = current.request.requested_for;


}


else if (current.sys_class_name == 'sc_task')


{


  current.u_requester = current.request_item.request.requested_for;


}




})(current, previous);



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

SanjivMeher
Kilo Patron
Kilo Patron

Hi Andrew,


Sys ids are generated after insertion. You can simplify your code. You dont need sys_id. Try the below code and it should work.



(function executeRule(current, previous /*null when async*/) {


if (current.sys_class_name == 'incident')


{


  current.u_requester = current.caller;


}


else if (current.sys_class_name == 'sc_req_item')


{


  current.u_requester = current.request.requested_for;


}


else if (current.sys_class_name == 'sc_task')


{


  current.u_requester = current.request_item.request.requested_for;


}




})(current, previous);



Please mark this response as correct or helpful if it assisted you with your question.

THANKS!!!! YOU DA BEST


DUGGI
Giga Guru

update the when condition to "after",i think your problem will solve.


vinothkumar
Tera Guru

Hello Andrew,



Try to use after insert business rule, since before inserting you won't be having a sys_id which might result in unexpected result.