how to validate dropdown /choices in Scripted REST Resource.

jobin1
Tera Expert

Hi All

 

I need to validate the languages in user table while end user is sending data through payload. The similar kind of validation I did for company and its working fine but how we can give the same for a drop down choice?

sample company filed validation->

var user_company = body['Company'].toString().trim();
if (user_company == '' || user_company == null || user_company == undefined)
return {
"Status": "Enter the Company"
};

var company = new GlideRecord('core_company');
company.addQuery('name', user_company);
company.query();
if (company.next()) {
user.company = company.getUniqueValue();
} else {
return {
"Status": "Invalid company"
};
}

 

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

if it's a creation then against what you wish to perform the validation?

are you saying you wish to check the sys_choice table and check value they pass is present in choice list or not?

in that case you can query sys_choice with element - preferred_language

name=sys_user^element=preferred_language

find_real_file.png

Regards
Ankur

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

View solution in original post

9 REPLIES 9

@Ankur Bawiskar 

like this?

var lang = new GlideRecord('sys_choice');
lang.addQuery('name', 'sys_user');
lang.addQuery('element', 'preferred_language');
lang.query();
if (lang.next()) {
user.lang = lang.getUniqueValue();
} else {
return {
"Status": "Invalid language"
};
}

yes

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

@jobin 

Thank you for marking my response as helpful.

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

Regards
Ankur

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

@Ankur Bawiskar  Done.

Snehangshu Sark
Mega Guru

Hi,

 

The first question would be - are you getting the user_name in the payload, if yes, then you can run GlideRecord on "sys_user" table and validate it.

 

user.preferred_language =<language>;

Please elaborate the issue

 

Regards,

Snehangshu Sarkar