Make field mandatory depending on something else

amacqueen
Mega Guru

I know I can use UI Policies to make a field mandatory based on certain conditions however i need to make a field mandatory the next time a form is opened and can't make it work.

Scenario is I am building a bespoke change application for a part of our business and once the change manager assigns the change to an individual I want the development field (u_development) to become mandatory only once the form is saved. I don't want the change manager to have to update the u_development field which is what a UI Policy enforces.

Can I achieve this?

Many thanks

10 REPLIES 10

zica
Giga Guru

Hello Anglus,



While loading the form, the the call is assigned to somebody, you want u_development field to be mandatory.


Please try the script below :



function onLoad() {


  //Type appropriate comment here, and begin script below


  var new_val = g_form.getValue("assigned_to");



  if (new_val == '') {


  try {


  g_form.hideFieldMsg('work_notes');


  } catch(e) {


  }


  g_form.setMandatory('work_notes', true);


  g_form.showFieldMsg('work_notes','Please add details','error');


  g_form.flash("work_notes", "#FFFACD", 0);


  return false;   //Abort submission


  }


}



Kind regards,


ZA



Do not feel shy to mark correct or helpful answer if it helps or is correct


amacqueen
Mega Guru

That has the same result as the previous script I'm afraid. Let me run through what I've done. I added the script and went into the form for the change which at this point did not have an assignment group or assigned to selected. I assigned the change to one of my colleagues and went to save the change and got the pop up box saying 'The following mandatory fields are not filled in: Development' so I couldn't continue unless I updated that field which is not what I want. I want to be able to assign the change to some one and have service now make the development filed mandatory the next time the change is opened.


Thanks for your perseverance though.


zica
Giga Guru

Hello Angus MacQueen,



Thank you for being patient. To achieve this query, we'll have to user script include and client script, It would be on-demand query.


Here below the script I have developed in order to make the worknotes mandatory (I havent got the sanme field than you, sorry), you can change the name of the field as per your requierement,



Type : onLoad, table : incident


Client Script   :


        function onLoad() {


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


                            var ga = new GlideAjax('ciCheck');


                          ga.addParam('sysparm_name', 'getCiSupportGroup');


                          ga.addParam('sysparm_ag', g_form.getValue('assigned_to'));


                          ga.getXML(doAlert);


              }


}



// Callback function to process the response returned from the server


function doAlert(response) {


      var answer = response.responseXML.documentElement.getAttribute("answer");


      alert(answer);


}




Script include :


NAME : ciCheck (Should be the same than under GlideAjax on client script)


Client callable : true (ticked)


accessible from all application scopes



        var ciCheck = Class.create();


        ciCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {


         


        getCiSupportGroup: function() {


              var retVal = ''; // Return value


              var agID     = this.getParameter('sysparm_ag');


              retVal = 'FILL out the worknotes';



              return retVal;


      }



});



This script pop the an alert asking to put a comment into worknotes field. What you can do is change that as per your requierement,



I am more than happy to help, if you need any help, feel free to contact me



Kind regards,


ZA



Do not feel shy to mark correct or helpful answer if it helps or is correct


This was getting a bit complicated for me so I've agreed with the business to simplify the requirement and have implemented that change.



Thanks for your help


divyabheemav
Kilo Explorer

how to make field mandatory which depends on another field using business rule, can anyone please help me out in this.