Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to update the sc_req_item variable when the sc_task is closed

Joshua Comeau
Kilo Sage

I want to know when a sc_task is closed to query the variable and/or the short description then update the same short description/variable to the sc_req_item

I was thinking something like this but it is not working:

 

function onCondition() {
    var grTask = new GlideRecord('sc_task');
    grTask.get(current.sys_id); // Use 'sys_id' to get the current task
    var taskShortDescription = grTask.short_description; // Get the short description of the sc_task

    var grRITM = new GlideRecord('sc_req_item'); // Use 'sc_req_item' for RITM
    grRITM.get(current.request_item); // Use 'request_item' to get the related RITM
    grRITM.short_description = taskShortDescription; // Update the RITM short description
    grRITM.update(); // Update the RITM record
}
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Just to be clear, variables only exist once, in an object that is available to all sc_task records within a sc_req_item record, so you don't need to copy one to/from the other.  You can copy a variable to a field like short description, or copy the short description to the short description or whatever.  It would be best to do this as a Business Rule.  In your scenario this BR would be on the sc_task table, after Update.  Add Filter Conditions for a specific Catalog Item and/or Catalog Tasks, unless you always want this to run when every Catalog Task is closed, and one for State changes to Closed Complete, or whatever fits your criteria.  In the Script on the Advanced tab is where you would do the GR on sc_req_item to update that record

(function executeRule(current, previous /*null when async*/ ) {
    var grRITM = new GlideRecord('sc_req_item'); // Use 'sc_req_item' for RITM
    if (grRITM.get(current.request_item)) { // Use 'request_item' to get the related RITM
        grRITM.short_description = current.short_description; // Update the RITM short description
        //grRITM.description = current.variables.var_name; //Update the RITM description with a variable
        grRITM.update(); // Update the RITM record
    }
})(current, previous);

View solution in original post

11 REPLIES 11

If the BR is running only after Insert, then current.short_description is going to be that of the task that was just created.  To be sure another BR or script is not affecting this, temporarily change it to current.number or something like.

Badrinarayan
Tera Guru

Hi @Joshua Comeau ,

Create a Business Rule on the `sc_task` table, set to run **after update**. It should update the `sc_req_item` record’s `short_description` when a task is closed.

 

Screenshot 2024-09-11 at 6.26.50 PM.png

 

Business Rule Code 

 

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

    if (current.state == '3' && current.state.changesTo('3')) {
        /* Create a GlideRecord object for the sc_req_item table */
        var grRITM = new GlideRecord('sc_req_item');
        /* Query the sc_req_item record related to this task */
        if (grRITM.get(current.request_item)) { 
            /* Update the sc_req_item short_description with the task short_description */
            grRITM.short_description= current.short_description; 
            grRITM.update(); /* Save the changes */
            gs.info('Updated RITM with short description from closed task.'); /* Log info message */
        }
    } else {
        gs.info('Task state is not closed. No update performed.');
    }

})(current, previous);

 

Please Mark it as Helpful ,If the Solution is Helpful

Thanks Regards

Badrinarayan