+ Start a Discussion
SrinivasSrinivas 

How to add try catch

How to add try catch for error handling for the below code

 

global class updatePolicyStatusSchedule implements Schedulable {
    global void execute(SchedulableContext ctx) {
        updatePolicyStatus();
    }
    
    public void updatePolicyStatus() {
        List<Policy__c> expiredInForcePolicies = [SELECT Id, Status__c, Policy_End_Date__c, Policy_Effective_Date__c
                                                  FROM Policy__c
                                                  WHERE (Policy_End_Date__c <= TODAY AND Status__c != 'Expired' AND Status__c != 'Terminated')
                                                  OR (Policy_Effective_Date__c <= TODAY AND Policy_End_Date__c > TODAY AND Status__c NOT IN ('In Force','In-Force - Non-Renewing'))];

        List<Policy__c> updateExpiredInForcePolicies = new List<Policy__c>();
        

        for(Policy__c policy : expiredInForcePolicies){
            if((policy.Policy_End_Date__c<=System.today() && policy.Status__c!='Expired' && policy.Status__c!='Terminated') && !(policy.Policy_Effective_Date__c <= System.today() && policy.Policy_End_Date__c > System.today() && (policy.Status__c!='In Force' || policy.Status__c!='In-Force - Non-Renewing'))){
                policy.Status__c = 'Expired';
            }

            if(!(policy.Policy_End_Date__c<=System.today() && policy.Status__c!='Expired' && policy.Status__c!='Terminated') && (policy.Policy_Effective_Date__c <= System.today() && policy.Policy_End_Date__c > System.today() && (policy.Status__c!='In Force' || policy.Status__c!='In-Force - Non-Renewing'))){
                policy.Status__c = 'In Force';
            }
            
            updateExpiredInForcePolicies.add(policy);
        }
    if(!updateExpiredInForcePolicies.isEmpty()){
            Database.update(updateExpiredInForcePolicies);  
        }
    }
}


Thanks In advance

Best Answer chosen by Srinivas
CharuDuttCharuDutt
Hii Srinivas
Try Below  Code
global class updatePolicyStatusSchedule implements Schedulable {
    global void execute(SchedulableContext ctx) {
        updatePolicyStatus();
    }
    
    public void updatePolicyStatus() {
        List<Policy__c> expiredInForcePolicies = [SELECT Id, Status__c, Policy_End_Date__c, Policy_Effective_Date__c
                                                  FROM Policy__c
                                                  WHERE (Policy_End_Date__c <= TODAY AND Status__c != 'Expired' AND Status__c != 'Terminated')
                                                  OR (Policy_Effective_Date__c <= TODAY AND Policy_End_Date__c > TODAY AND Status__c NOT IN ('In Force','In-Force - Non-Renewing'))];

        List<Policy__c> updateExpiredInForcePolicies = new List<Policy__c>();
        

        for(Policy__c policy : expiredInForcePolicies){
            if((policy.Policy_End_Date__c<=System.today() && policy.Status__c!='Expired' && policy.Status__c!='Terminated') && !(policy.Policy_Effective_Date__c <= System.today() && policy.Policy_End_Date__c > System.today() && (policy.Status__c!='In Force' || policy.Status__c!='In-Force - Non-Renewing'))){
                policy.Status__c = 'Expired';
            }

            if(!(policy.Policy_End_Date__c<=System.today() && policy.Status__c!='Expired' && policy.Status__c!='Terminated') && (policy.Policy_Effective_Date__c <= System.today() && policy.Policy_End_Date__c > System.today() && (policy.Status__c!='In Force' || policy.Status__c!='In-Force - Non-Renewing'))){
                policy.Status__c = 'In Force';
            }
            
            updateExpiredInForcePolicies.add(policy);
        }
    if(!updateExpiredInForcePolicies.isEmpty()){
	try {
 
            Database.update(updateExpiredInForcePolicies);  
		} catch(DmlException e) {
			System.debug('The following exception has occurred: ' + e.getMessage());
		}
	
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!