How do we call Client script from UI Action

saurabhsharma
Giga Contributor

Hi 

How do we call Client script from UI Action. Please help me in understanding this.

Thanks,

Saurabh

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Options below, with limitations

1) Place a function in an onload script (only works in global scope)

Client Script

function onLoad() {

}


// You can call this in UI Actions
// Only works in global scope (does not work in scoped apps)
// Will only work for UI Action of the same table
function yourFunctionHere() {
// Code here
}

UI Action

yourFunctionHere();

2) Write a UI Script

UI Script yourFunctionHere

// This will be callable from all client scripts form any table
// If global is not ticked, you must use ScriptLoader API

function yourFunctionHere() {
// Code here
}

UI Action

//If global is not ticked on UI Script
getScripts("yourFunctionHere", runScripts)

function runScripts() {

yourFunctionHere();

}

//If global is ticked on UI Script
yourFunctionHere();

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

Abhinay Erra
Giga Sage

You can check client check box and define onclick function for your ui action

Prateek kumar
Mega Sage

 Take a look at this

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

The SN Nerd
Giga Sage
Giga Sage

Options below, with limitations

1) Place a function in an onload script (only works in global scope)

Client Script

function onLoad() {

}


// You can call this in UI Actions
// Only works in global scope (does not work in scoped apps)
// Will only work for UI Action of the same table
function yourFunctionHere() {
// Code here
}

UI Action

yourFunctionHere();

2) Write a UI Script

UI Script yourFunctionHere

// This will be callable from all client scripts form any table
// If global is not ticked, you must use ScriptLoader API

function yourFunctionHere() {
// Code here
}

UI Action

//If global is not ticked on UI Script
getScripts("yourFunctionHere", runScripts)

function runScripts() {

yourFunctionHere();

}

//If global is ticked on UI Script
yourFunctionHere();

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi,

I am trying to automate and run the UI Action "Refresh Impacted Services" [action name ="refresh_impacted_services"] in the Change form on Save (Load).

I have tried your code but its not working. 

Firstly I'm not a developer (so its find it on google and give it a go....)

so the bit "Code here" in your script, am I supposed to write some code to make this work?

 

function onLoad() {

}
function refresh_impacted_services() {
// Code here << do I need anything here??

}

 

If no code required, then any idea why its not working?

 

Thanks
Nigel