Show field message dynamic for two catalog items

Sam198
Mega Guru

Hi all,

I have two catalog item that uses a variable set, the requirement on this variable set one variable to show two different message - I will need one Catalog item to show this message -   g_form.showFieldMsg('asset_in_stock',"... Asset in Stock Message 1",'info'); and the other catalog item to show separate message -   g_form.showFieldMsg('asset_in_stock',"... Asset in Stock Message 2",'info');

 

Is this possible?

Thanks in advance.

1 ACCEPTED SOLUTION

Hi Sam,

you need to use the syntax properly; updated script below: highlighted in bold you need to compare the variable each time

function onLoad() {
//Type appropriate comment here, and begin script below
var catalogItemSysId = g_form.getUniqueValue();
if(catalogItemSysId == 'sys_id' || catalogItemSysId == 'sys_id'|| catalogItemSysId == 'sys_id'|| catalogItemSysId == 'sys_id'){
g_form.showFieldMsg('asset_in_stock',"Asset in Stock Message 1",'info');

}
else if(catalogItemSysId == 'sys_id'){
g_form.showFieldMsg('asset_in_stock',"Asset not in Stock Message 2",'info');

}

}

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

View solution in original post

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could use g_form.getUniqueValue(). This will give you the sys_id of the current Catalog Item. With this, you could easily create an if statement.

if contains sys_id X then message ..., if contains sys_id Y then message ...

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sam,

 

create an onload catalog client script

function onLoad(){

var catalogItemSysId = g_form.getUniqueValue();

if(catalogItemSysId == '<yourCatalogItemSysId1>'){

g_form.showFieldMsg('asset_in_stock',"... Asset in Stock Message 1",'info'); 

}

else if(catalogItemSysId == '<yourCatalogItemSysId2>'){

g_form.showFieldMsg('asset_in_stock',

}

}

It is not good practice to hard code sys Ids in script so recommended approach

1) create 2 onload client scripts 1 for catalog item 1 and other for catalog item 2

2) then show respective field messages

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

Hi Sam,

 

Just to add with Ankur. It is always recommendable to create separate Catalog Client Script on the Item level  rather than on the Variable set level until unless you have a common validation. If you go for a single script on the variable set level, you will end up modifying the same again and again if any further enhancements comes tomorrow. Using different scripts on the item level has its own benefits, such as:

  • You can handle all the complex validations based on the Item. For example, if you need to check any other variable value (which is on item level) for the validation, it will be uncomplicated. 
  • Tomorrow if any further logic needs to be leveraged with your existing script, it would be less hectic.
  • It is always easy maintenance from operational perspective.

 

Hope the above gives you the needed information in order to decide the correct approach in your requirement.

 

Hope this helps. Please mark the answer Correct/Helpful based on the impact.

Regards,

Amlan

Thanks Ankur for the advise, however, the requirement is changed now, this variable set is now used on 5 catalog item, and now we want 1 catalog item to show message 2 and all the other 4 to show message 1. But the message 2 does not show up on this 1 catalog item (Edit: it shows message 1 on all 5 catalog items) - should i do separate script on the catalog level - I will  have to create 5 different ones now should not be one better in this situation? or i was looking at the switch statements but did not get it - your above script was more easier for me to understand.

 

function onLoad() {
//Type appropriate comment here, and begin script below
var catalogItemSysId = g_form.getUniqueValue();
if(catalogItemSysId == 'sys_id'||'sys_id'||'sys_id'||'sys_id'){
g_form.showFieldMsg('asset_in_stock',"Asset in Stock Message 1",'info');

}
else if(catalogItemSysId == 'sys_id'){
g_form.showFieldMsg('asset_in_stock',"Asset not in Stock Message 2",'info');

}

}