- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 08:04 AM
I have searched and am struggling to find the right answer.
I'd like to add an Insert and Stay function to my Change Records so that somebody can copy a previous Change Request to save time of filling out the form again. I created a UI Action called "Insert and Stay" for consistency so that it looks similar to what the other records look like for Request, Incident and Problem. I have tried several people's code and have been unsuccessful at getting something to work. The end result I'm looking for is to have a new record in a "Not Yet Requested" state and for the record to save/submit... NOT to request approvals. Seems that I can only get the new record saved by requesting approvals, though. Because I couldn't figure this out I tried several other people's code to see if I could get a variation to work... nope... the change record always requests approval. Any ideas??? The approach altogether could be wrong so I'm open to suggestions. Thanks!
Code I'm using.........
// Add all fields you do not want to copy here
var strNoCopy = 'number; state; u_state_text; conflict_status; opened_at; outside_maintenance_schedule; sys_updated_by; sys_updated_on; opened_by; sys_created_by; approval_set; sys_created_on; phase; u_approval_resetl closed_at; approval; sys_mod_count; phase_state; u_total_time_worked; sys_class_namel stage; active; work_notes; comments; parent; change_request';
// set the glide records for the old and new change
var ocrgr = new GlideRecord('change_request');
var ncrgr = new GlideRecord('change_request');
// get the old change details
- ocrgr.get(current.sys_id);
// getFields() returns a Java ArrayList
var fields = ocrgr.getFields();
// Enumerate GlideElements in the GlideRecord object that have values
for (var i = 0; i < fields.size(); i++) {
var glideElement = fields.get(i);
// see if the element has a value and ensure it is not in the strNoCopy field. If not, add the value to the field on the
// new record
if (glideElement.hasValue() && strNoCopy.toLowerCase().indexOf(glideElement.getName().toLowerCase()) == -1) {
ncrgr.setValue(glideElement.getName(), glideElement);
}
}
// Update the record
var sysID = ncrgr.insert();
//current.rfc = sysID;
var mySysID = current.update();
// set the glide records for the old and new change tasks
var octgr = new GlideRecord('change_task');
var nctgr = '';
// query for all all tasks associate with the old change
- octgr.addQuery('parent', current.sys_id);
- octgr.query();
// for each task found, create a new change task record
while (octgr.next())
{
// Create new glide record for each new task
nctgr = new GlideRecord('change_task');
// Get fields from change task
var fields = octgr.getFields();
// Enumerate GlideElements in the GlideRecord object that have values
for (var i = 0; i < fields.size(); i++) {
var glideElement = fields.get(i);
// see if the element has a value and ensure it is not in the strNoCopy field. If not, add the value to the field on the
// new record
if (glideElement.hasValue() && strNoCopy.toLowerCase().indexOf(glideElement.getName().toLowerCase()) == -1) {
nctgr.setValue(glideElement.getName(), glideElement);
}
}
//Make sure the new change task is associated to the new Change record
nctgr.setValue('parent',ncrgr.sys_id);
nctgr.setValue('change_request',ncrgr.sys_id);
// Insert new change task record
var sysID = nctgr.insert();
}
- current.approval = 'not_requested';
- gs.addInfoMessage("Change " + ncrgr.number + " created");
- action.setRedirectURL(ncrgr);
- action.setReturnURL(current);
Solved! Go to Solution.
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 05:03 PM
The default value on the Approval field should be 'not requested'. As long as your UI action doesn't contain the approval field, meaning you are copying the existing field state to the new change, the Approval state should be not requested.
However, in your case, made your workflow is triggering the issue? What are your conditions on your workflow? If you just change type = x, the workflow will trigger:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 08:06 AM
Hi Andy,
Please refer the below thread and let me know if you are blocked.
Can you clone/copy a change request?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 08:29 AM
I just found that link about 5 min ago, thanks Pradeep. So far that appears to work but the record still automatically requests approval. I think I need to know how to set the approval status to "Not Yet Requested" and then modify my workflow so that it doesn't advance until the "Request Approval" button is pressed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 08:35 AM
You are correct Andy. Let me know how it goes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 08:41 AM
Let me know if you have any tips... this is my first attempt at this. The first step in my approval process sets the approval to "approved." What should my steps be to set the status to "Not Yet Approved" waiting on the "Request Approvals" button to be clicked (or for the status to change manually)?