Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy Related Record to New Record

bburdick
Mega Guru

Basically, I am wanting to be able to copy all of the field attributes from one record into a new record, yet substitute just a few.

Rather than specifically call out every field such as the following:


current.assigned_to = producer.assigned_to;


I would like to just rather do a while loop where I could say something like this:


while(array.length > 0 ) {
current.array[x] = producer.array[x];
}


Any suggestions on what that coding would be? I am not sure how to get the fields in the array. I have done something simliar with catalog items where I declared all the variables on a form, but how do I get all the fields in a record?
6 REPLIES 6

tony_fugere
Mega Guru

This looks like a record producer? If so, the record producer will automagically perform a one-to-one match on any variables that match the name of a column on the target table. This loop is not necessary as it is already performed. If the variable does not match, it gets stored as a variable, as they all do, but will not store any data directly in the target table's fields.

If not record producer, what are you trying to do exactly? Get data out of a Catalog Item's variables and into the proper fields on the RITM record?


Thanks for the comments, Tony. Yes, it is record producer that it starts from --- however, I am not creating a bunch of variables.

Basically, I am asking three questions. 1. Which standard change request do you want to implement? (this is referenced to a previously approved normal change request..the one we will copy from.) 2. When will your change start? 3. When will your change end?

That is all I ask. Everything else I can gather by who is requesting the standard change and the reference of the "parent" normal change.

I think I actually have a solution to this from the SNCGuru site.

http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2


bburdick
Mega Guru

Okay, the information from SNCGuru's site was extremely close to providing the solution I need, however the problem I am having now is it is creating two change requests. One of the changes is a copy with the modifications I want added and the other is just a change with variables that are present in the record producer.

Here is my script in my record producer:



//COPY RECORDS FROM ORIGINAL PARENT NORMAL CHANGE REQUEST
var parentChange = producer.standard_change.toString();
var rec = new GlideRecord("change_request");
rec.get(parentChange);

copyChange();

function copyChange() {
var newChange = rec;
newChange.number = getNextObjNumberPadded(); //Get next change number
newChange.start_date = producer.start_date;
newChange.end_date = producer.end_date;
newChange.assigned_to = gs.getUser();
newChange.type = 'Standard';
newChange.u_rfc_state = 'Ready to Implement';
}


Any suggestions? How do I get this to only create the one change and not two? I tried changing the "newChange" to current, but that didn't seem to work at all.


You are accessing the old change perfectly, but the RP is creating a new change with number, etc. You don't need to build yet another "newChange". You should be doing this:



//COPY RECORDS FROM ORIGINAL PARENT NORMAL CHANGE REQUEST

//1. Get the parent
var parentChangeSysID = producer.standard_change.sys_id.toString();
var parentChange = new GlideRecord("change_request");
parentChange.get(parentChangeSysID);

//2. Get all of the fields from the original parent normal change
var fields = parentChange.getFields();

//3. Iterate through the fields and set the field on the new record (current) to the value from parent
for (var num = 0; num < fields.size(); num++) {
var field = fields.get(num);
if(!field.nil()) {
current[field.getName()] = field.toString();
}
}

//4. Override some of those fields that were copied from parent to make them what we want them to be
current.number = getNextObjNumberPadded(); //Get next change number
current.start_date = producer.start_date;
current.end_date = producer.end_date;
current.assigned_to = gs.getUser();
current.type = 'Standard';
current.u_rfc_state = 'Ready to Implement';