How to copy approver name, approved datetime and approval status into a field in task

omsa1
Kilo Guru

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'  

};  

1 ACCEPTED SOLUTION

Rama Chandra D
Kilo Guru

Hi,



Follow the steps:



  1. Create a BR on sc_task table.   Set when as 'after' and check 'advanced' and 'insert' options .
  2. Check the script to ensure, all the field names are populated as per your instance.
  3. Use the script below and paste it into 'advance script'.
  4. 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


View solution in original post

24 REPLIES 24

Brilliant!!



Its working as expected. Thanks you so much!



I have one more question, i'm trying to print the approval comment as well to the fields.I've modified the script as below, but in the field it only copy the first comment...what did i missed here?



  function updateFields(rec,count) {  


  if(count>1){  


  msg2 = " Approved by "+ rec.approver.getDisplayValue() + " on "+ rec.sys_updated_on.getDisplayValue() + " with comments : " + rec.comments.getJournalEntry(-1);  


  gs.info("MSG 2: "+msg2);  


  }  


  else {  


  msg1= "Approved by "+rec.approver.getDisplayValue() + " on "+ rec.sys_updated_on.getDisplayValue()+ " with comments : " + rec.comments.getJournalEntry(-1);  


  gs.info("MSG 1: "+msg1);  



find_real_file.png


I'm not sure. Let me see.Just check the maximum length of the new fields.Does your record has more than one comment in journal ? Please mark the reply as correct if it help you to resolve your issue.



Darshak


It is working, my guess is that the   max field length of the ring field you've defined for the manger and manager ops is not sufficient.



I changed the code like this,


msg2 = "Approved by "+rec.approver.getDisplayValue() + " on "+ rec.sys_updated_on.getDisplayValue()+ " with comments "+"\n"+ rec.comments.getJournalEntry(-1);


Use similar line for msg1 as well. Please look at the screenshot.



Darshak


Hi Darshak,



No its not showing the Joan/sharma 's approval comment with the script above.


Can you share a screenshot of Joan/Sharma's approval record? I wonder what might be going wrong.



Darshak.