Business Rule on sc_req_item to update custom Requested For field

galavodasal
Giga Expert

We currently have a Business Rule that will pull the user from the requested_for or who_requested variables on the catalog item and updated the Requested For field of the Request with that user. Of course, this poses issues if you open multiple catalog items for various people within one request.

We're adding a custom u_requested_for field to the sc_req_item table and would like a Business Rule that sets the u_requested_for based on who was entered in the requested_for or who_requested variables on the catalog item.

I tried altering the existing BR but had no luck. Here is the existing BR for the Request table. I just need it updated so that it works on the Requested Item (sc_req_item) table.

var grRITM = new GlideRecord('sc_req_item');      

grRITM.addQuery('request', current.sys_id);      

grRITM.query();      

while (grRITM.next()) {      

  if (!JSUtil.nil(grRITM.variables.requested_for)) {      

      current.requested_for = grRITM.variables.requested_for;    

  }      

  if (!JSUtil.nil(grRITM.variables.who_requested)) {      

      current.requested_for = grRITM.variables.who_requested;    

  }

}    

I tried changing the table to the sc_req_item table and updated the script but it didn't work. I tried a couple other versions but no luck.

var grRITM = new GlideRecord('sc_req_item');      

grRITM.query();      

while (grRITM.next()) {      

  if (!JSUtil.nil(grRITM.variables.requested_for)) {      

      current.u_requested_for = grRITM.variables.requested_for;    

  }      

  if (!JSUtil.nil(grRITM.variables.who_requested)) {      

      current.u_requested_for = grRITM.variables.who_requested;    

  }

}    

1 ACCEPTED SOLUTION

Mihir Mohanta
Kilo Sage

Hi Alexander,




Make the business rule you have written active false.


Write an async type business rule in the sc_req_item table.Script should be like:




updateRequestedFor();


function updateRequestedFor(){


if(current.variables.requested_for !=''){


current.u_requested_for = current.variables.requested_for ;


}


else{


current.u_requested_for   = current.variables.who_requested ;


}


current.update();


}






Mark it as Helpful/Correct if it helps




Thanks,


Mihir


View solution in original post

16 REPLIES 16

Mihir Mohanta
Kilo Sage

Hi Alexander,




Make the business rule you have written active false.


Write an async type business rule in the sc_req_item table.Script should be like:




updateRequestedFor();


function updateRequestedFor(){


if(current.variables.requested_for !=''){


current.u_requested_for = current.variables.requested_for ;


}


else{


current.u_requested_for   = current.variables.who_requested ;


}


current.update();


}






Mark it as Helpful/Correct if it helps




Thanks,


Mihir


Mihir,



Thank you, this worked, however I get an error when ordering the items. I'm not sure how they're getting duplicate sys_id's though.


Unique Key violation detected by database (Duplicate entry 'ef1665ba2bee1200116ad1cc27da15b7' for key 'PRIMARY')



Unique Key violation detected by database (Duplicate entry 'a31665ba2bee1200116ad1cc27da15b9' for key 'PRIMARY')
Thanks,
Alex

Debug the form and find out why the error message is coming.



Thanks,


Mihir


I'm not seeing anything in the errors, but I might not be looking for the right stuff. I see the Business Rule running and there are no errors:



13:00:03.649: ==> 'Update Request Requested For' on sc_req_item:RITM5397


13:00:03.650: Execute before insert business rules on sc_req_item:RITM5397 before engines (order <1000)


There are multiple 'update()'s happening here. Even if this is async, i suspect there are BRs written on sc_task or sc_request which are updating RITMs for some or other reason at the same time. If you have used async BR then i am pretty sure you were getting these errors previously. Please confirm.