How to make filed visible and mandatory when Form button is clicked

aarav
Kilo Contributor

Hi Team.

I want to make filed(rejected reason) Visible and make it mandatory when the 'Rejected' form button is clicked.

----------------------

Existing code   for Rejected form button is clicked

When Rejected form button is clicked ..Comments button becomes mandatory.

and the code for this is

--

current.state = 'rejected';

if(!JSUtil.nil(current.comments))

      current.update();

else{

      gs.addErrorMessage("Comments are required when rejecting an approval");

      current.state = 'requested';

      current.setAbortAction(true);

}

----------------

can i add the code to above code to make my field visible and mandatory   or do i need to write new UI action or something else..

Please suggest

Aarav.

16 REPLIES 16

bernyalvarado
Mega Sage

Hi Aarav,



Through client code you can use g_form.setMandatory();



To use that on your UI action, this will need to be transformed to a client one. In other words, the current object will not be available.



Thanks,


Berny


Hi Benny.



Do i need to include in the above code or do i need to write new UI action.


I included g_form.setMandatory();   and g_form.setVisible but that did not worked for me.


And i face script issue with g_form.setMandatory('filedname',true)



if u dont mind could you please elaborate what and where i need to perform.



Thank you very much in advance.



Aarav


Hi Aarav,



You can either change your existing UI action to be a client one or create a new one (which is client based). That basically can be accomplished by clicking the client checkbox of the UI action.



That will then imply you need to write client based code for your UI action. There's also an option to do what we call an hybrid UI action which idea i believe i first heard from this post: https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/   so... taking that as a baseline, what you then need will be something like this:



a) set an action name


find_real_file.png


b) make your UI action a client one


        find_real_file.png


c) enter an onClick function name


find_real_file.png


d) enter a code that will then be something like this:



//Client-side 'onclick' function


function runClientCode(){



      if(g_form.getValue('assigned_to')==''){


              g_form.showFieldMsg('assigned_to','assigned to is required when rejecting an approval','error');


                g_form.setMandatory('assigned_to','true');


              return false;   //Abort submission


      }



    //Call the UI Action and skip the 'onclick' function


    gsftSubmit(null, g_form.getFormElement(), 'submit_t_form'); //MUST call the 'Action name' set in this UI Action


}



//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


    runBusRuleCode();



//Server-side function


function runBusRuleCode(){


    current.state = 'rejected';


    action.setRedirectURL(current);


}



Please note that the above code makes reference to the field we entered before action name and the onclick function name.



And... voila! 😄



Thanks,


Berny


The output will be something like this:



find_real_file.png



Of course, please note that as reference I'm using the assigned to field instead of the comments field.