Create Reference Qualifier filter with if-else statement

Dan Loza
Tera Contributor

Hello,

 

I'm trying to create a reference qualifier that will filter based on the user's location such that if the location is anywhere in the US (location starts with US) then it will go to a particular US location. For example, if the user's location is USSF or USTX or USNY, the filter will show US.  Any other location, like FR or UK will show the user's location.

 

Here's the ref qual filter that I created but it's not working.

 

javascript: var ret= '';if (current.variables.requested_for.locationSTARTSWITHUS) ret += '^location=US'; else ret += '^current.variables.requested_for.location'; ret;

 

Any help is appreciated.

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Dan Loza 

are you applying ref qualifier on field referring to location table?

If yes then use this

javascript: var ret; if (current.requested_for.location.toString().startsWith('US')) ret = 'name=US'; else ret = 'sys_id='+ current.requested_for.location; ret;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar I'm referring to the location under alm_hardware. I tried the abovementioned script you gave, now it's coming up empty.

@Dan Loza 

are you sure alm_hardware table has requested_for field?

Because out of the box it's not there. see below image

ensure you use the correct field which refers to user table

AnkurBawiskar_0-1676980623073.png

 

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

Hi, @Ankur Bawiskar 

 

Sorry for the confusion, I'm using this on a Service Request where users request for hardware assets. Basically, if a user requests for a hardware asset, for example, a laptop, and that user is in USNY for example, then it will pull the asset from one US location. If a user is outside US, it will check the user's location (current.variables.requested_for.location) and pull the stock from the user's location. Sorry my initial script was missing the "variables". 

 

But this one below comes up empty:

 

javascript: var ret; if (current.variables.requested_for.location.toString().startsWith('US')) ret = 'name=US'; else ret = 'sys_id='+ current.variables.requested_for.location; ret;