Copying variables from an RITM into a change task?

bradschamerloh
Kilo Expert

I'm having problems getting a couple variables from an RITM copied into a change task.

I've built a workflow that creates a task after someone requests something.   So the person that's responsible for working on the task doesn't have to go into the RITM, I simply just want to copy the variables of the RITM into the change task.

RITM Variables:

find_real_file.png

Want to copy these two lines into the change task

find_real_file.png

1 ACCEPTED SOLUTION

Brad,



Try this


var var_list='';


var set = new GlideappVariablePoolQuestionSet();


set.setRequestID(current.getValue('sys_id'));


set.load();


var vs = set.getFlatQuestions();


for (var i=0; i < vs.size(); i++) {


      if((vs.get(i).getName()=='put in your variable Name here') || (vs.get(i).getName()=='put in your variable Name here')) {            


          var_list+=vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n";


      }


}  


task.description=var_list;


task.short_description=var_list;



This should work a expected now. Don't forget mark my responses as helpful and one of them as correct.



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

14 REPLIES 14

Figured it out. Thank you for the quick response.





Thank you,


Darren


Brad,



    Did it resolve your issue ? If yes please mark it as correct and close the thread or else let me know if you have any questions.



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Henrik Jutterst
Tera Guru

Found this post when I was looking for a way to copy variables from RITM to Change Request and if if helps here is the code I ran:

//copy variables from ritm to change request form
var ritmVar = new GlideRecord('sc_item_option_mtom');
ritmVar.addQuery('request_item', current.getUniqueValue());
ritmVar.addNotNullQuery('sc_item_option.value');
ritmVar.orderBy('sc_item_option.order');
ritmVar.query();

while(ritmVar.next()) {
	
	var chgVar = new GlideRecord('question_answer');
	chgVar.initialize(); 
	
	chgVar.table_name = 'change_request';
	chgVar.table_sys_id = chgSysId;
	chgVar.question = ritmVar.sc_item_option.item_option_new;
	chgVar.order = ritmVar.sc_item_option.order;
	chgVar.value = ritmVar.sc_item_option.value;
	
	chgVar.insert();
}

Henrik,

Where is 'chgSysId' coming from in this case, or rather how is it being calculated?

I'm trying to leverage similar logic to your post, but going from the question_answer table for HR Case back to HR Task, and again in another use case from Change Request down to Change Task, but I don't think I can without being able to specify table_sys_id properly which lead to my question.

Any insight is appreciated thanks.

Hi Adam, here is a version of the script I created. Just removed some user specific values, but this should give you a go.

Hope you get what you're looking for! (UI Action from the sc_req_item table)

KR

	var changeRequest = ChangeRequest.newNormal();
	
	//change_request, sc_req_item
	changeRequest.setValue("short_description", current.short_description);
	changeRequest.setValue("description", current.description);
	changeRequest.setValue("cmdb_ci", current.cmdb_ci);
	changeRequest.setValue("sys_domain", current.sys_domain);
	changeRequest.setValue("company", current.company);
	changeRequest.setValue("opened_by", current.assigned_to);
	changeRequest.setValue("parent", current.sys_id);
	
	changeRequest.insert();
	
	current.rfc = changeRequest.getGlideRecord().getUniqueValue();
	current.update();
	
	
	//copy work notes to change_request
	var chgSysId = changeRequest.getValue("sys_id");
	var chg = new GlideRecord('change_request');
	
	chg.addQuery('sys_id',chgSysId);
	chg.query();
	
	while(chg.next()) {
		chg.work_notes = current.work_notes.getJournalEntry(1);
		chg.update();
	}
	
	//copy ariables from ritm to change request form
	var ritmVar = new GlideRecord('sc_item_option_mtom');
	ritmVar.addQuery('request_item', current.getUniqueValue());
	ritmVar.addNotNullQuery('sc_item_option.value');
	ritmVar.orderBy('sc_item_option.order');
	ritmVar.query();
	
	while(ritmVar.next()) {
		
		var chgVar = new GlideRecord('question_answer');
		chgVar.initialize();
		
		chgVar.table_name = 'change_request';
		chgVar.table_sys_id = chgSysId;
		chgVar.question = ritmVar.sc_item_option.item_option_new;
		chgVar.order = ritmVar.sc_item_option.order;
		chgVar.value = ritmVar.sc_item_option.value;
		
		chgVar.insert();
	}
	
	//copy files from sc_req_item to change_request
	GlideSysAttachment.copy('sc_req_item', current.sys_id, 'change_request', chgSysId);
	
	gs.addInfoMessage("Change " + changeRequest.getValue("number") + " created");
	action.setRedirectURL(changeRequest.getGlideRecord());
	action.setReturnURL(current);