Updating RITM when related CHG updates

yasserbouat
Tera Guru

Hello experts,

I created a business rule that creates automatically a “change request” from RITM.
Then I created another business rule that does the following : when the change request state changes to “assess” , the related RITM updates to “work in progress” : the problem is my script is not working : when I update a chg request, the related RITM didn't get updated ! I tried to fix it with chatgpt but it didn't work : 

yasserbouat_0-1742392102431.png

 

 

(function executeRule(current) {

    
    if (current.state == 3) {

        var ritm = new GlideRecord('sc_req_item');
        
        ritm.addQuery('sys_id', current.parent);  
        ritm.query();

        if (ritm.next()) {
            ritm.state = 2;  
            ritm.update();
          
        } else {
            gs.error('RITM not found ' + current.sys_id); 
        }
    }

})(current);

 

 

1 ACCEPTED SOLUTION

Hi @yasserbouat 

add this code :

chg.parent = current.sys_id;

after - chg.priority = current.variables.v_priority.toString();

Then try to test it and check it parent field on change request form is updated or no.

View solution in original post

13 REPLIES 13

This is what I found : what shoul I do next in order to make the script functionning please ?

<order/>
<outside_maintenance_schedule>false</outside_maintenance_schedule>
<parent/>
<phase>requested</phase>
<phase_state>open</phase_state>



yasserbouat_0-1742395984941.png

 

So this means, when RITM creates change request, the RITM number is not updated in the parent field in the change request table. Can you share your 1st BR code? How are you establishing that the relation between change request and RITM? How do you know which RITM created which change request?

@Dhana3 here is thescript of the first BR that creates "chg" from RITM : 

(function executeRule(current) {

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

    chg.type = current.variables.v_model.toString();  
    chg.category = current.variables.v_category.toString();
    chg.short_description = current.variables.v_short_description.toString();
    chg.priority = current.variables.v_priority.toString();
    
    // Ensure service reference is valid
    if (current.variables.v_service) {
        chg.cmdb_ci = current.variables.v_service;
    }

    chg.state = 1; // Optional: Defaults to "New"

    // Assign to "ITSM Engineering" group
    var group = new GlideRecord('sys_user_group');
    if (group.get('name', 'ITSM Engineering')) {
        chg.assignment_group = group.sys_id;
    } else {
        gs.error('Assignment group "ITSM Engineering" not found.');
    }

    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);

Hi @yasserbouat 

 

In this script you need to below line of code to establish a relationship between RITM and Change. There is no relationship that is why parent is showing blank in your XML.

 

chg.parent = current.sys_id;    //

 

Hi @yasserbouat ,

 

Please accept the solution as well, so that it will help future readers as well.

 

Regards,

Rohit