- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 01:07 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 05:52 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 03:46 PM
Can you keep if condition i.e return false only if the count is greater than 0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 04:07 PM
That's exactly what I was thinking.
The problem (probably with my logic somewhere) is that the mail shows without the data in to, as if the form is not saved. Then, when I click again on the UI Macro, the email shows with the data. As if does not have enough time to save the data before the mail is shown.
See what I tried:
function sendMail() {
g_form.checkMandatory = false;
var arr = g_form.getMissingFields();
var len = arr.length;
if(len > 0) {
alert("The IDs of fields that are mandatory are not filled : " + arr);
return false;
}
if(len == 0) {
g_form.save();
}
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');
}
This is client side. I cannot use windows.open on server side and action.setRedirectURL(url) did not open in a new tab.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 04:54 PM
Hi Pradeep,
Sorry, I just noticed you said without the the len==0 statement.
Two things happen:
1. The alert "he IDs of fields that are mandatory" pops up for a second and then disappears.
2. I never get to see the "Please complete all mandatory fields" message, even when the only one to fill is the alternative field. That's not really important, as I have the first alert alert showing.
But how do I make the first alert stay until confirmation (clicking OK)?
Other than that - the email behaves as expected.
harel
Edit: I just noticed there's different behavior in Firefox and in Chrome:
Firefox: alert stays for a second and disappears. If you insert the missing field and click on the UI Macro button, data shows up in the mail.
Chrome: alert stays until clicking OK, however, if you insert the missing field and click on the UI Macro button (without saving first), data does not show up in the mail.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 05:52 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2016 07:32 PM
Try setTimeout() after g_form.save();
function sendMail() {
g_form.checkMandatory = false;
var arr = g_form.getMissingFields();
var len = arr.length;
if(len > 0) {
alert("The IDs of fields that are mandatory are not filled : " + arr);
return false;
}
if(len == 0) {
g_form.save();
}
var sys_id = g_form.getUniqueValue();
setTimeout(clientPopUp,3000);
function clientPopUp(){
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');
}
}