Need help - Scheduled job to copy the old RITM variable values in the new inserted RITM

MR1
Tera Contributor

Hi All - I have a scheduled job and it's only inserting the new REQ and RITM but the user option of the RITM is empty. 
No variables are copied from the old RITM to the new RITM. I need help to copy old RITM variables into new ones.

var arr=[];
var gr= new GlideRecord("sc_req_item");
gr.addEncodedQuery("cat_item=c47374801b6b2150f761e753604bcb5e^sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()");
gr.addQuery('sys_id','00a8bcec1bc00e90a13e33b4cc4bcbb4');
gr.query();
while(gr.next()){
if(gr.variables.u_tool=="work" && gr.variables.u_export=="Yes"){

var req=new GlideRecord("sc_request");
req.initialize();
req.requested_for=gr.request.requested_for;
req.short_description=gr.request.short_description;
req.description=gr.request.description;
var request=req.insert();
	var ritm=new GlideRecord("sc_req_item");
	ritm.initialize();
	ritm.short_description=gr.short_description;
	ritm.cat_item=gr.cat_item;
	ritm.request=request;
	ritm.insert();
	


arr.push(gr.getValue('sys_id'));
}
}
1 ACCEPTED SOLUTION

Appanna M
Tera Guru

Hello @MR1 ,

 

Can you try the below once and let me know the results. 

 

 

var arr = [];
var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("cat_item=c47374801b6b2150f761e753604bcb5e^sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()");
gr.addQuery('sys_id', '00a8bcec1bc00e90a13e33b4cc4bcbb4');
gr.query();

while (gr.next()) {
    if (gr.variables.u_tool == "work" && gr.variables.u_export == "Yes") {
        var req = new GlideRecord("sc_request");
        req.initialize();
        req.requested_for = gr.request.requested_for;
        req.short_description = gr.request.short_description;
        req.description = gr.request.description;
        var request = req.insert();

        var ritm = new GlideRecord("sc_req_item");
        ritm.initialize();
        ritm.short_description = gr.short_description;
        ritm.cat_item = gr.cat_item;
        ritm.request = request;
        var newRitmSysId = ritm.insert();

        // Copy variables from old RITM to new RITM
        copyVariables(gr.sys_id, newRitmSysId);

        arr.push(gr.getValue('sys_id'));
    }
}

function copyVariables(oldRitmSysId, newRitmSysId) {
    var grVars = new GlideRecord("sc_item_option_mtom");
    grVars.addQuery("request_item", oldRitmSysId);
    grVars.query();
    while (grVars.next()) {
        var newVar = new GlideRecord("sc_item_option_mtom");
        newVar.initialize();
        newVar.request_item = newRitmSysId;
        newVar.item_option = grVars.item_option;
        newVar.item_option_value = grVars.item_option_value;
        newVar.insert();
    }
}

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

View solution in original post

4 REPLIES 4

Appanna M
Tera Guru

Hello @MR1 ,

 

Can you try the below once and let me know the results. 

 

 

var arr = [];
var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("cat_item=c47374801b6b2150f761e753604bcb5e^sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()");
gr.addQuery('sys_id', '00a8bcec1bc00e90a13e33b4cc4bcbb4');
gr.query();

while (gr.next()) {
    if (gr.variables.u_tool == "work" && gr.variables.u_export == "Yes") {
        var req = new GlideRecord("sc_request");
        req.initialize();
        req.requested_for = gr.request.requested_for;
        req.short_description = gr.request.short_description;
        req.description = gr.request.description;
        var request = req.insert();

        var ritm = new GlideRecord("sc_req_item");
        ritm.initialize();
        ritm.short_description = gr.short_description;
        ritm.cat_item = gr.cat_item;
        ritm.request = request;
        var newRitmSysId = ritm.insert();

        // Copy variables from old RITM to new RITM
        copyVariables(gr.sys_id, newRitmSysId);

        arr.push(gr.getValue('sys_id'));
    }
}

function copyVariables(oldRitmSysId, newRitmSysId) {
    var grVars = new GlideRecord("sc_item_option_mtom");
    grVars.addQuery("request_item", oldRitmSysId);
    grVars.query();
    while (grVars.next()) {
        var newVar = new GlideRecord("sc_item_option_mtom");
        newVar.initialize();
        newVar.request_item = newRitmSysId;
        newVar.item_option = grVars.item_option;
        newVar.item_option_value = grVars.item_option_value;
        newVar.insert();
    }
}

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

MR1
Tera Contributor

Unfortunately, it didn't work

It has inserted the REQ and RITM without variables(user option). 

Bert_c1
Kilo Patron

Maybe adding the following line before the "ritm.insert();" will work:

 

ritm.variables = gr.variables;

 

But Appanna M's function 'copyVariables' looks like the proper way to do that.

MR1
Tera Contributor

I tried your suggestion but it didn't work.

not sure what's wrong and why it can't copy the RITM values