The CreatorCon Call for Content is officially open! Get started here.

set field mandatory based on another fields' value in record producer

ammar_k
Tera Expert

Hello, 

 

i want to set a field to be mandatory depending on another field's value in the record producer.

the other field is a choice list.

 

i added this client script

 

function onChange(control, oldValue, newValue, isLoading) {
// Skip the script if the form is loading
if (isLoading) {
return;
}

// Get the new value of the case type field
var caseType = newValue;

// Define the field you want to make mandatory
var fieldName = 'u_component_group';

// Define the case types for which the field should NOT be mandatory
var nonMandatoryCaseTypes = ['access_request', 'intervention', 'review']; // Specified non-mandatory case types

// Check if the case type is in the list of non-mandatory case types
if (nonMandatoryCaseTypes.indexOf(caseType) === -1) {
// Make the field mandatory if the case type is not in the non-mandatory list
g_form.setMandatory(fieldName, true);
} else {
// Make the field not mandatory if the case type is in the non-mandatory list
g_form.setMandatory(fieldName, false);
}
}
 
but it doesn't work 😞
 
can some one help me please
4 REPLIES 4

Satishkumar B
Giga Sage
Giga Sage

Hi @ammar_k 
Can you try using UI Policy. please refer below link:
https://www.servicenow.com/community/developer-forum/how-to-make-a-field-mandatory-based-on-a-choice...

also can you share me the screenshot of your client script condition

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand. 



swathisarang98
Giga Sage

Hi @ammar_k,

 

I tried your code in my pdi its working properly, can you please share some snaps of you onchange ? and put alert for caseType and share the screenshot of alert?

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Any specific reason you are trying this with a Client Script? Have you considered using a UI Policy without using any code?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Abhit
Tera Guru

@ammar_k  - You can try this with UI policy.