Skip to main content
Skip table of contents

SMS Connector on Mulesoft Anypoint Studio

This integration guide covers how to set up a connection between the Retarus SMS REST API and Mulesoft Anypoint Studios with Salesforce. For this example, a REST-based service connection is described. However, SOAP and SMTP are also viable options. This approach is also applicable to other Retarus gateways, such as Retarus Cloud Fax and Transactional Email.

Prerequisites

Part 1 Logical Setup and Logic Blocks

Logical Setup

image-20240611-171510.png

Architectural Overview of the Components

Logic Blocks

Anypoint Studio IDE Logic required blocks:

  1. Anypoint Studio IDE Logic required blocks: listener, transform message, and request

  2. Connecting Salesforce to Mulesoft

  3. Salesforce Flows

image-20240611-172208.png

Necessary Logic Blocks

1. Listener

The Listener listens on a port (e.g. 8081) for requests. 

A Request to the listener can be generated via Postman; the SMS Job ID is provided to the Postman App, as depicted here.

image-20240611-172400.png
image-20240611-172443.png

HTTP Listener config 

image-20240611-172507.png

2. Transform Message

Hardcoded output application/JSON in the Transform Message

image-20240611-174840.png

3. Request
image-20240611-174941.png

Part 2 Connecting requesting Salesforce application to Mulesoft

1. Mulesoft Setups
image-20240611-175720.png
  • Or get access to the localhost via an application like NGROK

image-20240611-175909.png

Apex Class and its function in Salesforce can't call localhost


image-20240611-180121.png

Apex Class and its function in Salesforce can't call localhost

2. Making the local API public available via tunnel like NGROK

Please be aware that there is an open connection to your local environment via a dedicated port. Consequently, it is recommended that you only do this for testing.

image-20240611-180408.png

image-20240611-180427.png

Ngrok localhost tunnel for external availability

image-20240611-180457.png

Unauthorized Access-Point → Needs to be whitelisted

3. Sample APEX Code in Salesforce
CODE
public class RetarusSMSAPI {
    public static void callSMSApi(){
        Http http = new HTTP();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        req.setEndpoint('<Your_API_URL>/send'); ###  Example using NGROK: https://3dfb-94-199-89-121.ngrok.io ###
        req.setMethod('GET'); 
        res = http.send(req);
        System.debug(res.getBody());
    }
}


### Call to:  RetarusSMSAPI.callSMSApi() ###

Part 3 Send SMS from your Salesforce Application

How to send SMS out of your Salesforce Application

In this section, you will see how you can send an SMS out of any Salesforce application by using:

  • Mulesoft Anypoint Studio

  • Salesforce APEX Code

  • Salesforce Flows

At the end of this section, you will see how to integrate a "Send SMS" - GUI into a Salesforce Opportunity Page (see red box in the image below).

image-20240611-180957.png

Salesforce Opportunity Page: Integrated "Send SMS"- Flow

Add your desired content in the "Send SMS" box on the right (see image below) and click Next.

image-20240611-181125.png

After clicking Next a message displays in Salesforce, notifying you that the send process was triggered. 

This is NOT a successful delivery notification provided by Retarus. Grabbing the JobID Status is not a part of this overview. The data flow is depicted here as: Salesforce Dashboard → Salesforce Flows → Apex Code → HTTP Request to Mulesoft was performed without error. If an error occurs in this toolchain, an error message displays: "Please consult your Salesforce Admin is triggered”.

In this example, the Salesforce Admin receives an email with more information on the send request via email.

image-20240611-181322.png

Programming Logics and Salesforce Flows

The following logics data flows are covered here:

  1. Message parameters are defined by the end-user using the "Dashboard"

  2. The input data is processed by a Salesforce "Flow" Component 

  3. The Flow component calls an APEX Class providing via HTTP Request the data to Muleosoft AnyPoint Studio

  4. Mulesoft AnyPoint Studio (compiled program, hosted locally but reachable using NGROX via https request) listens to the HTTP Request von specified Port

  5. AnyPointStudio transforms the received data into the REST API Format required by the Retarus REST API

  6. AnypointStudio sends out an HTTP Request to the Retarus infrastructure

    image-20240611-181439.png

1. Message parameters

Defined by the end-user using the "Dashboard" that can be integrated into any Salesforce Page using the "Edit Page”/Lightning App Builder:

image-20240611-181734.png

2. Input Data

The input data is processed by a Salesforce "Flow" Component (see example logic below).

image-20240611-181959.png

Flow using a Frontend-SMS Sending - Apex Action for interaction with APEX Classes defined in Salesforce Developer Console

image-20240611-182042.png

Open the Salesforce Developer Console

3. Call the APEX Class

The Flow component "Apex Action" calls an APEX Class providing via HTTP Request the data to Muleosoft AnyPoint Studio.

Sample APEX Code for embedding multiple parameters out of Salesforce Flow:

CODE
global class RetarusSMSAPI_In {
    @InvocableMethod(label = 'Send SMS' description='method description')
    
    global static void callSMSApi_in(smsRequest[] requests){
        for (smsRequest request: requests) {
            Http http = new HTTP();
        	HttpRequest req = new HttpRequest();
        	HttpResponse res = new HttpResponse();
        	req.setEndpoint('https://d3ab-2a02-7b07-0-c11e-475-00-967.ngrok.io/send'); 
        
        	String SRC = request.SRC;
        	String msg = request.msg;
        	String dst = request.dst;
        	JSONGenerator gen = JSON.createGenerator(true);
        	gen.writeStartObject();
        	gen.writeStringField('SRC',SRC);
        	gen.writeStringField('msg',msg);
        	gen.writeStringField('dst',dst);
        	gen.writeEndObject();
        	String jsonS = gen.getAsString();
        	System.debug(jsonS);
        	req.setBody(jsonS);
        	req.setHeader('Content-Type','application/json');
        	req.setMethod('POST');		        
        	res = http.send(req);
        	System.debug(res.getBody());
        	}        
    }
    
      global class smsRequest {
       @InvocableVariable(required=true)
        global String SRC; 
          
         @InvocableVariable(required=true)
         global String dst;
        
         @InvocableVariable(required=true)
         global String msg;
          
   		 }
   
}

Mulesoft AnyPoint Studio (compiled program, hosted locally but reachable using NGROX via https request) listens to the HTTP Request von specified Port.

As described earlier in this document, AnyPoint Studio compiled program can be hosted via NGROX for external connectivity.

image-20240611-182926.png

4. Transform Message

AnyPointStudio transforms the received data into the REST API Format required by the Retarus REST API

image-20240611-183251.png

A transform message block transforms the received JSON data into the REST format required by the Retarus REST API POST Format:

Sample code:

CODE
%dw 2.0
output application/json
---
{
"options" : {
  "src" : payload.SRC,
  },
  "messages" : [ {
  "text" : payload.msg,
  "recipients" : [ {
  "dst" : payload.dst
  }]
  }
  ]	
}
5. Request the Retarus Messaging Platform

AnypointStudio "Request" component sends out an HTTP Request to the Retarus infrastructure in the required JSON Format.

image-20240611-184200.png

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.