Help with code

B_pppppp
Tera Contributor

I try to check if this reference field is null (has no values to choose from) - not only empty. 

11 REPLIES 11

@B_pppppp 

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

Community Alums
Not applicable

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 

@Community Alums It says "here" and the 'fieldObj="" '

Community Alums
Not applicable

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

SarthakKashya2_0-1713428648018.png

 

When Business Service is not empty 

SarthakKashya2_1-1713428698945.png

It makes it mandatory 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

 

 

 

Robbie
Kilo Patron
Kilo Patron

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