UI Macro and mandatory fields

oharel
Kilo Sage

Hi all,

I have a reference field and a UI Macro button that can send an email to the person in the field.

I would like the UI Macro work only if all mandatory fields were populated, and if not they are not filled, to pop up an alert and abort.

If all mandatory fields are populated, clicking on the UI Macro button saves the form and pops up the email.

This is what I am currently using (based on Mike Allen's answer to this post.)

function sendMail() {

  var case1 = g_form.getValue('u_alternative_approver');

  if(case1 == '') {

  alert('Please complete all mandatory fields and save the form before sending a mail.');

  return false;

  }

  g_form.save();

  var sys_id = g_form.getUniqueValue();

  var url = "email_client.do?sysparm_table=u_new_employee_requests&sysparm_sys_id=" + g_form.getUniqueValue() + "&sysparm_target=u_new_employee_requests&sys_target=u_new_employee_requests&sys_uniqueValue=" +g_form.getUniqueValue() + "&sys_row=0&sysparm_encoded_record=&u_new_employee_requests.u_email_type=e48c5d9537fce600865bdb9643990e9b&sysparm_domain_restore=false&sysparm_stack=no";

  window.open(url, '_blank');

  window.focus();

}

The problem with the script is that it only checks if the field alternate approver is empty. If it is not empty, a regular pop up "The following mandatory fields are not filled in:..." will appear and then proceed to show the email anyway. However, the email must contain data from mandatory fields, and if the data is not there, the email is empty.

So I need to abort if mandatory fields are not populated.

How do I do that?

Thanks

harel

1 ACCEPTED SOLUTION

Instead, I put in a client script that hides the button if it's a new record. This way, all mandatory fields will have to be filled anyway before submitting the form, and then the UI Macro will show.


function onLoad() {


  //Type appropriate comment here, and begin script below


  var el = document.getElementById('mailbtn');


  if(g_form.isNewRecord()){


  el.style.display = 'none';


  } else {


  el.style.display = 'inline';


  }


}


I can also cut my UI Action to this:


function sendMail() {



  var sys_id = g_form.getUniqueValue();


  var url = "email_client.do?sysparm_table=u_new_employee_requests&sysparm_sys_id=" + g_form.getUniqueValue() + "&sysparm_target=u_new_employee_requests&sys_target=u_new_employee_requests&sys_uniqueValue=" +g_form.getUniqueValue() + "&sys_row=0&sysparm_encoded_record=&u_new_employee_requests.u_email_type=e48c5d9537fce600865bdb9643990e9b&sysparm_domain_restore=false&sysparm_stack=no";



  window.open(url, '_blank');


}



What do you think?



harel


View solution in original post

14 REPLIES 14

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Harel,



You should be able to get missing field at client side with below line of code.


g_form.getMissingFields();



Reference :


Mandatory Check


Hi Pradeep,



For some reason, even if all missing fields are filled, I still get "The IDS of fields that are mandatory...:" and then the list is empty and the email window does not come up.


This is what I added:


if(case1 == '') {


alert('Please complete all mandatory fields and save the form before sending a mail.');
return false;
} else if(!case1=='') {
var arr = g_form.getMissingFields();
alert("The IDs of fields that are mandatory are not filled : " + arr);
return false;
}

Harel



Edit: How do I add that if there are no missing fields found, just go ahead with the rest of the UI action?


Sample code. This will return how many mandatory fields on form is missing.


var arr = g_form.getMissingFields();


var len = arr.length;


alert(len);



Can you put alerts and check once at client editor side.


It goes down from 7 to 0, but when it gets to 0 it still issues the alert.



Harel