How to assign an user for approval by using script in workflow in run script

siddharth29
Mega Contributor

Hi,

can any one guide how to assign an approver by using script in work flow(run script)

I had written some code, pls check and tel me if am wrong

var desc = task.description;

var startDate = current.variables.colleague_start_date;

if(startDate != null && startDate != ''){

    task.description = "Colleague Start Date: " + startDate + "\n\n" + desc;  

}

  if(current.variables.u_emp_site == "u_hgstsz" && current.variables.u_items == "u_laptop")

  {

  current.sysapproval_approver.approver= "0526160bb4c6f500b47b0c26378dccee";

}

1 ACCEPTED SOLUTION

guhann
Mega Guru

1) Create a IF conditional activity to check the condition



answer = ifScript();


function ifScript() {


  if (current.variables.u_emp_site == "u_hgstsz" && current.variables.u_items == "u_laptop") {


              return 'yes';


    }


    return 'no';


}



2) Then connect the Yes flow to an Approval User activity in your workflow with below code in it to generate approval to a specific user.,


answer = [];


answer.push('0526160bb4c6f500b47b0c26378dccee'); // sys_id of the user.


View solution in original post

28 REPLIES 28

You cannot generate an approval like this. You gotta use Approval - User/Group activities in workflow to do that or else by initialize GlideRecord() for sysapproval_approver table via script.


var startDate = current.variables.colleague_start_date;


if(startDate != null && startDate != ''){


    task.description = "Colleague Start Date: " + startDate + "\n\n" + desc;


}


  if(current.variables.u_emp_site == "u_hgstsz" && current.variables.u_items == "u_laptop"){


  current.sysapproval_approver.approver= "0526160bb4c6f500b47b0c26378dccee";



}



this will not work in run script like this? if not can u pls brief me the above process ... bcoz i want to learn that also pls


Your workflow is configured for sc_req_item table. So the object 'current' will be having the reference of RITM record for which the workflow is running. And there is no field called sysapproval_approver available in sc_req_item table.


sysapproval_approver is table where the approval records for users gets stored with reference to the relevant task record. So if you want to generate an approval for a User/Group you have to use the workflow activities or GlideRecord() Insert() method via script.


no workflow is on ritm table....


pls tell me that sciprt syntax and where to write that script?


Can you tell me on which table you have configured the workflow you shown in the screenshots?



If you want to generate approval via a script see below. The code in bold will create approval record to the user. NOTE: Workflow will never wait for the approvals created via script or manually.



var gr = new GlideRecord('u_it_service_request_nrp');


gr.addQuery('u_emp_site',current.variables.u_hgstsz);


gr.addQuery('u_item',current.variables.u_laptop);


gr.query();



if(gr.next()) {


        var grApprover = new GlideRecord('sysapproval_approver');


        grApprover.initialize();


        grApprover.sysapproval = 'sys_id_of_the_task_record'; // In your case you must use current.sys_id


        grApprover.approver = 'sys_id_of_the_approver_user'; // In your case you must use gr.u_approver


        grApprover.state = 'requested';


        grApprover.insert();


}