- 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:43 AM
Hi Andy,
OOB by default when the record is created it will be in "Not yet requested". Based on the workflow kick off condition you have to alter the values.
Please let me know if you have any questions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 09:59 AM
That's my problem. If I clone/copy a previous Change (in any state) it creates the new Change Request and automatically sets it to an approval status of "Requested" and fires off for approvals. I'm not sure how to change the behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 01:02 PM
^^ this isn't true that I can tell. It *IS* the case for NEW change records but NOT for copied/cloned change records. Can I script the approval to "Not Yet Approved" as it creates the record? It appears that the copy is creating the record and basically hitting the "Request Approval" button also because my approval workflow starts right away when the new record is created.

- 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-24-2015 05:18 AM
^^ this... simple enough. I checked the properties of the workflow and sure enough the only condition was that the type is comprehensive. I added the other step and bam... working.
Problem now... the "Request Approval" button is now missing on my cloned Change Requests. If I create a NEW Change Request it's there but the cloned Change Request has the button missing altogether.