function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ratna MansaniRatna Mansani 

Looking for Salesforce integration with Amazon SQS

Hi Everyone!!

I am looking for help on the integration of Salesforce with Amazon-SQS , this is to enable Salesforce to read the messages from SQS and process them. Over the web i can only find 3rd Party Apps that will be enable us to integrate SF and SQS , does any one have any experience on acheiving this through custom code in apex?

Any help or pointers on this is highly appreciated.

Thanks,
ShivankurShivankur (Salesforce Developers) 
Hi Ratna,

Please check out the code example given over below link:
https://webmonkeyswithlaserbeams.wordpress.com/2011/03/08/salesforce-and-amazon-sqs/

You may want to take trial with following type of code to send or receive messages from SQS:
public class AmazonSqsReceiver
{
    private String getCurrentDate() {
        return DateTime.now().formatGmt('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
    }

    public void receiveMessage() {
        //AmazonAws__c is a custom setting object that stores our keys, an Amazon Host, and a queue name
        //You can just put your keys, host and queue below as strings
        AmazonAws__c aws = AmazonAws__c.getOrgDefaults();

        String accessKey =aws.accessKey__c;
        String secretKey = aws.secretKey__c;
        String host = aws.host__c;
        String queue = aws.queue__c;

        Map<String,String> params = new Map<String,String>();

        params.put('AWSAccessKeyId',encode(accessKey));
        params.put('Action','ReceiveMessage');
        params.put('MaxNumberOfMessages','5');
        params.put('AttributeName','All');
        params.put('SignatureMethod','HmacSHA1');
        params.put('Expires', '2020-04-18T22%3A52%3A43PST');
        params.put('SignatureVersion','2');
        params.put('Version','2009-02-01');

        //The string to sign has to be sorted by keys
        List<String> sortedKeys = new List<String>();
        sortedKeys.addAll(params.keySet());
        sortedKeys.sort();

        String toSign = 'GET\n' + host +'\n'+queue+'\n';
        Integer p = 0;
        for (String key : sortedKeys) {
            String value = params.get(key);
            if (p > 0) {
                toSign += '&';
            }
            p++;
            toSign += key+'='+value;
        }
        params.put('Signature',getMac(toSign,secretKey));

        String url = 'https://'+ host+queue+'?';
        p = 0;
        for (String key : params.keySet()) {
            if (p > 0) {
                url += '&';
            }
            p++;
            url += key+'='+params.get(key);
        }

        HttpRequest req = new HttpRequest();
        req.setEndPoint(url);
        req.setMethod('GET');
        Http http = new Http();
        try {
            //System.debug('Signed string: ' + toSign);
            System.debug('Url: ' + url);
            HttpResponse res = http.send(req);
            System.debug('Status: ' + res.getStatus());
            System.debug('Code  : ' + res.getStatusCode());
            System.debug('Body  : ' + res.getBody());
        }
        catch (System.CalloutException e) {
            System.debug('ERROR: ' + e);
        }

    }
    private String encode(String message){
        return EncodingUtil.urlEncode(message,'UTF-8').replace('+', '%20').replace('*', '%2A').replace('%7E','~');
    }

    private String getMac(String RequestString, String secretkey) {
        String algorithmName = 'hmacSHA1';
        Blob input = Blob.valueOf(RequestString);
        Blob key = Blob.valueOf(secretkey);
        Blob signing =Crypto.generateMac(algorithmName, input, key);
        return EncodingUtil.urlEncode(EncodingUtil.base64Encode(signing), 'UTF-8');
    }

    public static void sendTest() {
        AmazonSqsSender t = new AmazonSqsSender();
        t.sendMessage('Hello from Salesforce ' + Math.random());
    }
}`

Hope above information helps. Please mark as Best Answer so that it can help others in future.

​​​​​​​Thanks.
Scott KostojohnScott Kostojohn
I have implemented an integration between SQS and Salesforce, but using an AWS Lambda function to read the SQS Queue and then call into the Salesforce REST API.  I know you specified reading SQS from Apex - thought I would comment in case you had any interest in an alternative approach.  If you do I can provide more details and try to answer any questions that you might have.

Thanks
Scott
Ratna MansaniRatna Mansani
Hi Scott , 

Thank you for your response and for posting the approach , which helped me think towards this direction.

Sorry for my delaed response , as i was doing some reading around Lambda functions and also SQS.

Can you please provide me more info for proceeding further on implementing a POC on this?

Thanks,
Ratna 

 
Ratna MansaniRatna Mansani
Hi Shivankur,

Thank you for your response , Will take a look and get back to you.

Thanks,
Ratna