OnLoad Client script help to setup condition

rishabh31
Mega Sage

Hello Community members,

I am learning scripting in ServiceNow & stuck in setting condition to check whether the 'Caller (caller_id)' of respective incident is 'VIP' or Not, if 'VIP' then display a field msg. on 'Form View of Agent Workspace'. if false (i.e., Not VIP) then no need to Display the VIP field msg.

For this, created an onload CS on Incident table, View- workspace (Global-false), UI type- All, Isolated script- check, 

 

Script- 

 

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    g_form.showFieldMsg('caller_id','VIP','info');
    }

 

 
Unable to find the correct condition which checks the caller of the respective incident in Agent workspace Form view is 'VIP' or not. I tried OOB on change the Client script condition to check VIP status but it did not work. Thus currently it shows VIP info msg for all the callers in form view for Agent workspace (screenshot attached)
 
Requesting community members to help me with the condition so that 'VIP' field msg. gets displayed to only VIP callers of Incidents not everyone (currently its displaying VIP for every users).
 
Will mark the solution as 'Helpful' once the same is resolved.
 
Requesting support, thanks
 
 
1 ACCEPTED SOLUTION

kamlesh kjmar
Mega Sage
Mega Sage

Hi @rishabh31 ,

 

Try the below script. This should server your purpose:

 

 

g_form.getReference("caller_id",checkIfVIP);
function checkIfVIP(user){
    if(user.vip == "true")
    g_form.showFieldMsg("caller_id","This user is a VIP user","info") //Change your field msg content
    else
        g_form.hideFieldMsg("caller_id")
}

 

 

I hope this helps.

 

Please hit Helpful  if this helps you and Accept the response if this solves your issue.

 

Regards,

Kamlesh

View solution in original post

5 REPLIES 5

Basheer
Mega Sage

Hi @rishabh31 ,

do like this

 

var caller = g_form.getValue("caller_id");
if(caller == "VIP"){
g_form.showFieldMsg('caller_id','VIP','info');
}
else
{
g_form.showFieldMsg('caller_id','Not VIP','info');
}

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Amit Pandey
Kilo Sage

Hi Rishabh,

 

Please try this-

 

function onLoad() {
// Get the value of the caller_id field
var caller_id = g_form.getValue('caller_id');

// Check if the caller_id is equal to 'VIP'
if (caller_id === 'VIP') {
// Show the field message if the caller_id is 'VIP'
g_form.showFieldMsg('caller_id','VIP','info');
}
}

 

In this script, the g_form.getValue('caller_id') method is used to get the value of the caller_id field. The value is then compared to the string 'VIP' in the if statement to determine whether the Caller is a VIP or not. If the Caller is a VIP, the g_form.showFieldMsg('caller_id','VIP','info') method is used to display the field message on the form.

kamlesh kjmar
Mega Sage
Mega Sage

Hi @rishabh31 ,

 

Try the below script. This should server your purpose:

 

 

g_form.getReference("caller_id",checkIfVIP);
function checkIfVIP(user){
    if(user.vip == "true")
    g_form.showFieldMsg("caller_id","This user is a VIP user","info") //Change your field msg content
    else
        g_form.hideFieldMsg("caller_id")
}

 

 

I hope this helps.

 

Please hit Helpful  if this helps you and Accept the response if this solves your issue.

 

Regards,

Kamlesh

@kamlesh kjmar , Thanks Sir🙂, It works fine, just applied a semicolon on g_form.showFieldMsg & g_form.hideFieldMsg.

@Amit Pandey @kamlesh kjmar @Basheer @shyamkumar VK Sir, to apply the background colour on the caller field for List view of agent workspace, created another post by following below URL, requesting you all to please have a look at this URL for details & help me to get this done, shall be highly grateful to you all.

https://www.servicenow.com/community/developer-forum/onload-client-script-help-to-display-background...

 

 

This ask is solved so Marked as Accepted solution & Helpful! Thanks once again