- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2017 09:05 PM
Hi All,
I need help with this , i'm trying to copy approval details into task field
i tried with script include, but something missing here ,it is not creating a task after approval.
var AddApproverDateTime = Class.create();
AddApproverDateTime.prototype = {
initialize: function() {
},
addDateTime: function(id) {
var sys_updated_on =' ';
var gr= new GlideRecord('sc_req_item');
gr.get(id);
var apv = new GlideRecord('sysapproval_approver');
var qc=apv.addQuery('sysapproval',gr.getValue('request'));
qc.addOrCondition('sysapproval', gr.getValue('sys_id'));
apv.addQuery('sys_updated_on','!=',''); // add this to only find approvals that have comments on them
apv.query();
while (apv.next()) { // change if to while so it will go through all approvals with comments
DateTime += "\nApproval DateTime: from " + apv.approver.name + "\n" + apv.sys_updated_on; // add approver name here
}
return DateTime;
},
type: 'AddApproverDateTime'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2017 03:08 AM
Hi,
Follow the steps:
- Create a BR on sc_task table. Set when as 'after' and check 'advanced' and 'insert' options .
- Check the script to ensure, all the field names are populated as per your instance.
- Use the script below and paste it into 'advance script'.
- It is advised to test the script on non-production instances before implementing this modification
Script
(function executeRule(current, previous /*null when async*/) {
var msg1 = '';
var msg2 = '';
var app =[];
var count = 0;
getApprovalDetails();
gs.info(current.request_item.sys_id);
function getApprovalDetails() {
var thisTask = new GlideRecord('sysapproval_approver');
thisTask.addEncodedQuery('sysapproval='+current.request_item.sys_id);
thisTask.orderBy('sys_updated_on');
thisTask.query();
while(thisTask.next()){
count = count+1;
gs.info(thisTask.approver.getDisplayValue());
gs.info("Updated: "+thisTask.sys_updated_on.getDisplayValue());
updateFields(thisTask,count);
}
gs.info(count);
updateTask();
}
function updateFields(rec,count) {
if(count>1){
msg2 = "Approved by "+rec.approver.getDisplayValue() + " on "+ rec.sys_updated_on.getDisplayValue();
gs.info("MSG 2: "+msg2);
}
else {
msg1= "Approved by "+rec.approver.getDisplayValue() + " on "+ rec.sys_updated_on.getDisplayValue();
gs.info("MSG 1: "+msg1);
}
}
function updateTask() {
var task = new GlideRecord('sc_task');
task.addQuery('sys_id',current.sys_id);
task.query();
if(task.next()){
task.u_manager_s_approval.setValue(msg1);
task.u_marketing_ops_approval.setValue(msg2);
task.update();
}
}
})(current, previous);
The gs.info statements are to debug. You can remove them or keep them.
Please, see the image for the output.
Darshak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2017 12:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2017 12:32 AM
Ah! My bad, I was under impression that you have a total of two approvals, but I guess, you have three. Do one thing,change line 12 to
thisTask.addEncodedQuery('state=approved^sysapproval='+current.request_item.sys_id);
This will ensure the query on approval table returns records whose state is approved.
To check the code you need to trigger the BR again.
Darshak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2017 01:37 AM
Perfect!
Its working now ..Thanks for all your help Darshak.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 10:51 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 11:00 PM
Hi,
Trouble shoot in the similar way as we did before, if you've the MSG2 still in gs.info(). You can check the system logs to see what message is being picked up. And check the Approval record if the comment is displayed there. Since we are picking the journal entry, it should work.I see the time stamp for the comments is picked up, so look at your actual comment.
Darshak