EPM GROOVY – Sending Emails Directly from Business Rules (No Third-Party Required)

We have to deal with long-running rules when dealing with larger datasets, especially when it comes to data aggregation, data maps, and so on. The idea is to provide business rule executors the option of receiving or not receiving an email notification regarding the status of a business rule.

Oracle includes a dedicated REST API for sending email directly from business rules, with the option to attach files smaller than 10 MB. This eliminates the need to purchase or configure third-party APIs in order to send emails.

ESTABLISH AN INTERNAL REST CONNECTION

The connection needs to be set up as “Other Web Service Provider”, but we can specify the local host URL in the connection field. I prefer not to set up the advanced options in the connection setup since that can be specified later in the groovy script. There are no ways to check the connection for other web service provider type connections.

always use domainName.userEmail format in the user input field below.

CREATE ‘SEND EMAIL’ TEMPLATE

I prefer to make a template that I can use for the majority of the rules. We need to capture the recipient’s email address, subject, and email body in order to send an email. We don’t have any APIs that allow us to retrieve the rule executor’s email address. We can acquire the user’s complete name and last name, but not their email address. I’m hoping it’ll be included in the package shortly. For the time being, the email ID is retrieved using an RTP variable. The message will not be sent if the user does not provide an email address. It would throw an error stating that the “send email functionality cannot handle CRLF items because the parameters cannot have next line strings.

Groovy
String mailTo = stripQuotes(rtps.userEmail.enteredValue)

if (mailTo) {
    def mailStatus = sendEmail(mailTo) as Integer
    if (mailStatus == 0) {
        println "Email successfully triggered"
    }
}

def sendEmail(String sendTo) {
    String connection = "internalConnection"
    String subject = "Completion Status: " + rule.name
    String body = rule.name + " has been completed successfully. Please check the job console for more details."

    String contents = "to=${sendTo}&subject=${subject}&body=${body}"
    //Initiate email
    HttpResponse<String> jsonResponse = operation.application.getConnection(connection).post("/interop/rest/v1/services/sendmail")
            .header("Content-type", "application/x-www-form-urlencoded")
            .body(contents).asString()
    //capture error and throw error
    if (!(200..299).contains(jsonResponse.status))
        throwVetoException("Error occured: $jsonResponse.statusText")

    //trace the process using JobId status until it is complete
    final int IN_PROGRESS = -1
    ReadContext ctx = JsonPath.parse(jsonResponse.body)
    String jobId = ctx.read('$.links[1].href').toString().split("/").last() as String
    int status = ctx.read('$.status')
    for (long delay = 50; status == IN_PROGRESS; delay = Math.min(1000, delay * 2)) {
        sleep(delay)
        HttpResponse<String> pingResponse = operation.application.getConnection(connection).get("/interop/rest/v1/services/jobs/" + jobId).asString()
        status = JsonPath.parse(pingResponse.body).read('$.status')
    }

    return status
}

ADD TEMPLATE TO THE RULES

The rule needs to have {userEMail} variable as a prerequisite. Below is how it can be attached to the rule.

Groovy
*RTPS: {userEmail}*/

//Main Script 
println "Start"
sleep(6000)
println "End"

//sendEmail Template
%Template(name:="Groovy Template - Send Email",application:="MyApp",plantype:="plan1",dtps:=())

FINALLY, EXECUTION