create catalog task

ShraddhaMore
Tera Contributor

Hello Community,

 

Requirement: If the particular field of the requested item record is true then auto create catalog task for the requested item.

I have created a business rule to achieve this. But the task is getting created on every update of the requested item record.

I want the catalog task to be created once and if the requested item is updated then the existing task needs to update instead of creating new catalog task each time.

Any inputs how to achieve this?

 

Thanks in advance!

ShraddhaMore_0-1714498604274.pngShraddhaMore_1-1714498630318.png

 

 

1 ACCEPTED SOLUTION

@ShraddhaMore Please try this updated script, I forgot to add method brackets () in current.operation().

 

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

    if (current.cat_item.name == "Request email alias" && current.u_create_task == true && current.operation()  == 'insert') { 
// Create a new catalog task 
        var task = new GlideRecord('sc_task');
        task.initialize();
        task.request_item = current.sys_id;
        task.short_description = 'Auto-generated task';
        task.state = current.state;
        task.setDisplayValue('assignment_group', 'App Engine Admins');
        task.u_stage = current.stage;
        task.u_boolean_1 = current.u_create_task;
        task.insert();
    } else {
       //Update exusting task
        var existingTask = new GlideRecord('sc_task');
        existingTask.addQuery('request_item', current.sys_id);
		existingTask.query();
        if (existingTask.next()) {
            existingTask.short_description = 'Auto-generated task';
            existingTask.state = current.state;
            existingTask.setDisplayValue('assignment_group', 'App Engine Admins');
            existingTask.u_stage = current.stage;
            existingTask.u_boolean_1 = current.u_create_task;
            existingTask.update();
        }

    }
})(current, previous);

Hope this helps.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@ShraddhaMore Please update your business rule script as follows and it should create only one task during insert and update it during the time of update.

 

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

    if (current.cat_item.name == "Request email alias" && current.u_create_task == true && current.operation == 'insert') { 
// Create a new catalog task 
        var task = new GlideRecord('sc_task');
        task.initialize();
        task.request_item = current.sys_id;
        task.short_description = 'Auto-generated task';
        task.state = current.state;
        task.setDisplayValue('assignment_group', 'App Engine Admins');
        task.u_stage = current.stage;
        task.u_boolean_1 = current.u_create_task;
        task.insert();
    } else {
       //Update exusting task
        var existingTask = new GlideRecord('sc_task');
        existingTask.addQuery('request_item', current.sys_id);
		existingTask.query();
        if (existingTask.next()) {
            existingTask.short_description = 'Auto-generated task';
            existingTask.state = current.state;
            existingTask.setDisplayValue('assignment_group', 'App Engine Admins');
            existingTask.u_stage = current.stage;
            existingTask.u_boolean_1 = current.u_create_task;
            existingTask.update();
        }

    }
})(current, previous);

Please mark the answer helpful and correct if it addresses your question.

@Sandeep Rajput , it's updating the existing catalog task, but not creating new task if there isn't any already existing task for request item.

 

@ShraddhaMore Please try this updated script, I forgot to add method brackets () in current.operation().

 

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

    if (current.cat_item.name == "Request email alias" && current.u_create_task == true && current.operation()  == 'insert') { 
// Create a new catalog task 
        var task = new GlideRecord('sc_task');
        task.initialize();
        task.request_item = current.sys_id;
        task.short_description = 'Auto-generated task';
        task.state = current.state;
        task.setDisplayValue('assignment_group', 'App Engine Admins');
        task.u_stage = current.stage;
        task.u_boolean_1 = current.u_create_task;
        task.insert();
    } else {
       //Update exusting task
        var existingTask = new GlideRecord('sc_task');
        existingTask.addQuery('request_item', current.sys_id);
		existingTask.query();
        if (existingTask.next()) {
            existingTask.short_description = 'Auto-generated task';
            existingTask.state = current.state;
            existingTask.setDisplayValue('assignment_group', 'App Engine Admins');
            existingTask.u_stage = current.stage;
            existingTask.u_boolean_1 = current.u_create_task;
            existingTask.update();
        }

    }
})(current, previous);

Hope this helps.

@Sandeep Rajput still not working, only updating the existing catalog task, but not creating new task if there isn't any already existing task for request item.