Business Rule to Updte Group Approval short_description from RITM

Joe Trimble
Tera Expert

I am trying to write a Business Rule to update the sysapproval_group short_description field with the content of the RITM (sc_req_item) short_description.  Below is the code I'm working with currently.  I get an error in the log file when this BR fires:

Invalid query detected, please check logs for details [Unknown field null in table sc_req_item]

 

Business Rule:

Table:  sysapproval_group

After Insert or Update

No Filter conditions specified

Advanced is checked

Script:

 

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

    // Get the Request Item (RITM) sys_id
    // var ritmSysId = current.request_item.toString();
    var ritmSysId = current.request_item();

    // Check if the Request Item field is not empty
    if (ritmSysId) {
        // Get the RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(ritmSysId)) {
            // Update the Group Approval short description with the RITM short description
            // current.short_description = ritmGr.short_description.toString();
            current.short_description = ritmGr.short_description();
        }
    }

})(current, previous);

 

 

Why would I get the error? Any advice?

Thanks!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Joe Trimble ,

I tried your problem in my PDI and it works for me please make some small configurations it will work for you 

1. Please mark your BR Before not after.

2. Please add below code 

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

    // Add your code here
	gs.log('inside Check BR = ' + current.parent);
    var ritmSysId = current.parent;

    // Check if the Request Item field is not empty
    if (ritmSysId) {
		gs.log('inside IF = ' + current.parent);
        // Get the RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(ritmSysId)) {
			gs.log('inside IF and IF = ' + ritmGr.short_description);
            // current.short_description = ritmGr.short_description();
			current.setValue('short_description', ritmGr.short_description);
        }
    }

})(current, previous);

This will work for you 

Here's the result 

SarthakKashya2_0-1713803922552.png

Short Description is updating in sysapproval_group table 

SarthakKashya2_1-1713803975478.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @Joe Trimble ,

I tried your problem in my PDI and it works for me please make some small configurations it will work for you 

1. Please mark your BR Before not after.

2. Please add below code 

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

    // Add your code here
	gs.log('inside Check BR = ' + current.parent);
    var ritmSysId = current.parent;

    // Check if the Request Item field is not empty
    if (ritmSysId) {
		gs.log('inside IF = ' + current.parent);
        // Get the RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(ritmSysId)) {
			gs.log('inside IF and IF = ' + ritmGr.short_description);
            // current.short_description = ritmGr.short_description();
			current.setValue('short_description', ritmGr.short_description);
        }
    }

})(current, previous);

This will work for you 

Here's the result 

SarthakKashya2_0-1713803922552.png

Short Description is updating in sysapproval_group table 

SarthakKashya2_1-1713803975478.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

 

Thank you! This does work.  Appreciate the quick response!

Joe

Sandeep Rajput
Tera Patron
Tera Patron

@Joe Trimble You need to update the business rule to run before insert/update and keep the script as follows.

 

 

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

    // Get the Request Item (RITM) sys_id
    var ritmSysId = current.parent+'';
    // Check if the Request Item field is not empty
    if (ritmSysId) {
        // Get the RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(ritmSysId)) {
            // Update the Group Approval short description with the RITM short description
            current.short_description = ritmGr.getValue('short_description');            
        }
    }

})(current, previous);

 

 

In case you do not wish to have an onBefore business rule and would like to just keep the business rule to run after insert/update then update the BR script as follows.

 

 

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

    // Get the Request Item (RITM) sys_id
    var ritmSysId = current.parent+'';
 

   // Check if the Request Item field is not empty
    if (ritmSysId) {
        // Get the RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(ritmSysId)) {
            // Update the Group Approval short description with the RITM short description
             current.short_description = ritmGr.getValue('short_description');
              current.setWorkflow(false);
              current.update();
        }
    }

})(current, previous);