- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2015 11:28 AM
I have an existing UI Action which copies a case (part of a custom app) to a new record. A number of fields from the original case are written to the new one.
There is a request to also copy over the work notes (work_notes) journal field. I've found / tweaked code to do that but the entries are not being copied in date order. The code I've found to sort an array is not doing anything.
If someone could point out what's wrong with this code or how it should look to sort the journal entires - newest at the top - I'd very much appreciate the help.
var notes = current.work_notes.getJournalEntry(-1); //gets all journal entries as a string where each entry is delimited by '\n\n'
notes.sort(function(a, b){
return a.sys_created_on-b.sys_created_on
})
var na = notes.split("\n\n"); //stores each entry into an array of strings
for (var i = 0; i < na.length; i++){
gs.print(na[i]);
gr.work_notes = na[i];
};
var msg = (gs.getMessage('copy.case'));
gr.work_notes = msg + current.number;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 11:41 AM
Popping in to post the resolution to this issue.
The issue turned out to be with the date-time stamps on the journal entries on the newly copied entries - the speed of SN is such that multiple entries are created with the same date/time stamp and so appear in a random order.
I wound up resetting the date/time stamp to that of the original journal entry, preserving the original date/time stamp and the original order.
------
function runMsgValidation(){
var msgCon = getMessage('copy.case.receiveddate.message');
var con = alert(getMessage('copy.case.receiveddate.message'));
if(con){
return true; //Abort submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'Copy_Case_Action'); //MUST call the 'Action name' set in this UI //Action
}
current.update();
doInsertAndStay();
function doInsertAndStay() {
var gr = new GlideRecord('u_case');
gr.initialize();
gr.u_request_from = current.u_request_from;
//gr.u_received = current.u_received;
gr.u_received = nowDateTime();
gr.short_description = current.short_description;
gr.description = current.description;
gr.u_risk_type = current.u_risk_type;
gr.u_product_type = current.u_product_type;
gr.u_product_sub_type = current.u_product_sub_type;
gr.u_region = current.u_region;
gr.u_brokerage=current.u_brokerage.sys_id;
gr.u_source = current.u_source;
gr.u_sub_source = current.u_sub_source;
gr.u_language = current.u_language;
gr.u_line_of_business = current.u_line_of_business;
gr.u_policy_prem = current.u_policy_prem;
gr.u_copied_case = current.sys_id;
gr.u_name_insured = current.u_name_insured;
gr.assigned_to = gs.getUserID();
//new
gr.insert();
var grJ = new GlideRecord('sys_journal_field');
grJ.addQuery('element_id', current.sys_id);
grJ.orderByDes('sys_created_on');
grJ.query();
while(grJ.next()){
var grJN = new GlideRecord('sys_journal_field');
grJN.initialize();
grJN.name = grJ.name;
grJN.element_id = gr.sys_id;
grJN.value = grJ.value;
grJN.element = grJ.element;
grJN.insert();
grJN.sys_created_on = grJ.sys_created_on;
grJN.sys_created_by = grJ.sys_created_by;
grJN.update();
}
//end new
var msg = (gs.getMessage('copy.case'));
gr.work_notes = msg + current.number;
gr.u_entity = 'u_case';
gr.setWorkflow(false);
// gr.insert();
gr.update();
action.setRedirectURL(gr);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2015 01:26 PM
would it be possible that you are missing a semicolon after the notes.sort section?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2015 05:51 AM
I tried adding a semicolon:
notes.sort(function(a, b){
return a.sys_created_on-b.sys_created_on
}); <<RIGHT HERE
But that didn't make a difference. The syntax checked out and the code compiled either way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 11:41 AM
Popping in to post the resolution to this issue.
The issue turned out to be with the date-time stamps on the journal entries on the newly copied entries - the speed of SN is such that multiple entries are created with the same date/time stamp and so appear in a random order.
I wound up resetting the date/time stamp to that of the original journal entry, preserving the original date/time stamp and the original order.
------
function runMsgValidation(){
var msgCon = getMessage('copy.case.receiveddate.message');
var con = alert(getMessage('copy.case.receiveddate.message'));
if(con){
return true; //Abort submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'Copy_Case_Action'); //MUST call the 'Action name' set in this UI //Action
}
current.update();
doInsertAndStay();
function doInsertAndStay() {
var gr = new GlideRecord('u_case');
gr.initialize();
gr.u_request_from = current.u_request_from;
//gr.u_received = current.u_received;
gr.u_received = nowDateTime();
gr.short_description = current.short_description;
gr.description = current.description;
gr.u_risk_type = current.u_risk_type;
gr.u_product_type = current.u_product_type;
gr.u_product_sub_type = current.u_product_sub_type;
gr.u_region = current.u_region;
gr.u_brokerage=current.u_brokerage.sys_id;
gr.u_source = current.u_source;
gr.u_sub_source = current.u_sub_source;
gr.u_language = current.u_language;
gr.u_line_of_business = current.u_line_of_business;
gr.u_policy_prem = current.u_policy_prem;
gr.u_copied_case = current.sys_id;
gr.u_name_insured = current.u_name_insured;
gr.assigned_to = gs.getUserID();
//new
gr.insert();
var grJ = new GlideRecord('sys_journal_field');
grJ.addQuery('element_id', current.sys_id);
grJ.orderByDes('sys_created_on');
grJ.query();
while(grJ.next()){
var grJN = new GlideRecord('sys_journal_field');
grJN.initialize();
grJN.name = grJ.name;
grJN.element_id = gr.sys_id;
grJN.value = grJ.value;
grJN.element = grJ.element;
grJN.insert();
grJN.sys_created_on = grJ.sys_created_on;
grJN.sys_created_by = grJ.sys_created_by;
grJN.update();
}
//end new
var msg = (gs.getMessage('copy.case'));
gr.work_notes = msg + current.number;
gr.u_entity = 'u_case';
gr.setWorkflow(false);
// gr.insert();
gr.update();
action.setRedirectURL(gr);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2019 03:07 AM
Hi Sue,
I've copied your script but the entries don't show and I think it;s down to what
Having said that, the new journal entries don't appear in the sys_journal_field table either. I'm currently testing to see if this changes when a history line record is created.
Here is my total UI Action script which is designed to convert an incident into a service request at the click of a button:
(function(current, previous, gs, action) {
current.update();
var request = new GlideRecord("sc_request");
request.short_description = current.short_description;
request.requested_for = current.caller_id;
request.description = current.description;
request.location = current.location;
request.company = current.company;
request.cmdb_ci = current.cmdb_ci;
request.sys_domain = current.sys_domain;
request.u_caller_id = current.caller_id;
request.u_category = current.category;
request.u_subcategory = current.subcategory;
request.assignment_group = current.assignment_group;
request.assigned_to = current.assigned_to;
request.u_record_type = "request";
//next section queries the sys journal table for records that match the incident sys_id. Then is places them with associated details into the resultant Service Request
var journals = new GlideRecord('sys_journal_field');
journals.addQuery('element_id',current.sys_id);
journals.orderByDes('sys_created_on');
journals.query();
while (journals.next()){
var newJournals = new GlideRecord('sys_journal_field');
newJournals.initialize();
newJournals.name = journals.name;
newJournals.element_id = request.sys_id;
newJournals.value = journals.value;
newJournals.element = journals.element;
newJournals.insert();
newJournals.sys_created_on = journals.sys_created_on;
newJournals.sys_created_by = journals.sys_created_by;
newJournals.update();
}
request.work_notes = ("Request converted from " + current.number);
var sysID = request.insert();
current.request_id = sysID;
var mySysID = current.update();
gs.addInfoMessage(gs.getMessage("Request {0} created",request.number));
gs.eventQueue('incident.convert', current, request.number);
action.setRedirectURL(request);
action.setReturnURL(current);
current.work_notes = (gs.getMessage("Converted to Request {0}",request.number));
current.close_code = "Incident Cancelled";
current.close_notes = (gs.getMessage("Converted to Request {0}",request.number));
current.state = 8;
current.u_cancelled_reason = (gs.getMessage("Converted to Request {0}",request.number));
current.update();
})(current, previous, gs, action);