Copy variables values from old catalog item A into a new catalog item B

MR1
Tera Contributor

Hi All,

 

The below-scheduled job works as expected and copies the variable values from the old RITM into the new RITMs. However, we want to copy the variable text type "Description" from inactive catalog item A into the variable "short description" of active catalog item B.

Existing old RITM variables need to be copied into the new RITM.

 

var arr = [];
var usr = [];

var gr1 = new GlideRecord("x_***_access");
gr1.addEncodedQuery("grant=cfdeed621b504210644e42a6bc4bcb16^expiration_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^status=active");

gr1.query();

while(gr1.next()){

	request_for = gr1.user;
	gs.info ('hello1'+ gr1.user.employee_number);

var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("cat_item=c47374801b6b2150f761eb95604bcb5e^requested_for="+request_for);
gr.orderByDesc('sys_created_on');

gs.info("hello1.5 user"+request_for+" "+gr.getRowCount());
while(gr.next()){
//if(gr.variables.RequestFor == "Yes"){
	
	gs.info ('hello2');
        var req = new GlideRecord("sc_request");
        req.initialize();
        req.requested_for = request_for;
		// req.requested_for = gr.request.requested_for;
        req.short_description = gr.request.short_description;
        req.description = gr.request.description;
        var request = req.insert();
		gs.info("hello2.5 "+request.number);

        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);
		break;
        //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.sc_item_option = grVars.sc_item_option;
      //  newVar.item_option_value = grVars.item_option_value;
		newVar.item_option_value = grVars.item_option_value;
		ritm.variables = gr.variables;
        newVar.insert();
    }
}

 

 

Thanks

3 REPLIES 3

Appanna M
Tera Guru

Hello @MR1 ,

 

I am not sure if this script helps you. But give a try and let me know the results. 

var arr = [];
var usr = [];
var gr1 = new GlideRecord("x_***_access");
gr1.addEncodedQuery("grant=cfdeed621b504210644e42a6bc4bcb16^expiration_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^status=active");
gr1.query();
while (gr1.next()) {
    var request_for = gr1.user;
    gs.info('Processing user: ' + request_for.employee_number);
    var gr = new GlideRecord("sc_req_item");   
	gr.addEncodedQuery("cat_item=c47374801b6b2150f761eb95604bcb5e^requested_for=" + request_for);
    gr.orderByDesc('sys_created_on');
    gr.query();
    gs.info("Number of old RITMs for user: " + request_for + " is " + gr.getRowCount());
    while (gr.next()) {
        gs.info('Found old RITM: ' + gr.number);
        // Fetch the "Description" variable from the old RITM
        var oldDescription = '';
        var grVars = new GlideRecord("sc_item_option_mtom");
        grVars.addQuery("request_item", gr.sys_id);
        grVars.query();
        while (grVars.next()) {
            if (grVars.sc_item_option.item_option_new.name == 'description') {
                oldDescription = grVars.item_option_value;
                break;
            }
        }
        var req = new GlideRecord("sc_request");
        req.initialize();
        req.requested_for = request_for;
        req.short_description = gr.short_description;
        req.description = gr.description;
        var request = req.insert();
        gs.info("Created new request: " + request.number);
        var ritm = new GlideRecord("sc_req_item");
        ritm.initialize();
        ritm.short_description = oldDescription; // Set the short description with the old Description
        ritm.cat_item = 'new_catalog_item_sys_id'; // Replace with the sys_id of the new catalog item
        ritm.request = request;
        var newRitmSysId = ritm.insert();
        // Copy other variables from old RITM to new RITM
        copyVariables(gr.sys_id, newRitmSysId);
        gs.info('Created new RITM: ' + ritm.number + ' with short description from old Description');
        break;
    }
}
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.sc_item_option = grVars.sc_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

@Appanna M - Thank you for your response!

The above script overrides all the old RITM variables into the new RITM. Old RITM refers to catalog item A whereas new RITM refers to catalog item B. We want to copy the old RITM variable values, not all the variables.

Moreover, the Description variable value of catalog item A RITM should be copied into the Short description variable of catalog item B RITM.

 

Thanks

MR1
Tera Contributor

Can we import the data into the RITM table via Excel sheet and insert a new REQ and RITM?

 

Thanks