Create automatically a change request from RITM

yasserbouat
Tera Guru

Hello,

I want to create a change request automaticaaly from RITM when this one is created, and in order to do that I created a business rule as the following but it is not working : 

yasserbouat_0-1742307396817.png

Script : 

(function executeRule(current) {

    var chg = new GlideRecord('change_request'); 
    chg.initialize();

    chg.type = current.variables.model.toString();  
	chg.category = current.variables.category.toString();
    chg.short_description = current.variables.short_description.toString();
    chg.priority = current.variables.priority.toString();
    // Ensure service reference is valid
    if (current.variables.service) {
        chg.cmdb_ci = current.variables.service;
    }

    chg.state = 1; // Optional: Defaults to "New"
    var chgSysId = chg.insert();

    if (chgSysId) {
        gs.info('Change Request ' + chgSysId + ' created from RITM ' + current.sys_id);
    } else {
        gs.error('Failed to create Change Request from RITM ' + current.sys_id);
    }

})(current);

and the mapping must be with the variables in RITM : Model - Category - Short description - Priority - service.
 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@yasserbouat 

your script is fine.

Did you check variable names are correct?

Did you add log at each line and see which line is breaking?

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

View solution in original post

6 REPLIES 6

KeerthiP
Mega Guru

Hi,

 

I would really prefer a Flow Designer for this functionality or a UI action.

Can you try from flow?

 

Thanks,

Keerthi

Kieran Anson
Kilo Patron

Because of the data policies within change management, you're likely creating an invalid Change Record. Instead, use the provided ChangeRequest script include which will create a change record based on these enforced policies. It's also maintainable, as it future proofs against the possible migration to change models.

 


var newChangeGR = ChangeRequest.newChange( current.variables.model.getValue() );
newChangeGR.setValue('category' , current.variables.category.getValue() );
newChangeGR.setValue('short_description' , current.variables.short_description.getValue() );
newChangeGR.setValue('priority' , current.variables.priority.getValue() );

newChangeGR.insert();