• Zen Newman 7
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Scheduled Apex Troubleshooting
Hello- I'm trying to build a scheduled apex class that'll run a series of queries to identify leads to remove from the system and then create a record on a custom object and populate a couple fields with the number of leads being deleted. I'm not throwing any errors, but it's failing to either delete the leads or create the record on the custom object. I'm also struggling with how to build a test class that covers this use case. Appreciate any guidance on where I'm going wrong!

Main Class:
global class LeadRemoval implements Schedulable {
    public static String CRON_EXP = '0 0 0 * * ? *';
    global void execute(SchedulableContext SC){}
    public void removal () {
        List<Lead> remove = new List<Lead>();      
    }
    public static void dqReason(List<Lead> remove) {
        List<Lead> dq = new List<Lead>(); 
		dq = [SELECT Id FROM Lead WHERE Disqualification_Reason__c = 'Not a fit' OR Disqualification_Reason__c = 'Single Seat' OR
            Disqualification_Reason__c = 'Spam' OR Disqualification_Reason__c = 'Support' OR Disqualification_Reason__c = 'Student'
            OR Disqualification_Reason__c = 'Ad Sales Request' OR Disqualification_Reason__c = 'Duplicate'];
		
        remove.addall(dq);          
    }
    public static void emailBounce(List<Lead> remove) {
        List<Lead> b = new List<Lead>(); 
        b = [SELECT Id FROM Lead WHERE mkto_si__Last_Interesting_Moment__c LIKE '%unsubscribe%'];
        remove.addall(b); 
    }
    public static void staleLead(List<Lead> remove) {
        List<Lead> s = new List<Lead>(); 
        for(lead l: [SELECT Id, Email, mkto_si__Last_Interesting_Moment_Date__c, OwnerId FROM Lead 
                    WHERE OwnerId = '00G1i000000GKUTEA4']) {
                        integer Stale = (system.today()).daysBetween(Date.valueOf(l.mkto_si__Last_Interesting_Moment_Date__c));
                        if((l.Email.contains('gmail') || l.Email.contains('aol') || l.Email.contains('yahoo')|| 
                            l.Email.contains('msn')|| l.Email.contains('hotmail')) && Stale > 30) 
                        {
                        	s.add(l);    
                        }else if(Stale > 270) { 
                        	s.add(l);
                        }
         remove.addall(s);                 
        }     
    }
    public static void toDelete(List<Lead> remove) {
        Purge_Snapshot__c ps = new Purge_Snapshot__c ();
        ps.Disqualified_Total__c = [SELECT COUNT() FROM Lead WHERE Disqualification_Reason__c = 'Not a fit' OR Disqualification_Reason__c = 'Single Seat' OR
            Disqualification_Reason__c = 'Spam' OR Disqualification_Reason__c = 'Support' OR Disqualification_Reason__c = 'Student'
            OR Disqualification_Reason__c = 'Ad Sales Request' OR Disqualification_Reason__c = 'Duplicate'];
        ps.Bounced_Unsubscribed_Total__c = [SELECT COUNT() FROM Lead WHERE mkto_si__Last_Interesting_Moment__c LIKE '%unsubscribe%'];
        ps.Unengaged_Total__c = [SELECT COUNT() FROM Lead WHERE OwnerId = '00G1i000000GKUTEA4'];
        ps.Total_Removed_Leads__c = ps.Disqualified_Total__c + ps.Bounced_Unsubscribed_Total__c + ps.Unengaged_Total__c;
        
        insert ps; 
    	delete remove; 
    }
}
Test Class
@isTest
public class LeadRemovalTest {
    static testMethod void removeDQ() {
        
    	Lead l = new Lead(Company = 'Test', LastName = 'Test', Status = 'Unqualified', Disqualification_Reason__c = 'Spam');
        System.debug('The ' + l.Company + ' is in status ' + l.Status + ' for ' + l.Disqualification_Reason__c + ' reason'); 
        
        insert l; 
        
        test.startTest(); 
        String jobId = System.schedule('ScheduledLeadRemovalTestClass', LeadRemoval.CRON_EXP ,new LeadRemoval());  

        test.stopTest();
        l = [SELECT Id, IsDeleted FROM Lead WHERE Id =: l.Id];
        System.assertEquals(l.IsDeleted, true);
        Purge_Snapshot__c ps = [SELECT Name, Disqualified_Total__c FROM Purge_Snapshot__c WHERE Purge_Date__c = TODAY]; 
        System.assertEquals(ps.Disqualified_Total__c, 1); 
        
    } 
}


 
Hello- I'm trying to build a scheduled apex class that'll run a series of queries to identify leads to remove from the system and then create a record on a custom object and populate a couple fields with the number of leads being deleted. I'm not throwing any errors, but it's failing to either delete the leads or create the record on the custom object. I'm also struggling with how to build a test class that covers this use case. Appreciate any guidance on where I'm going wrong!

Main Class:
global class LeadRemoval implements Schedulable {
    public static String CRON_EXP = '0 0 0 * * ? *';
    global void execute(SchedulableContext SC){}
    public void removal () {
        List<Lead> remove = new List<Lead>();      
    }
    public static void dqReason(List<Lead> remove) {
        List<Lead> dq = new List<Lead>(); 
		dq = [SELECT Id FROM Lead WHERE Disqualification_Reason__c = 'Not a fit' OR Disqualification_Reason__c = 'Single Seat' OR
            Disqualification_Reason__c = 'Spam' OR Disqualification_Reason__c = 'Support' OR Disqualification_Reason__c = 'Student'
            OR Disqualification_Reason__c = 'Ad Sales Request' OR Disqualification_Reason__c = 'Duplicate'];
		
        remove.addall(dq);          
    }
    public static void emailBounce(List<Lead> remove) {
        List<Lead> b = new List<Lead>(); 
        b = [SELECT Id FROM Lead WHERE mkto_si__Last_Interesting_Moment__c LIKE '%unsubscribe%'];
        remove.addall(b); 
    }
    public static void staleLead(List<Lead> remove) {
        List<Lead> s = new List<Lead>(); 
        for(lead l: [SELECT Id, Email, mkto_si__Last_Interesting_Moment_Date__c, OwnerId FROM Lead 
                    WHERE OwnerId = '00G1i000000GKUTEA4']) {
                        integer Stale = (system.today()).daysBetween(Date.valueOf(l.mkto_si__Last_Interesting_Moment_Date__c));
                        if((l.Email.contains('gmail') || l.Email.contains('aol') || l.Email.contains('yahoo')|| 
                            l.Email.contains('msn')|| l.Email.contains('hotmail')) && Stale > 30) 
                        {
                        	s.add(l);    
                        }else if(Stale > 270) { 
                        	s.add(l);
                        }
         remove.addall(s);                 
        }     
    }
    public static void toDelete(List<Lead> remove) {
        Purge_Snapshot__c ps = new Purge_Snapshot__c ();
        ps.Disqualified_Total__c = [SELECT COUNT() FROM Lead WHERE Disqualification_Reason__c = 'Not a fit' OR Disqualification_Reason__c = 'Single Seat' OR
            Disqualification_Reason__c = 'Spam' OR Disqualification_Reason__c = 'Support' OR Disqualification_Reason__c = 'Student'
            OR Disqualification_Reason__c = 'Ad Sales Request' OR Disqualification_Reason__c = 'Duplicate'];
        ps.Bounced_Unsubscribed_Total__c = [SELECT COUNT() FROM Lead WHERE mkto_si__Last_Interesting_Moment__c LIKE '%unsubscribe%'];
        ps.Unengaged_Total__c = [SELECT COUNT() FROM Lead WHERE OwnerId = '00G1i000000GKUTEA4'];
        ps.Total_Removed_Leads__c = ps.Disqualified_Total__c + ps.Bounced_Unsubscribed_Total__c + ps.Unengaged_Total__c;
        
        insert ps; 
    	delete remove; 
    }
}
Test Class:
@isTest
public class LeadRemovalTest {
    static testMethod void removeDQ() {
        
    	Lead l = new Lead(Company = 'Test', LastName = 'Test', Status = 'Unqualified', Disqualification_Reason__c = 'Spam');
        System.debug('The ' + l.Company + ' is in status ' + l.Status + ' for ' + l.Disqualification_Reason__c + ' reason'); 
        
        insert l; 
        
        test.startTest(); 
        String jobId = System.schedule('ScheduledLeadRemovalTestClass', LeadRemoval.CRON_EXP ,new LeadRemoval());  

        test.stopTest();
        l = [SELECT Id, IsDeleted FROM Lead WHERE Id =: l.Id];
        System.assertEquals(l.IsDeleted, true);
        Purge_Snapshot__c ps = [SELECT Name, Disqualified_Total__c FROM Purge_Snapshot__c WHERE Purge_Date__c = TODAY]; 
        System.assertEquals(ps.Disqualified_Total__c, 1); 
        
    } 
}


 
Hi All,

I have requirement where i have to assign the lead on the basis of Zip code to particular user (District head). And i have more then 1000 Zip code.

Please suggest what will be best apporach to this.

Thanks
Sid
.