Adding affected CI's to change request using Transform script

Jake Adams
Tera Contributor

Hi,

 

I have an import set api to create change requests.

 

Based on few conditions I want to add multiple Affected CI's to the Change request once its created.

 

I am trying to do this using the onbefore Transform script but it's not adding the CI's to Affected CI related list.

 

How can I achieve this? 

3 REPLIES 3

Niklas Peterson
Mega Sage
Mega Sage

Hi,

Is the import creating the Change Request? Then you may want to consider using onAfter instead of onBefore. The Change Request must exist before the Affected CIs can be mapped to it.

 

What does your script look like?

 

Regards,
Niklas

Hi,

 

Yes the import API is creating the change request

 

I also tried it in OnAfter script:

 

Below is the script that I am using 

 

var gr = new GlideRecord('table_name'); //table_name is my custom table name
   
    gr.addQuery('u_configuration_item.operational_status', '1');
    gr.query();
    while (gr.next()) {
        var taskCR = new GlideRecord('task_ci');
        taskCR.initialize();
        taskCR.task = target.sys_id;
        taskCR.ci_item = gr.u_configuration_item;
        taskCR.insert();
    }

Hi @Jake Adams ,

 

Please try the below code in onAfter transform script:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var gr = new GlideRecord('table_name'); //table_name is my custom table name
    gr.addEncodedQuery("cmdb_ci.operational_status = 1");
    gr.query();
    while (gr.next()) {
        var taskCR = new GlideRecord('task_ci');
        taskCR.initialize();
        taskCR.task = target.sys_id;
        taskCR.ci_item = gr.u_configuration_item;
        taskCR.insert();
    }

})(source, map, log, target);

 

 

Please mark this response as correct or helpful if it assisted you with your question.

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.