How to create business rule on catalog task (sc_task) table?

Annie10
Tera Contributor

Hello,

How to create a business rule that grab all the Catalog Tasks that belong to a RITM?

Here is what I have, but it did not work.  Please provide suggestions.  Thank you

find_real_file.png

HERE IS THE ENTIRE CODE:

When to run:  after insert and update

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

    var grTasks = new GlideRecord('sc_task');
    grTasks.addQuery('parent', current.sys_id);
    grTasks.query();
    while (grTasks.next()) {
        //do something with the task record
        grTasks.short_description = current.short_description;
        grTasks.decription = current.decription;
        grTasks.assignment_group = "Service Desk";
        grTasks.cmdb_ci = "Software";
        grTasks.update();
    }
})(current, previous);

 

1 ACCEPTED SOLUTION

AnirudhKumar
Mega Sage
Mega Sage

I will make an assumption of what it it you are looking for. Every catalog task that gets created as part of one particular catalog item needs to be updated with the RITM's short description and description in the corresponding fields, (I hope I'm right?)

 

Ideally this needs to be done on the Workflow or the Flow designer that is tagged to the catalog item. It's far more easier to maintain that way. Anyway, if for some reason you really have to do it with a BR, then have the BR on Catalog Task table and run it before insert.

Here's your code:

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

    current.short_description = current.request_item.short_description;
    current.description = current.request_item.description;

})(current, previous);

 

 

Also, this can be achieved without any code with a flow designer. Give it a try 🙂

View solution in original post

9 REPLIES 9

@AnirudhKumar 

I have one last question. 

I was trying to update the task short description with the RITM variable, but it did not work.

I must have done it incorrectly.  Can you please make correction? Thanks

current.short_description = current.request_item.variables.requested_for;

Annie,

I tested this out in my PDI and it works - your code is good.

If the short description isn't updating then there may be several reasons:

- The variable name requested_for could be incorrect

- The field requested_for may not be holding any value

- If the BR was updated to run 'After' the task insert/update, then an additional current.update() is required after the current line. (I'd avoid running After BR entirely)

- At the moment of catalog task creation, there is some other script running at the same time overwriting our update.

 

Have you tried logging out the value?

gs.info('Mirror mirror on the wall... ' + current.request_item.variables.requested_for);
current.short_description = current.request_item.variables.requested_for;

 

 

@AnirudhKumar 

I  hope the entered the gs.info in the correct place.  After the business rule executed, where you do I find the log?  Many thanks to you.

find_real_file.png

Annie,

The statement gs.info or gs.log creates a record in the syslog table. 

Whenever our script does not work as desired, and we wish to understand why system 'is on not on the same page' as we are in, we log parts of the code.

 

Navigate to System Logs -> System Log -> All

find_real_file.png

@AnirudhKumar 

You have been an excellent teacher.  My team and I learned a lot from you.

We are sincerely thank you!