Client Script - Loop - setMandatory - Problem

poyntzj
Kilo Sage

Looking to modify some work that has been done here to make the user experience a little better.

Firstly, on this record we want the user to be able to Save the record with no checks.   If the user Submits, then we check for specific fields to be filled in.

The way it has been done via UI Actions works, but when the user submits there are no mandatory field markers and this is what needs addressing.

OK, create a client script and set the fields to be mandatory - easy ?   yeah ?   - Nope.

I have the following Client Script that checks for the action name, sets a boolean field marker to be true / false and then using an array, loop through all the fields that need checking. - the array has been truncated and some debugging added

function onSubmit() {

   

var action = g_form.getActionName();

g_form.addInfoMessage(action);

var bolMand = false;

if (action == 'submit_tpip')

        bolMand = true;

var arrFields = ['u_project_manager','u_engineer'];

g_form.addInfoMessage(arrFields.length)

for (i=0;i<arrFields.length;i++)

{

        g_form.addInfoMessage(arrFields[i] + ' : ' + i);

        g_form.setMandatory(arrFields[i], bolMand);

        if(i == arrFields.length-1)

        g_form.addInfoMessage('Breaking');

}

when I click on save, I see this

find_real_file.png

which is fine

when I click on Submit, I see this......

find_real_file.png

So, Why does it do the first field correctly and get suck on the second ?

Why is it not even reaching the if statement to show the "Breaking" info message ?

It is not the field as it is happening on whatever subset of the array I am chosing, and in this screenshot I have just reversed the two fields from the previous code

find_real_file.png

Ontop of that, I did try using true instead of bolMand and it gives the same problem

Bizzarely, the following code works so it is clearly to do with this running in a loop

      g_form.setMandatory('u_engineer',bolMand);

      g_form.setMandatory('u_project_manager',bolMand);

      g_form.addInfoMessage('and here I am');

any ideas or thoughts ?

1 ACCEPTED SOLUTION

Carpenter
Tera Contributor

Hi Julian,



Could there possibly be a scoping issue with your variable 'i' you are using for your loop that may be causing the unexpected behavior?



You may try changing your for statement to:



for (var i=0;i<arrFields.length;i++)  


View solution in original post

9 REPLIES 9

Deepak Ingale1
Mega Sage

Hi Julian,



I dont find anything wrong with the code.


I run this code on Fuji and Geneva, it worked for me.


What version you are running this code?


Must have been a browser issue as I have rebooted over lunch and it is now OK


I would also suggest to add try catch statements in client side codes as well whenever doing something related to DOM manupulation.


not sure if that would have helped on that particular one.


for quite a few options I do add a try / catch, but on those which is using the OOB code I am not quite some festidious