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
JaiJai 

Please help me below future method code

Hi All,

In below code control is not entering into the future method (future method not getting executed) please help me on this 

ublic static void consumerCareSurveyCriteria(List<Case> caseList,map<id,Case> oldMap){
        Set<Id> caseRecordID = new Set<Id>();
        Map<String,String> caseSurveyMap =  new Map<String,String>();
        Map<String,String> caseSurvey2Map =  new Map<String,String>();
        Map<ID, RecordType> recordTypeMap = New Map<ID, RecordType>([Select ID, Name From RecordType Where sObjectType = 'Case']);
        for(Case caseRecord : caseList)
            {
                /*if(recordTypeMap.get(caseRecord.recordTypeId).name.containsIgnoreCase('Consumer Complaints') && (caseRecord.Invisalign_Pro_Survey__c != null || caseRecord.Invisalign_Pro_Survey_2__c != null)  && caseRecord.Patient_Email_Address__c !=null && caseRecord.Status == 'Closed')
                {
                   if(Trigger.isInsert) {
                    caseRecordID.add(caseRecord.Id);
                       system.debug('case details11 '+caseRecordID);
                } 
                }*/
                system.debug('boolean test2 '+!oldMap.isEmpty());
                system.debug('boolean test '+Trigger.isUpdate);
                system.debug('record type11 '+recordTypeMap.get(caseRecord.recordTypeId).name.containsIgnoreCase('Consumer Complaint'));
                Boolean flag1 = (caseRecord.Invisalign_Pro_Survey__c != null && oldMap.get(caseRecord.Id).Invisalign_Pro_Survey__c != caseRecord.Invisalign_Pro_Survey__c);
                system.debug('Flag111 '+flag1);
                if(Trigger.isUpdate && !oldMap.isEmpty() && recordTypeMap.get(caseRecord.recordTypeId).name.containsIgnoreCase('Consumer Complaint') && (caseRecord.Invisalign_Pro_Survey__c != null && oldMap.get(caseRecord.Id).Invisalign_Pro_Survey__c != caseRecord.Invisalign_Pro_Survey__c) || (caseRecord.Invisalign_Pro_Survey_2__c != null && oldMap.get(caseRecord.Id).Invisalign_Pro_Survey_2__c != caseRecord.Invisalign_Pro_Survey_2__c) && caseRecord.Status == 'Closed' && caseRecord.Status!=oldMap.get(caseRecord.Id).Status)
                {
                    system.debug('boolean test '+Trigger.isUpdate);
                    system.debug('boolean test1 '+!oldMap.isEmpty());
                    system.debug('record type '+recordTypeMap.get(caseRecord.recordTypeId).name.containsIgnoreCase('Consumer Complaint'));
                    system.debug('status '+caseRecord.Status);
                    Boolean flag = (caseRecord.Invisalign_Pro_Survey__c != null && oldMap.get(caseRecord.Id).Invisalign_Pro_Survey__c != caseRecord.Invisalign_Pro_Survey__c);
                    system.debug('Flag111 '+flag);
                   caseRecordID.add(caseRecord.Id);
                    system.debug('oldrectest '+oldMap.get(caseRecord.Id).Invisalign_Pro_Survey__c);
                    system.debug('surveyvalue '+caseRecord.Invisalign_Pro_Survey__c);
                    caseSurveyMap.put(caseRecord.Id,caseRecord.Invisalign_Pro_Survey__c);
                    caseSurvey2Map.put(caseRecord.Id,caseRecord.Invisalign_Pro_Survey_2__c);
                }
            }
                if(!caseRecordID.isEmpty()) { 
                    system.debug('case Id '+caseRecordID);
            createConsumerCareSurveyInvitation(caseRecordID,caseSurveyMap,Trigger.isUpdate,caseSurvey2Map);
                    
        }

    }
    
    @future
    public static void createConsumerCareSurveyInvitation(Set<Id> caseIds,Map<String,String> caseSurveyMap,Boolean isAftrUpdate,Map<String,String> caseSurvey2Map) {
        try {
            system.debug('update test '+isAftrUpdate);
                    system.debug('caseSurveyMap11 '+caseSurveyMap);
                    system.debug('caseSurveyMap22 '+caseSurvey2Map);
                    system.debug('caseRecordID1111 '+caseIds);
            final Set<String> setProgramNames = new Set<String>();
            List<Case> caseList;
            caseList = new List<Case> ();
            system.debug('case ids11 '+caseIds);
            caseList = [select id,Invisalign_Pro_Survey__c,Invisalign_Pro_Survey_2__c,TFM_Cluster_Contact__c,ContactId from case where id in: caseIds WITH SECURITY_ENFORCED];
            List<SurveyInvitation> lstSurveyInvitn ;
            lstSurveyInvitn = new List<SurveyInvitation>();
            system.debug('case listrec '+caseList);
            for(case caseRecord: caseList) {
                if(caseRecord.Invisalign_Pro_Survey__c != null) {
                    setProgramNames.add(caseRecord.Invisalign_Pro_Survey__c);
                }  
                if(caseRecord.Invisalign_Pro_Survey_2__c != null) {
                    setProgramNames.add(caseRecord.Invisalign_Pro_Survey_2__c); 
                }
                
            }
            
            //Query Survey to get the ID's based on program names
            final Map<String,Id> mapProgramName = new Map<String,Id>();
            
            for(Survey surveyRecord : [select id,name from Survey where name in: setProgramNames WITH SECURITY_ENFORCED]) {
                mapProgramName.put(surveyRecord.name,surveyRecord.id);
            } 
            lstSurveyInvitn = getConsumerCareSurveyInvitationLst(caseList,mapProgramName,isAftrUpdate,caseSurveyMap,caseSurvey2Map);
            //trRecList = getTrainingRecLst(caseList,mapProgramName,isAftrInsert,caseSurveyMap,caseSurvey2Map);
            if (!lstSurveyInvitn.isEmpty() && Schema.sObjectType.SurveyInvitation.isCreateable()) {
                system.debug('test invitation '+lstSurveyInvitn);
                insert lstSurveyInvitn;
            }
            /*if (!trRecList.isEmpty() && Schema.sObjectType.Training_Records__c.isCreateable()) {
                insert trRecList;
            }*/
            
        } catch(Exception e) {
            final UCB_ExceptionService exceptionService = new UCB_ExceptionService(userinfo.getuserid(), 'NASurveyHandler Error', 'NASurveyHandler Error', '');
            
            exceptionService.add(e);
            
            exceptionService.publish();
        }
    }

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
hi Jai,

Were you able to debug and check if it is entering into  if loop before calling the future method?

thanks,
 
JaiJai
Hi Sai,

I debugged it is entering the if condition before calling to future method.

User-added image

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jai,

This means it should be calling future method as well. Did you check in Apex jobs if it is calling the future method or not?

Thanks,