petercawdron
Kilo Guru

We had a challenging set of requirements from a customer. They needed a bunch of mandatory fields (easy enough with UI Policies) but there was a twist—they needed:

  • Fields that were mandatory ONLY if the person wanted to send the record for approval
  • Four fields but ONLY one of the four is mandatory (ie, put data in any one field and all four are good to go)
  • There had to be at least one record in a related list

Modal dialogs are awesome!

find_real_file.png

I developed a simple approval dialog box for a UI action to cater to these requirements. It's a good example of how complex requirements can be distilled into a simple solution.

////////////////////////////////////////////////////////////////
//Client Side 
////////////////////////////////////////////////////////////////
function approvalDialog(){

	var warnings = "";

	//////////////////////////////////////////////////////////////
	//check for at least one of four quantities to be entered
	if( g_form.getValue('u_candidates') =='' &&  g_form.getValue('u_customers') =='' && g_form.getValue('u_suppliers') =='' && g_form.getValue('u_teammembers') =='')
	{
		warnings = '<li>The quantity is missing</li>';
	}

	//////////////////////////////////////////////////////////////
	//check other fields that aren't mandatory but should be completed before the approval stage
	if(g_form.getValue('u_hosting')==''){warnings+="<li>Where this system is hosted</li>";}
	if(g_form.getValue('u_managed')==''){warnings+="<li>Where this system is managed</li>";}
	if(g_form.getValue('u_managed_by')==''){warnings+="<li>Who's managing this system</li>";}

	//////////////////////////////////////////////////////////////
	//check to see if child records exist, if not, prevent the approval going ahead
	var gr = new GlideRecord('u_categories');
	gr.addQuery('u_cmdb_ci',g_form.getValue('cmdb_ci'));
	gr.query(checkCategories);

	//async callback
	function checkCategories (response){
		if(response.rows.length==0){warnings+="<li>Added any other applicable information</li>";}
		
		//now we can open our dialog
		approvalMessage();
	}

	function approvalMessage(){

		if(warnings ==""){
			gsftSubmit(null, g_form.getFormElement(), 'approvalDialog');
		}else{
			var gm = new GlideModal('approvalRequest');
			gm.setTitle('Approval Request'); 
			gm.renderWithContent('<div style="padding:15px"><p style="color:red;font-weight:bold">Warning!</p><p>You cannot submit this form for approval until you have provided the following information:</p><ul>'+warnings+'</ul></div>'); 
			return false;//prevents the form from submitting when the dialog first load
		}
	}
}

////////////////////////////////////////////////////////////////
//Server Side code
////////////////////////////////////////////////////////////////
if (typeof window == 'undefined')
	approvalRequest();

function approvalRequest(){
	current.approval = 'requested';
	current.update();
}

Have fun and happy coding!

Version history
Last update:
‎06-29-2021 11:58 PM
Updated by: