How to pass an input parameter to a workflow triggered by a catalog item from script action

Milan13
Giga Expert

Hello,

I have a script action which is triggered by an event from a business rule.

Using this script action I "submit" a service catalog item which triggers a workflow and sets sc item variables.

What I need is to pass parameter(s) from the "current" object from the script action code in order to use them in the workflow to set up timer variable "answer".

Any idea how to do this?

Appreciate your help on this,

Milan

 

My script action code which set up sc catalog item variables and triggeres the workflow:

gs.log('Onboarding WF - D "variable" for Timer....' + event.parm1 + 'sys_id: ' + current.sys_id);


(function() {
var user_id = gs.getProperty('com.snc_onboarding_user_id');
//id of service catalog item...NOT a workflow id...
var workflow_id = gs.getProperty('com.snc_onboarding_workflow_id');
var email_suffix = gs.getMessage('conware-email');
var sd = gs.getMessage('New Hire');
var rec = new GlideRecord("u_test_milan_1");
rec.addQuery('sys_id','=', current.sys_id);
rec.query();
while (rec.next()) {
var cartId = gs.generateGUID();
var cart = new global.Cart(cartId,user_id);
var item = cart.addItem(workflow_id);
var personalEmail = getOpenForPE(rec.u_opened_for);
cart.setVariable(item,'first_name',rec.u_opened_for.first_name);
cart.setVariable(item,'last_name',rec.u_opened_for.last_name);
cart.setVariable(item,'email_address',rec.u_opened_for.user_name + email_suffix);
if (personalEmail) {
cart.setVariable(item,'personal_email',personalEmail);}
var rc = cart.placeOrder();
gs.info(rc.u_number);
var parentRequest = new GlideRecord('sc_request');
parentRequest.addQuery('number', rc.number);
parentRequest.query();
if (parentRequest.next()) {
parentRequest.setValue('requested_for', user_id);
parentRequest.setValue('short_description', sd + rec.opened_for.first_name + ' ' + rec.opened_for.last_name);
parentRequest.update();}
}
})();

function getOpenForPE(userid){
var userGR = new GlideRecord('sn_hr_core_profile');
if (userGR.get('user', userid)) {
return userGR.getValue('personal_email');
}
}

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So you are creating RITM from script; the workflow for this would be triggered as soon as record on RITM is inserted.

On which table the business rule is present? Is there any field on your RITM table which holds this value i.e. the table record on which the business rule is present

I would recommend to trigger the workflow from script if you want to pass variables

var wf = new Workflow();

var   wfId = wf.getWorkflowFromName("WorkflowInputTest");

var variableArr = {};

variableArr.u_comments = current.<fieldName>;

wf.startFlow(wfId, null, "WorkflowInputTest", variableArr);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

hello @Ankur Bawiskar ,

 

How to get the variable value variableArr in workflow , could you explain more into that.

 

i have requirement where need to send the notification to user's pervious manager on certain condition .

thinking to trigger the workflow from Business rule and pass the value of  previous manager to workflow. and reminder the same