SAP development inbound email
Description
Create a request for incoming mails if the sender is registered in Xurrent. If not send a reject mail back.
Approach
Get the mail from address and fetch it from datacenter and directory account. If found create the request. If not send the reject mail.
Prerequisites
The service instance id must be set in the environment variables section.
$SAP_Basis_SI- a default value containing the SAP service instance id used to create the request
Default values are used so solve the following problems:
- Different environments: E.g. service instance id's are different in sandbox and production. Testing is done in sandbox - after completion the package code can be transferred to production without a change, only the default values must be changed in production.
- Different packages: The same value should be used in different packages - a default value is identical in every package of an environment.
In addition the webhook must be set with following parameters:
- Account: datacenter
- Event: sap-develpment (email-in)
Commented code
// Email-IN Example
// write a log with the mail subject and mail from address to the package log
log('INCOMING EMAIL: ', mail.subject);
log('mail from: ', mail.from);
// mail.from is a list of address information in the mail.
// get the first mail from value into the mailFrom variable.
let mailFrom = mail.from[0];
// get the mail from address into the mailFromAddr variable
let mailFromAddr = mailFrom.address;
// for demo we simulate beatrice as sender to show the creation of the request
// comment this line to work with real e-mail in values
mailFromAddr = 'beatrice.baldwin@widget.com'; //simulate beatrice as sender
// To check if there is a person with the senders e-mail address,
// we do a search in Xurrent with the fetchFilter function
// set a filter variable with the filter value and url compatible (encodeURI) mailFrom Email Adress value
let filter = 'primary_email=' + encodeURI(mailFromAddr);
// perform the search on the people module
// in this fetch the datacenter account is used, because it is configured in the package trigger
let fetchResult = fetchFilter('people', filter);
// since the result of fetchFilter is always a list we try to get out the first element (with index 0)
let person = fetchResult.first();
// Existence check - if no person was found in the fetchFilter above ...
if (!person) {
// ... we do the fetchFilter again in the directory account
fetchResult = fetchFilter('people', filter, 'directory');
person = fetchResult.first();
}
// after this there is known if the e-mail address is known in the Xurrent accounts.
// set the $personId variable for easier handling in later code
// if there is no person the $personId value is null - a existence check is possible
let personId = person.id;
log('personId: ', personId);
// if the person was found in the code above ...
if (personId) {
// ... set up request data with the mail subject and the service instance id available in defaults
// add the mail text as a note, and set the requested by id to the person id
let requestData = {
category: 'rfc',
subject: mail.subject,
service_instance_id: $SAP_Basis_SI,
requested_by_id: personId,
note: mail.text
};
// create the request and log the result into the package log
let request = create('requests', requestData);
log('request: ', request);
} else {
// if the person was not found in the code above the "if(personId)" existence check was false and
// the else part of the if will be executed
// set up mail data without a template
// set the receiver mail to address to the original sender of the mail
let text = 'Your email address ' + mailFrom.address + '\n'
+ 'was not found in our records and was therefore rejected.\n\n'
+ 'SUBJECT: ' + mail.subject + '\n\n'
+ mail.text + '\n';
let mailData = {
to: mailFrom.address,
subject: 'SAP Development Request rejected',
body: {
text: text
}
};
// send the mail an log its content to the package log
sendMail(mailData);
log('reject mail: ', mailData);
}
Condensed code
The comments and log entries are helpful, but make the code above quite long. Leaving them out, the code can be condensed to:
let mailFrom = mail.from[0];
let mailFromAddr = mailFrom.address;
// use demo address
mailFromAddr = 'beatrice.baldwin@widget.com'; //simulate beatrice as sender
let filter = 'primary_email=' + encodeURI(mailFromAddr);
let fetchResult = fetchFilter('people', filter);
let person = fetchResult.first();
if (!person) {
fetchResult = fetchFilter('people', filter, 'directory');
person = fetchResult.first();
}
let personId = person.id;
if (personId) {
let requestData = {
category: 'rfc',
subject: mail.subject,
service_instance_id: $SAP_Basis_SI,
requested_by_id: personId,
note: mail.text
};
let request = create('requests', requestData);
} else {
let text = 'Your email address ' + mailFrom.address + '\n'
+ 'was not found in our records and was therefore rejected.\n\n'
+ 'SUBJECT: ' + mail.subject + '\n\n'
+ mail.text + '\n';
let mailData = {
to: mailFrom.address,
subject: 'SAP Development Request rejected',
body: {
text: text
}
};
sendMail(mailData);
}