Business Rule Script to Update Another Table

jmiskey
Kilo Sage

We have a custom application named "Desktop Services".   Within that, we have a table named "Desktop Services Table", which is an extension of the Task table.

To the "State" field, we added a custom choice option of "Never Hired".   I would like to add a Business Rule that does the following:

When the State field is set to "Never Hired", the following changes should happen to the User record that this Desktop Services task is for:

- Update the Term Date field (u_term_date) to match the value in the Last Hire Date field (u_hire_date)

- Uncheck the "active" checkbox

- Check the "term ticket generated" (u_term_ticket_generated) checkbox

I know how to set the Business Rule to run on the update of the State field in the "Desktop Services" table, but I am unsure how to write the Business Rules script that would then make these three updates to the corresponding User record.

Thanks

1 ACCEPTED SOLUTION

Here you go

 

var usr = new GlideRecord('sys_user');

 

usr.addQuery('sys_id',current.<user sys id>);// or get user sys_id from desktop service task table

 

usr.query();

 

while(usr.next()){

 

 

usr.u_term_date = usr.u_hire_date;

 

usr.active = "false";

 

usr.u_term_ticket_generated = "true";// provided this is Boolean column

 

usr.update();

 

}

 

Regards,

Sachin

View solution in original post

10 REPLIES 10

sachin_namjoshi
Kilo Patron
Kilo Patron

Hi,



Add condition in BR that state changes and add below


You can achieve your requirement with below business rule script.




var usr = new GlideRecord('sys_user');


user.addQuery('sys_id',current.<user sys id>);// or get user sys_id from desktop service task table


user.query();


while(user.next()){



user.u_term_date = user.u_hire_date;


user.active = "false";


user.u_term_ticket_generated = "true";// provided this is Boolean column


user.update();


}



Regards,


Sachin


For some reason, that does not seem to be working out for me.



Here is what my "When to run" tab looks like:


find_real_file.png


and here is what my "Advanced" tab looks like (not I noticed you declared the variable as "usr" but then used "user" after that in the code, I corrected that):


find_real_file.png


The Business Rule is active, and the Application is set to "Desktop Services".   I updated a Desktop ticket for a record where the Category was "New User Install", changing the State to "Never Hired".   But nothing happened to the User record.



Any idea why it would not work?


Please add log statements to check if usr.query returns any records.


Also, this business rule is onbefore, right?



Regards,


Sachin


I found the issue!


When I was correcting the discrepancy between "usr" and "user", I missed this one:


usr.u_term_date = user.u_hire_date;



So, you original code, once corrected for usr/user typo worked.



Is there any way you can edit it to fix it in the original code, so if I accept that as the solution, the accepted solution/code shown will be correct (in case other users reference it)?


If not, if you can just post the code again with the correction, I will accept that one.



Thanks for you help!