populate change task fields from change request

Scotty88
Tera Contributor

I am trying to create a BR to do the following however can't get this to work, I have tried both before and after insert (see script below), all I am trying to do is:

 When a change task is created from a change request the following fields on the change tasks are copied from the parent change:

- The Change Number of the parent change is copied to the Short Description of the Change Task

- The Planned start date and Planned end date of the parent change are copied to the Planned start date and Planned end date of the change task

 

 

 

(function executeRule(current, gsn, gs) {

    if (!current.change_request) {
        return;
    }

    // Load parent change request
    var parentChange = new GlideRecord('change_request');
    if (parentChange.get(current.change_request)) {

        // Load current task again in writeable mode
        var task = new GlideRecord('change_task');
        if (task.get(current.sys_id)) {

            // Populate short_description if empty
            if (!task.short_description) {
                task.short_description = parentChange.number;
            }

            // Populate planned_start_date if start_date exists
            if (!task.planned_start_date && parentChange.start_date) {
                task.planned_start_date = parentChange.start_date;
            }

            // Populate planned_end_date if end_date exists
            if (!task.planned_end_date && parentChange.end_date) {
                task.planned_end_date = parentChange.end_date;
            }

            task.update();
        }
    }

})(current, gsn, gs);
8 REPLIES 8

@Scotty88 

did you check if the BR triggered?

It worked for me well for Short Description but not for other 2 fields

This doesn't sound like a valid business requirement as 1 Change can have multiple change tasks

If this requirement is just for your learning purpose then check the details below

you can use before insert BR on change_task table and copy the fields from CHG to Change task

something like this in BR

(function executeRule(current, previous /*null when async*/) {
   
   current.planned_start_date = current.change_request.start_date;
   current.planned_end_date = current.change_request.end_date;

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Not sure what is happening, tried all solutions in both work instance and PDI and nothing is working

@Scotty88 

something is blocking the fields from getting populated

Did you see if you can edit them manually?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

nayanmule
Tera Expert

Hi @Scotty88 ,

 

Please make use of Out of box Change Properties. Where you can define the fields that you want to populate from Change request to change tasks .

nayanmule_0-1746706175350.png

Thankyou!