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

jobin1
Tera Expert

@Ankur Bawiskar 

@shloke04 

 Any idea?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you receiving the user_name from external system?

if yes then you can query in sys_user for that user and get the language

what's the challenge faced?

Regards
Ankur

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

@Ankur Bawiskar No. its new user profile creation setup they need to pass all fields in pay load we are validating and creating user in ServiceNow.

 

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"
};
}

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