0

I got following error in production.

Apex script unhandled exception by user/organization: 00580000005FYRy/00D80000000PQ16

Failed to invoke future method 'public static void updateSubs(List)' on class 'MDLiveIntegration' for job id '7078000001iFGgq'

caused by: System.DmlException: Update failed. First exception on row 0 with id a0q8000000QlTHsAAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SubscriberUpdateTrigger: execution of AfterUpdate

caused by: System.AsyncException: Future method cannot be called from a future or batch method: MDLiveIntegration.deleteSubs(String, Id)

Trigger.SubscriberUpdateTrigger: line 55, column 1: []

Class.MDLiveIntegration.updateSubs: line 247, column 1

But in sandbox i don't get any error. Please give your suggestions.

Thanks, Vimal

  • Welcome to SF.SE. Vimal. Would you please post the code from your SubscriberUpdateTrigger and your class MDLiveIntegration? The key here seems to be in this part of your error message: System.AsyncException: Future method cannot be called from a future or batch method: MDLiveIntegration.deleteSubs(String, Id). – crmprogdev Mar 10 '15 at 14:38
0

Here is my code

Apex trigger

List subRecords = new List(); List subUpdateRecords = new List(); Boolean deleteflag = false; if(!trigger.IsDelete) {

    for(Subscriber__c subs : Trigger.new){           

        if(subs.Active_Subscriber__c == true && subs.Auto_Telemedicine__c == true){

            Boolean needsUpdate = false ;        
            if(Trigger.isInsert){             
                needsUpdate = true;
            } 
            else if (Trigger.isUpdate){                
                needsUpdate = false;
            }                
            if(needsUpdate)
                subRecords.add(subs.id);
            else
                subUpdateRecords.add(subs.id);   
        }
        else
        {
            deleteflag = true;
        }
    }       
    if(subRecords.size() >0){             
       if(system.isFuture()) 
           return;  
       MDLiveIntegration.insertSubs(subRecords);     

    }       
    if(subUpdateRecords.size() >0){             
        if(system.isFuture()) 
            return;              
        MDLiveIntegration.updateSubs(subUpdateRecords);   
        MDLiveIntegration.reactiveSubs(subUpdateRecords);                              

    }       
}
else 
{        
    for(Subscriber__c subs : Trigger.old) {                                                          
        if(subs.Active_Subscriber__c == true && subs.Auto_Telemedicine__c == true){    
            MDLiveIntegration.deleteSubs(subs.MDLIVE_ID__c,subs.id);    
        }       
    }
}     

if(deleteflag){
    if(Trigger.isUpdate){
        for(Subscriber__c subs : Trigger.old) {                                                                   
            MDLiveIntegration.deleteSubs(subs.MDLIVE_ID__c,subs.id);  // Display Error in this line           
        }
    }            
}          

Apex Class

@future(callout=true) public static void updateSubs(List ids){

    String responseBody;
    String Data;
    List<Subscriber__c> lists = new List<Subscriber__c>();         
    List<Subscriber__c> subs = [Select id,MDLIVE_ID__c,Subscriber_Contact__r.MDLive_Birthdate__c,Name,
    Product_Bundle__c,Subscriber_Account__c,Related_Opportunity__c,Subscriber_Contact__r.Gender__c,
    Subscriber_Contact__r.name, Subscriber_Contact__r.Phone,Subscriber_Contact__r.FirstName, Subscriber_Contact__r.LastName,
    Subscriber_Contact__r.MailingCity, Subscriber_Contact__r.MailingState, Subscriber_Contact__r.MailingCountry,
    Subscriber_Contact__r.MailingPostalCode, Subscriber_Contact__r.MailingStreet,Subscriber_Contact__r.Email,
    Subscriber_Contact__r.Birthdate from Subscriber__c where id =: ids];    

    for(Subscriber__c subitems : subs ){

        // get value code
        gen = subitems.Subscriber_Contact__r.Gender__c;
        fname = subitems.Subscriber_Contact__r.FirstName;    
        lname = subitems.Subscriber_Contact__r.LastName;
        Product_Bunndle = subitems.Product_Bundle__c;
        Account_name = subitems.Subscriber_Account__c;
        Opps_name = subitems.Related_Opportunity__c;
        email = subitems.Subscriber_Contact__r.email;            
        street = subitems.Subscriber_Contact__r.MailingStreet;
        city = subitems.Subscriber_Contact__r.MailingCity;
        state = subitems.Subscriber_Contact__r.MailingState;
        code = subitems.Subscriber_Contact__r.MailingPostalCode;
        country = subitems.Subscriber_Contact__r.MailingCountry;
        phone = subitems.Subscriber_Contact__r.phone;
        bdate = subitems.Subscriber_Contact__r.MDLive_Birthdate__c;
        ext_id = subitems.Name;             

        if(subitems.MDLIVE_ID__c != null){

            // Http callout code                                                             
            req.setEndpoint('https://members.mdlive.com/services/members/'+subitems.MDLIVE_ID__c);
            req.setMethod('PUT');
            req.setHeader('Content-Type', 'application/json');
            Blob headerValue = Blob.valueOf(username +':' +password);
            String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('RemoteUserId', 'f58b2369a9b9df5438ea');            

            jsonData = '{"member":{"first_name":' + '\"'+ fname +'\"'+',"last_name":'+'\"'+ lname +'\"'+',"address1":'+'\"' + street + '\"'+',"gender":'+'\"'+gen+'\"'+',"state":'+'\"'+state+'\"'+',"zip":'+'\"'+code+'\"'+',"City":'+'\"'+city+'\"'+',"Country":'+'\"'+country+'\"'+',"Email":'+'\"'+email+'\"'+',"Phone":'+'\"'+phone+'\"'+',"birthdate":'+'\"'+bdate+'\"'+'}}';
            req.setBodyAsBlob(Blob.valueof(jsondata));         

            Http http = new Http();                  
            HttpResponse res; 
            if(!Test.isRunningTest()){
                res = http.send(req);
                responseBody = res.getBody();
                System.debug('Body Data:'+res.getBody());    
            } else {
                responseBody = '{"member":{"external_id":"28","Id":"713","first_name":"Jimmy","last_name":"Games","address1":"india","gender":"male","state":"NY","zip":"36560","birthdate":"27-08-1988"}}';
            }

            JSONParser parser = JSON.createParser(responseBody);
            while (parser.nextToken() != null) {
                if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
                    data = parser.getText();
                    if(data == 'errors'){
                        sendMail(responseBody);
                        break;
                    }                        
                }    
            }                                                                        
        } 
        else {

            req.setEndpoint('https://members.mdlive.com/services/members');
            req.setMethod('POST');
            req.setHeader('Content-Type', 'application/json');
            Blob headerValue = Blob.valueOf(username +':' +password);
            String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);    

            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('RemoteUserId', 'f58b2369a9b9df5438ea');

            jsonData = '{"member":{"external_id":' + '\"'+ ext_id +'\"'+',"first_name":' + '\"'+ fname +'\"'+',"last_name":'+'\"'+ lname +'\"'+',"address1":'+'\"' + street + '\"'+',"gender":'+'\"'+gen+'\"'+',"state":'+'\"'+state+'\"'+',"zip":'+'\"'+code+'\"'+',"City":'+'\"'+city+'\"'+',"Country":'+'\"'+country+'\"'+',"Email":'+'\"'+email+'\"'+',"Phone":'+'\"'+phone+'\"'+',"birthdate":'+'\"'+bdate+'\"'+'}}';
            req.setBodyAsBlob(Blob.valueof(jsondata));             

            Http http = new Http();  
            req.setTimeout(60000);           
            HttpResponse res;
            if(!Test.isRunningTest()){
                res = http.send(req);
                responseBody = res.getBody();
                System.debug('Response Body::'+res.getBody());
            }else{
                responseBody = '{"member":{"external_id":"28","first_name":"Jimmy","last_name":"Games","id":"713","address1":"india","gender":"male","state":"NY","zip":"36560","birthdate":"27-08-1988"}}';                
            }

            // JSon parse for Id

            JSONParser parser = JSON.createParser(responseBody);
            while (parser.nextToken() != null) {
                if(parser.getCurrentToken() == JSONToken.FIELD_NAME){

                    String fieldName = parser.getText();
                    System.debug('My Field Name:'+fieldName);

                    data = parser.getText();
                    if(data == 'errors'){
                        sendMail(responseBody);
                        break;
                    }

                    parser.nextToken();
                    System.debug('Last Token'+parser.getText());
                    if(fieldName == 'Id'){
                        String vals = parser.getText();
                        System.debug('Value val'+vals);
                        //MyID.add(vals);
                        Subscriber__c s = new Subscriber__c();         
                        s.MDLIVE_ID__c = vals;   
                        s.id = subitems.Id;                      
                        lists.add(s);
                    }
                }
            }
        }                                   
    }
    if(lists != null && !Test.isRunningTest())
        update lists;  // display error in this line       
}

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.