- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 07:50 AM
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'));
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:35 AM
Unfortunately, it didn't work
It has inserted the REQ and RITM without variables(user option).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:19 AM - edited 05-16-2024 08:22 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 09:12 AM
I tried your suggestion but it didn't work.
not sure what's wrong and why it can't copy the RITM values