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

HI Abhinay,



When I click on the UI Macro button, I get back to the list of records, and no email shows...


By the way, any idea why this shows an error "el is null":


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';  


  }  


}




harel


Yes, well, the client script did not work because I had an extra space in the ref_contributions statement:


ref_contributions<space>=email_alternative_manager


instead of


ref_contributions=email_alternative_manager


Did you get this working? or you still looking for help?


Hi Abhinay,



I got it to work using the client script. It is not exactly what I had in mind at the beginning, but it does the job.


Thanks.



Harel


Glad, you got it working. Please mark your answer as correct and close this thread. let me know if you nay questions.