Help with code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 12:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:24 AM
To ensure that the field is only set as mandatory when there are options available to choose from, you can modify the script to first check if there are options in the reference field
function onLoad() {
// Get a reference to the business_service field
var fieldObj = g_form.getControl('business_service');
// Check if control exists and if there are options available
if (fieldObj && fieldObj.options.length > 0) {
// Check if the field is empty
if (g_form.getValue('business_service') == '') {
// Set business_service field to mandatory
g_form.setMandatory('business_service', true);
}
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:13 AM
Hi @B_pppppp ,
Can you please try below code.
function onLoad() {
//Type appropriate comment here, and begin script below
alert("here");
var fieldObj = g_form.getValue('business_service');
alert('fieldObj = ' + JSON.stringify(fieldObj));
// Check if control exists and there's more than one option
if (fieldObj) {
alert('Inside if ');
// Set business_service field to mandatory
g_form.setMandatory('business_service', true);
}
}
Please mark my answer correct and helpful if this works for you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:20 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:25 AM
Hi @B_pppppp ,
You need to add some value in field i.e., business_service
Please check image I'm uploading below
When business Service is empty
When Business Service is not empty
It makes it mandatory
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:23 AM
Hi @B_pppppp,
Can I ask what you're trying to achieve with the getControl method?
As I see it (granted, based on the high-level question), you simply want to make the field mandatory if the field is empty? Is that correct?
If that's the case, just make the field mandatory via a UI Policy (Best practice) or an onChange Script for example, as a form can't be submitted if it's mandatory and empty.
This is where I'm struggling as to why you're using the getControl method to simply do this. It's not needed - unless I'm missing something.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie