Skip to main content

executeAction()

Last updated 11/09/2024

Example One

The following code executes an action on the Five server to send a leave request email to management.

Executing a mail merge action
function sendLeaveRequestEmail(five: Five, context: any, result: FiveError): FiveError {
const emailContext: any = {};
emailContext['SMTPToName'] = 'Bill Watts';
emailContext['SMTPToEmail'] = 'bill@gmail.com';
emailContext['SMTPSubject'] = 'Request for leave';

const executeResult = five.executeAction('LeaveRequest', emailContext);
if (executeResult.isOk() === false) {
return five.createError(executeResult, 'Failed to send email');
}

return five.success();
}

Example Two

The following code is identical to example one except the action is initiating from the client. The optional

onResult()
will be executed when the action has completed on the server.

Executing a mail merge action initiated from the client
function sendLeaveRequestEmail(five: Five, context: any, result: FiveError): FiveError {
const emailContext: any = {};
emailContext['SMTPToName'] = 'Bill Watts';
emailContext['SMTPToEmail'] = 'bill@gmail.com';
emailContext['SMTPSubject'] = 'Request for leave';

const executeResult = five.executeAction('LeaveRequest', emailContext, (result: FiveError) => {
if (result.errorCode === "ErrErrorOk") {
five.showMessage('Mail has been sent');
} else {
five.showMessage(result.message);
}
});

if (executeResult.isOk() === false) {
return five.createError(executeResult, 'Failed to send email');
}

return five.success();
}

Example Three

The following code executes an action using the optional

at
paramater on the Five server to send a leave request email to management 24 hours later.

Executing a mail merge action at a future time
function sendLeaveRequestEmail(five: Five, context: any, result: FiveError): FiveError {
const emailContext: any = {};
emailContext['SMTPToName'] = 'Bill Watts';
emailContext['SMTPToEmail'] = 'bill@gmail.com';
emailContext['SMTPSubject'] = 'Request for leave';

const timeToSend = new Date();
timeToSend.setDate(timeToSend.getDate() + 1);

const executeResult = five.executeAction('LeaveRequest', emailContext, timeToSend);
if (executeResult.isOk() === false) {
return five.createError(executeResult, 'Failed to send email');
}

return five.success();
}

Example Four

The following code executes the StaffExtension report in the backend and emails the report to all staff.

Generate a report into a mail merge
function EmailReport(five: Five, context: any, result: FiveError) : FiveError {
////////////////////////////////////////////////////////////////////////////////////////////////
// Execute the report action
////////////////////////////////////////////////////////////////////////////////////////////////
const reportResult = five.executeAction('StaffExtension', {});
if (reportResult.isOk() === false) {
return five.createError(reportResult);
}

////////////////////////////////////////////////////////////////////////////////////////////////
// The reportResult contains a parameter called report, which is the report result in the format
// of a pdf (encoded as data url application/pdf mimetype)
////////////////////////////////////////////////////////////////////////////////////////////////
const mailMergeContext = {SMTPToEmail: five.field.EmailAddress, SMTPToName: "Staff", SMTPAttachments: [reportResult.report]};
const mailResult = five.executeAction('ExtensionReportEmail', mailMergeContext);
if (mailResult.isOk() === false) {
return five.createError(mailResult);
}

return five.success('Report has been sent');
}

Example Five

The following code executes the StaffExtension report in the backend and sends to the client so the report PDF can be downloaded.

Generate and download a report
function DownloadReport(five: Five, context: any, result: FiveError) : FiveError {

////////////////////////////////////////////////////////////////////////////////////////////////
// Execute the report action
////////////////////////////////////////////////////////////////////////////////////////////////
const reportResult = five.executeAction('StaffExtension', {});
if (reportResult.isOk() === false) {
return five.createError(reportResult);
}

////////////////////////////////////////////////////////////////////////////////////////////////
// can send back to the client, can be access in the OnComplete event on the action button
// the reportResult contains a report value containing the report as a pdf encoded as a data URL
////////////////////////////////////////////////////////////////////////////////////////////////
result = five.success();
result.setData(five.MIMETypeDataURL, reportResult.report);
return result;
}