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
DevSFDevSF 

Make a callout when a record is created.

Hi all,

My task is to make a callout to a website which returns rank of the website. I am using Http Get method.

So I need to make a callout when a lead is created in salesforce and use the website field which the user enters while creating a lead.
we make callout using this website.

 It is working if I enter the website name manually and check for rank. But how to do this when a lead is created, and use that website field in callout.

How to achieve this.!
 
Srinivas SSrinivas S
Create an Apex Trigger on Lead object and make an asynchronous  callout.

You should call a mehtod from trigger which should looks below -
public class LeadHandler {
​	@future(callout = true)
	public static void makeCallOut(Map<Id,String> LeadIdAndWebAddresses) {
		//your callout code
		//from the response get populate rank field in lead object by querying and update.
	}
}

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
DevSFDevSF
Thank alot srinivas reddy,

But i have one question for you.

How do i pass the Website field value in my Code. Because i need the results of that website itself. 

Please have a look at the below code andmake necessary changes. (To pass the lead website__c field in callout). And i need this when a lead is created.



public with sharing class AlexaServiceCallout{
    public static String timestamp;
    public static String AWSSignature;
    public static String accessKeyId;
    private static String secretAccessKey;
    
    public static void returnAlexaScore1(String site){
     returnAlexaScore(site);
    }
//If you are calling from user action

    public static Integer returnAlexaScore(String site){
        HttpRequest req = new HttpRequest();
        String endpoint = 'http://awis.amazonaws.com/';
        calculateTimeStamp();
        String request = 'AWSAccessKeyId=' + accessKeyId  +
                '&Action=TrafficHistory'  +
                '&ResponseGroup=History' +
                '&SignatureMethod=HmacSHA1' +
                '&SignatureVersion=2' +            
                '&Timestamp=' + EncodingUtil.urlEncode(timestamp, 'UTF-8') +
                '&Url=' + EncodingUtil.urlEncode(site, 'UTF-8') ;
        generateAWSSignature('GET','google.com',request);
        endpoint += '?' +
                'AWSAccessKeyId=' + EncodingUtil.urlEncode(accessKeyId, 'UTF-8') +
                '&Action=' + EncodingUtil.urlEncode('TrafficHistory', 'UTF-8') +           
                '&ResponseGroup=' + EncodingUtil.urlEncode('History', 'UTF-8') +           
                '&SignatureMethod=' + EncodingUtil.urlEncode('HmacSHA1', 'UTF-8') +
                '&SignatureVersion=' + EncodingUtil.urlEncode('2', 'UTF-8') +       
                '&Timestamp=' + EncodingUtil.urlEncode(timestamp, 'UTF-8') +
                '&Url=' + EncodingUtil.urlEncode(site, 'UTF-8') +
                '&Signature=' + EncodingUtil.urlEncode(AWSSignature, 'UTF-8');
        req.setEndpoint(endpoint);         
        req.setMethod('GET');
        String responseBody;
        Http http = new Http();
        HttpResponse res = http.send(req);
        String GlobalRank;

        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('BODY: '+res.getBody());
      //  return   GlobalRank.trim()<>''? Integer.valueOf(GlobalRank.trim()):null;
      return null;
    }
    private static void calculateTimeStamp(){
        Datetime now = Datetime.now();
        timestamp = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';
        System.Debug('Formatted date : '+timestamp);
    }
    private static void generateAWSSignature(String verb ,String action, String request){
        String canonical = verb+'\n'+
                'awis.amazonaws.com\n'+
                '/\n'+
                request;
        system.Debug('String to Sign:\n'+canonical+'\n');
        Blob bsig = Crypto.generateMac('HmacSHA1', Blob.valueOf(canonical), Blob.valueOf(secretAccessKey));
        String signature = EncodingUtil.base64Encode(bsig);
        AWSSignature = signature;
    }
}



Thanks,
Raj
Srinivas SSrinivas S
public static void returnAlexaScore1(Map<Id,String> lieadIdSiteUrl){
/****/     
update new Lead(id=leadId,rank__c=returnAlexaScore(lieadIdSiteUrl.values()[0]))
    } 
public static Integer returnAlexaScore(String site){
        HttpRequest req = new HttpRequest();
        String endpoint = 'http://awis.amazonaws.com/';
        calculateTimeStamp();
        String request = 'AWSAccessKeyId=' + accessKeyId  +
                '&Action=TrafficHistory'  +
                '&ResponseGroup=History' +
                '&SignatureMethod=HmacSHA1' +
                '&SignatureVersion=2' +            
                '&Timestamp=' + EncodingUtil.urlEncode(timestamp, 'UTF-8') +
                '&Url=' + EncodingUtil.urlEncode(site, 'UTF-8') ;
        generateAWSSignature('GET','google.com',request);
        endpoint += '?' +
                'AWSAccessKeyId=' + EncodingUtil.urlEncode(accessKeyId, 'UTF-8') +
                '&Action=' + EncodingUtil.urlEncode('TrafficHistory', 'UTF-8') +           
                '&ResponseGroup=' + EncodingUtil.urlEncode('History', 'UTF-8') +           
                '&SignatureMethod=' + EncodingUtil.urlEncode('HmacSHA1', 'UTF-8') +
                '&SignatureVersion=' + EncodingUtil.urlEncode('2', 'UTF-8') +       
                '&Timestamp=' + EncodingUtil.urlEncode(timestamp, 'UTF-8') +
                '&Url=' + EncodingUtil.urlEncode(site, 'UTF-8') +
                '&Signature=' + EncodingUtil.urlEncode(AWSSignature, 'UTF-8');
        req.setEndpoint(endpoint);         
        req.setMethod('GET');
        String responseBody;
        Http http = new Http();
        HttpResponse res = http.send(req);
        String GlobalRank;

        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('BODY: '+res.getBody());
        /****/
        GlobalRank = [populate global rank from body];
        return GlobalRank.trim()<>''? Integer.valueOf(GlobalRank.trim()):null;
      return null;
    }
Please search for * to see the changed lines.

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.