Triggering workflow from UI action

pritimayee
Tera Contributor

Hello,

Can anyone please help me on this below requirment.

I have created a new state as Draft for change request.Created new ui action'submit request'.this button will be visible on all type change request like emergency standard and normal and the when the state is in draft.

Once click on the ui action 'submit request' the normal workflow that has been defined for all the type request will start.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can use this script to trigger workflow from script

Remember UI Action should be server side

triggerWorkflow();

function triggerWorkflow(){

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName('give the workflow name'), current, 'update');

}

I would insist if possible make the workflow trigger based on the proper condition

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Hi,

so you want to trigger which workflow?

are you saying based on the change type you want to trigger that particular workflow?

if yes then try this

Ensure you give valid choice values and valid workflow names

triggerWorkflow();

function triggerWorkflow(){

var type = current.type;

var workflowName = '';

if(type == 'emergency'){
    workflowName = 'emergencyWorkflowName';
}
if(type == 'normal'){
    workflowName = 'normalWorkflowName';
}
if(type == 'standard'){
    workflowName = 'standardWorkflow';
}

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName(workflowName), current, 'update');

}

Regards
Ankur

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

Hello  Ankur,

 

I created the UI action for triggering the  workflow using the above script .But I am not able to trigger . when I clicked the button its getting refreshed. And its showing the error like first select the table .So please help me .

PFA screenshots.

Your UI action should be server side

Client checkbox - uncheck

Onclick - remove function

Script - keep as it is

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

Hello Ankur,

 

It is triggering the workflow.If i am saving the record instead of clicking the UI action'submit the request' the state is getting changed to new.After saving the record it should stay in draft state.

triggerWorkflow();

function triggerWorkflow(){

var type = current.type;

var workflowName = '';

if(type == 'Emergency'){
workflowName = 'Emergency Change';
}
if(type == 'normal'){
workflowName = 'normalChange';
}
if(type == 'standard'){
workflowName = 'Standard Change';
}

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName(workflowName), current, 'update');

}
if(typeof window == 'undefined')
saveRecordAndReload();

function saveRecordAndReload() {
current.state==-7;//draft state value
current.update();
action.setRedirectURL(current);
}

But this is not working to save the record in draft state.Can you please let me know where i am doing mistake.

@pritimayee1@gmail

I assume you want once UI action is clicked the workflow should trigger and state should change to -7

please update code as below

triggerWorkflow();

function triggerWorkflow(){

var type = current.type;

var workflowName = '';

if(type == 'Emergency'){
workflowName = 'Emergency Change';
}
if(type == 'normal'){
workflowName = 'normalChange';
}
if(type == 'standard'){
workflowName = 'Standard Change';
}

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName(workflowName), current, 'update');


current.state = -7;//draft state value

current.update();

action.setRedirectURL(current);

}


Regards
Ankur

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