0

global class UpdateLeadInfo implements Database.Batchable,Database.AllowsCallouts {

global Database.QueryLocator start(Database.BatchableContext BC)
{ 
    String query = 'SELECT Id, Identifiant_Fiscal_1__c,Reference_de_l_Avis_Fiscal_1__c FROM Lead ';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Lead> scope) {  
    list<Lead>valid=new list <Lead>();
    list<Lead>invalid=new list <Lead>();
    integer nbrupd=0;
    for (Lead myld:scope){ 
        String requestString = 'http://*******.ngrok.io/?req='+myld.Identifiant_Fiscal_1__c+'||'+myld.Reference_de_l_Avis_Fiscal_1__c;
        System.debug('requestString');
        Http http = new Http();                    
        HttpRequest request = new HttpRequest();    
        request.setEndpoint(requestString);         
        request.setMethod('GET'); 
        request.setTimeout(6000); 
        HttpResponse response = http.send(request);

        if (response.getStatusCode() == 200) {
            nbrupd+=1;
            System.debug('on est!! '+nbrupd);
            Map<String, Object> results =(Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug(results);
            if(results.get('statut')==true){
                Map<String, Object> infoclient = (Map<String, Object>) results .get('json');
                String revenufiscal =(String)(infoclient.get('revenuFiscalReference'));
                String adresse =(String)(infoclient.get('adresse'));
                String  annee= (String)(infoclient.get('titre'));
                System.debug('anneé  '+annee.right(4)); 
                String nombreparts = (String)(infoclient.get('nombreParts'));
                String nbrPersonnesCharge = (String)(infoclient.get('nombreDePersonnesCharge'));
                String dateEtablissement = (String)(infoclient.get('dateEtablissement'));
                String situationDefamille = (String)(infoclient.get('situationDefamille'));
                String statut = (String)(infoclient.get('statut'));
                System.debug('==========> mes info ' + infoclient  + 
                             ' Customers processed successfully.');
                Map<String, Object> dec1 = (Map<String, Object>) infoclient .get('declarant1');
                System.debug('monrevenufiscal est:!!!  ' +revenufiscal );
                String nom = (String)dec1.get('nom');
                String prenom = (String)dec1.get('prenom');
                Map<String, Object> dec2 = (Map<String, Object>) infoclient .get('declarant2');
                System.debug('monrevenufiscal est:!!!  ' +revenufiscal );
                String nom2 = (String)dec2.get('nom');
                String prenom2 = (String)dec2.get('prenom');
                System.debug('declarant1 **** '+nom+' '+prenom);
                System.debug('declarant2 **** '+nom2+' '+prenom2);
                System.debug('toute mes info** habite à '+ adresse+' parts '+nombreparts+'  revenude '+revenufiscal+' charge '+nbrPersonnesCharge+' datetab '+dateEtablissement+' famille '+ situationDefamille   );  
                string s=revenufiscal.remove(' €')+'.00';
                System.debug('mastring **** '+s +'deciii'+Decimal.ValueOf(s));
                //System.debug('decimal ****  '+decVal);
                // if (statut == 'true') {
                myld.Nom_impots__c=nom;
                myld.Prenom_impots__c=prenom;
                myld.Revenu_fiscal_de_reference__c=Decimal.ValueOf(s);
                // myld.Nombre_de_Foyers_Fiscaux_l_adresse_de__c=nombreparts;**
                myld.Nb_de_personnes_a_charge__c=Decimal.ValueOf(nbrPersonnesCharge);
                myld.Adresse_rue__c=adresse;
                myld.Nom_Declarant_2__c=nom2 ;
                myld.PrEnom_Declarant_2__c=prenom2 ;
                myld.Annee_de_la_Declaration_1__c= annee.right(4);               

                valid.add(myld);
                System.debug('je suis rentrée!! '+valid);
            }
            else {
                myld.Status='Annule';
                myld.Motif_annulation__c='Mauvaise Information';

                System.debug('futureinva '+ myld.Motif_annulation__c);
                invalid.add(myld);
            }
        } 
    }
    System.debug(valid);
    System.debug(invalid);
    update valid;
    update invalid;

}
global void finish(Database.BatchableContext BC)
{

    AsyncApexJob job = [SELECT Id, Status FROM AsyncApexJob WHERE Id = :BC.getJobId()];
    System.debug('statut  '+job);

}

}

trigger trigger CallBatch on Lead (after insert) { if(Trigger.isAfter){ if (Trigger.isInsert) { database.executebatch(new UpdateLeadInfo ()); }}}

  • when I changed my trigger I had this error:15:37:33.117 (122125864234)|FATAL_ERROR|System.CalloutException: Exceeded maximum time allotted for callout (120000 ms) – maya Aug 28 at 13:43
0

Salesforce Callouts have certain limitations and the one that you are hitting is:

"The maximum cumulative timeout for callouts by a single Apex transaction is 120 seconds. This time is additive across all callouts invoked by the Apex transaction".

You can find the other limitations here: Callout Limits and Limitations

I believe it would be a good idea to convert the implementation into a Batch job and set the appropriate batch size by benchmarking so that it doesn't hit the Max Callout time allowed.

Here is the developer article on how to use Batch processes: Using Batch Apex

Go through it and let me know if you have any questions. Also, post to the thread if you need any additional info about Batch processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.