• ApexNewbie77
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies

Hello,
I am getting this error: Too many Email Invocations: 11

Is there any way around this? I tried to run a batch with a scope of 1 but its looks like I need to keep it at 200 otherwise I get this error: 

No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

Below is my code:

Batch Class:
 

global class BatchRenewal implements Database.Batchable<sObject>{
    
    public String query = 'SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL';
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Account> acct)
    {
            
            Messaging.SingleEmailMessage AlertOver = new Messaging.SingleEmailMessage(); //Alert when greater than or equal to 2500
            Messaging.SingleEmailMessage AlertUnder = new Messaging.SingleEmailMessage(); //Alert  when less than 2500
            
            String[] toAdd1 = new String[] 
            {
               test@test.com
            };
            
            String[] toAdd2 =  new String[] 
            {
                test1@test.com
            };
            
            //Account[] acct = [SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL];
            
            for(Account a: acct)
            {
                String sURL = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+a.Id;
                Date tod = System.today();
                System.debug('%%%%%%%%% '+tod);
                Date contEnd = a.Contract_End_Date__c;
                System.debug('&&&&& '+contEnd);
                Integer dayDiff = contEnd.daysBetween(tod);
                System.debug('******* '+dayDiff);
                
                
                if(dayDiff == 60)
                {    
                    Integer days = 60;
                    
                    if(a.Contract_Amount__c >= 2500)
                    {
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertOver.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertOver.setToAddresses(toAdd1);
                        AlertOver.setSaveAsActivity(False);
                        AlertOver.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertOver
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                    
                    }
                    else
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertUnder.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertUnder.setToAddresses(toAdd2);
                        AlertUnder.setSaveAsActivity(False);
                        AlertUnder.setHtmlBody(bod);
                        
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertUnder
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }
                }
                
                if(dayDiff == 30)
                {
                    Integer days = 30;
                    
                    if(a.Contract_Amount__c >= 2500)
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertOver.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertOver.setToAddresses(toAdd1);
                        AlertOver.setSaveAsActivity(False);
                        AlertOver.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertOver
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }
                    else
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertUnder.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertUnder.setToAddresses(toAdd2);
                        AlertUnder.setSaveAsActivity(False);
                        AlertUnder.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertUnder
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }    
                }
            }
            
        }
        
       
   
    
    global void finish(Database.BatchableContext BC){      
        
    }
}


Schedule Class:

global class ScheduleBatchRenewal implements Schedulable {
   global void execute(SchedulableContext sc) {
      BatchRenewal b = new BatchRenewal(); 
      database.executebatch(b, 1);
   }
}


Test Class:​

@isTest
private class BatchRenewalTest
{
    @testSetup
    static void setup() 
    {
        List<Account> acct = new List<Account>();
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Apple'+i;
            Integer am = 250 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,6,10),Contract_Amount__c=am));
        }
        
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Orange'+i;
            Integer am = 5000 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,6,10),Contract_Amount__c=am));
        }
        
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Banana'+i;
            Integer am = 250 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,7,10),Contract_Amount__c=am));
        }
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Grape'+i;
            Integer am = 5000 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,7,10),Contract_Amount__c=am));
        }
        
        insert acct;
        System.debug(Acct);
        
    }
    
    static testmethod void test() 
    { 
    	Test.startTest();
        BatchRenewal uca = new BatchRenewal();
        uca.query='SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL';
        Id batchId = Database.executeBatch(uca, 200);
        
        ScheduleBatchRenewal sch = new ScheduleBatchRenewal();
        sch.execute(null);
        
        Test.stopTest();
    }
}

Any help would be greatly appreciated!

 

Hello,
I am trying to create an apex class that will send an email alert 60 days and 45 days before a contract end date. Here are the requirements:
On the account object, there is a date field called, Contract_End_Date__c. An alert must be sent to a user 60 days and 45 days before the Contract_End_Date__c. An alert is sent to one person when the Contract_Amount__c is greater than or equal to 2500 and to a different person when the Contract_Amount__c is less than 2500.

My idea was to create an apex class with a scheduler class and have it run every night to check if Today's date is 60 days or 45 days from the Contract_End_Date__c. Below is my code but I am getting this error: Dependent class is invalid and needs recompilation: Class RenewCredit : Static method cannot be referenced from a non static context: void CreditRenewalNotification.NotifyRenewal()

 

Apex Class: 

public with sharing class CreditRenewalNotification
{
    
    public static void NotifyRenewal()
    {
        Messaging.SingleEmailMessage AlertOver = new Messaging.SingleEmailMessage(); //Alert when greater than or equal to 2500
        Messaging.SingleEmailMessage AlertUnder = new Messaging.SingleEmailMessage(); //Alert when less than 2500
        
        String[] toAdd1 = new String[] 
        {
            'test@test.com'
        };
        
        String[] toAdd2 =  new String[] 
        {
            'tes1@test.com'
            
        };
        
        Account[] acct = [SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL];
        
        for(Account a: acct)
        {
            String sURL = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+a.Id;
            Date tod = System.today();
            System.debug('%%%%%%%%% '+tod);
            Date contEnd = a.Contract_End_Date__c;
            System.debug('&&&&& '+contEnd);
            Integer dayDiff = contEnd.daysBetween(tod);
            System.debug('******* '+dayDiff);
            
            
            if(dayDiff == 60)
            {    
                Integer days = 60;
                
            	if(a.Contract_Amount__c >= 2500)
                {
                    String bod = 'The following account will need to be renewed in '+days+'.<br/>';
                    bod += 'Name: '+a.Name+ '<br/>';
                    bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                    bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                    bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                    bod += 'Link: '+sURL+ '<br/>';
                    
                    AlertOver.setSubject('ALERT: '+a.Name+ 'has a contract expiring in '+days+' days.');
                    AlertOver.setToAddresses(toAdd1);
                    AlertOver.setSaveAsActivity(False);
                    AlertOver.setHtmlBody(bod);
                    
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertOver
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                
                }
                else
                {
                    
                    String bod = 'The following account will need to be renewed in '+days+'.<br/>';
                    bod += 'Name: '+a.Name+ '<br/>';
                    bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                    bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                    bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                    bod += 'Link: '+sURL+ '<br/>';
                    
                    AlertUnder.setSubject('ALERT: '+a.Name+ 'has a contract expiring in '+days+' days.');
                    AlertUnder.setToAddresses(toAdd2);
                    AlertUnder.setSaveAsActivity(False);
                    AlertUnder.setHtmlBody(bod);
                    
                    
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertUnder
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }
            }
            if(dayDiff == 45)
            {
                Integer days1 = 45;
                
                if(a.Contract_Amount__c >= 2500)
                {
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertOver
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }
                else
                {
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertUnder
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }    
            }
        }
    	
    }
}


Scheduler class:

global class RenewCredit implements Schedulable{
global void execute(SchedulableContext SC) {
        CreditRenewalNotification cred = new CreditRenewalNotification();
        cred.NotifyRenewal();
    }
}


Any help one this would be great! Thanks!
 
Hello,
I have created a custom object that we use to track different issues. Just like any custom object, I have the notes and attachements related list on there. On the object, there is a multi-select picklist field that contains a few different values. I wrote the following apex trigger to send an email notification when a new NOTE is added. The email will be sent to specific address based upon the value in the muli-select picklist. I am trying to figure out how to add a secuence to the subject line. So everytime a note is added the subject shows that it is the first, third, eighth, etc. 

Example: A new custom object record is created. The user creates a new note, which will trigger and send an email notification to a specified email. Since that was the first note added for that record, the subject line should say, "Update 1: A new note has been added to Custom Object Record. When another note is added, the subject like should say, "Update 2: A new note has been added to Custom Object Record". The number should increment for every new note added.

Any help one this would be great! Thanks!
trigger NoteAddedNotification on Note (after insert) 
{
    
    Set<ID> noteids = new Set<ID>();
    
    for (Note n:Trigger.new)
    {
        
        noteids.add(n.parentID);
    }
    
    List<Custom_Object__c> notelist = new List<Custom_Object__c>([SELECT Id, Name, MultiSelect_Picklist__c FROM Custom_Object__c WHERE ID in:noteids]);
    
     Messaging.SingleEmailMessage NoteToCS = new Messaging.SingleEmailMessage();
    
    	String[] toAddresses1 = new String[]
        {
        	'email1@test.com'
    	};
            
        String[] toAddresses2 = new String[]
        {
            'email2@test.com', 
        };    
        
        String[] toAddresses3 = new String[]{
        	'email3@test.com'    
        };
           
    for(Custom_Object__c c:notelist)
    {
    
        NoteToCS.setSubject('A note has been added to Custom Object: ' +c.Name);
        
        String stringURL  = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+ c.Id;
        
        String body = 'A new note was added to ' + c.Name + '<br/>';
        body += 'Multi-Select Picklist: ' + c.MultiSelect_Picklist__c + '<br/>';
        body += 'Please click on the link to take a look:' + '<br/>';
        body += stringURL;
        
        
         if(String.isEmpty(c.MultiSelect_Picklist__c) && c.MultiSelect_Picklist__c == null)
         {
            
                c.addError('Please select a value for Multi-select Picklist before adding a 
                note.');

         }
        else
        {
               if(c.MultiSelect_Picklist__c.contains('Value1'))
               {
            		NoteToCS.setToAddresses(toAddresses1);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});
        		}
        		else if(c.MultiSelect_Picklist__c.contains('Value2'))
                {
            		NoteToCS.setToAddresses(toAddresses2);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});
        		}
        		else if(c.MultiSelect_Picklist__c.contains('Value3'))
                {
        			NoteToCS.setToAddresses(toAddresses3);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});	                             
        		}
        }
        
    }
update notelist ;
}

Hello,
I am getting this error: Too many Email Invocations: 11

Is there any way around this? I tried to run a batch with a scope of 1 but its looks like I need to keep it at 200 otherwise I get this error: 

No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

Below is my code:

Batch Class:
 

global class BatchRenewal implements Database.Batchable<sObject>{
    
    public String query = 'SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL';
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Account> acct)
    {
            
            Messaging.SingleEmailMessage AlertOver = new Messaging.SingleEmailMessage(); //Alert when greater than or equal to 2500
            Messaging.SingleEmailMessage AlertUnder = new Messaging.SingleEmailMessage(); //Alert  when less than 2500
            
            String[] toAdd1 = new String[] 
            {
               test@test.com
            };
            
            String[] toAdd2 =  new String[] 
            {
                test1@test.com
            };
            
            //Account[] acct = [SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL];
            
            for(Account a: acct)
            {
                String sURL = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+a.Id;
                Date tod = System.today();
                System.debug('%%%%%%%%% '+tod);
                Date contEnd = a.Contract_End_Date__c;
                System.debug('&&&&& '+contEnd);
                Integer dayDiff = contEnd.daysBetween(tod);
                System.debug('******* '+dayDiff);
                
                
                if(dayDiff == 60)
                {    
                    Integer days = 60;
                    
                    if(a.Contract_Amount__c >= 2500)
                    {
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertOver.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertOver.setToAddresses(toAdd1);
                        AlertOver.setSaveAsActivity(False);
                        AlertOver.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertOver
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                    
                    }
                    else
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertUnder.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertUnder.setToAddresses(toAdd2);
                        AlertUnder.setSaveAsActivity(False);
                        AlertUnder.setHtmlBody(bod);
                        
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertUnder
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }
                }
                
                if(dayDiff == 30)
                {
                    Integer days = 30;
                    
                    if(a.Contract_Amount__c >= 2500)
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertOver.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertOver.setToAddresses(toAdd1);
                        AlertOver.setSaveAsActivity(False);
                        AlertOver.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertOver
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }
                    else
                    {
                        
                        String bod = 'The following account will need to be renewed in '+days+' days.<br/>';
                        bod += 'Name: '+a.Name+ '<br/>';
                        bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                        bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                        bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                        bod += 'Link: '+sURL+ '<br/>';
                        
                        AlertUnder.setSubject('ALERT: '+a.Name+ ' has a contract expiring in '+days+' days.');
                        AlertUnder.setToAddresses(toAdd2);
                        AlertUnder.setSaveAsActivity(False);
                        AlertUnder.setHtmlBody(bod);
                        
                        try {
                                Messaging.sendEmail(new Messaging.Email[] {
                                    AlertUnder
                                });
                            } catch (Exception e) {
                                System.debug('An unexpected error has occurred: ' + e.getMessage());
                            }
                        
                    }    
                }
            }
            
        }
        
       
   
    
    global void finish(Database.BatchableContext BC){      
        
    }
}


Schedule Class:

global class ScheduleBatchRenewal implements Schedulable {
   global void execute(SchedulableContext sc) {
      BatchRenewal b = new BatchRenewal(); 
      database.executebatch(b, 1);
   }
}


Test Class:​

@isTest
private class BatchRenewalTest
{
    @testSetup
    static void setup() 
    {
        List<Account> acct = new List<Account>();
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Apple'+i;
            Integer am = 250 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,6,10),Contract_Amount__c=am));
        }
        
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Orange'+i;
            Integer am = 5000 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,6,10),Contract_Amount__c=am));
        }
        
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Banana'+i;
            Integer am = 250 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,7,10),Contract_Amount__c=am));
        }
        for(Integer i=0;i<3;i++)
        {
            String nam = 'Grape'+i;
            Integer am = 5000 + i;
            acct.add(new Account(Name=nam, Type='RAW',Sales_Team__c='B2B', Contract_End_Date__c=Date.newInstance(2018,7,10),Contract_Amount__c=am));
        }
        
        insert acct;
        System.debug(Acct);
        
    }
    
    static testmethod void test() 
    { 
    	Test.startTest();
        BatchRenewal uca = new BatchRenewal();
        uca.query='SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL';
        Id batchId = Database.executeBatch(uca, 200);
        
        ScheduleBatchRenewal sch = new ScheduleBatchRenewal();
        sch.execute(null);
        
        Test.stopTest();
    }
}

Any help would be greatly appreciated!

 

Hello,
I am trying to create an apex class that will send an email alert 60 days and 45 days before a contract end date. Here are the requirements:
On the account object, there is a date field called, Contract_End_Date__c. An alert must be sent to a user 60 days and 45 days before the Contract_End_Date__c. An alert is sent to one person when the Contract_Amount__c is greater than or equal to 2500 and to a different person when the Contract_Amount__c is less than 2500.

My idea was to create an apex class with a scheduler class and have it run every night to check if Today's date is 60 days or 45 days from the Contract_End_Date__c. Below is my code but I am getting this error: Dependent class is invalid and needs recompilation: Class RenewCredit : Static method cannot be referenced from a non static context: void CreditRenewalNotification.NotifyRenewal()

 

Apex Class: 

public with sharing class CreditRenewalNotification
{
    
    public static void NotifyRenewal()
    {
        Messaging.SingleEmailMessage AlertOver = new Messaging.SingleEmailMessage(); //Alert when greater than or equal to 2500
        Messaging.SingleEmailMessage AlertUnder = new Messaging.SingleEmailMessage(); //Alert when less than 2500
        
        String[] toAdd1 = new String[] 
        {
            'test@test.com'
        };
        
        String[] toAdd2 =  new String[] 
        {
            'tes1@test.com'
            
        };
        
        Account[] acct = [SELECT Id, Name, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c FROM Account WHERE Contract_End_Date__c != NULL AND Contract_Amount__c != NULL];
        
        for(Account a: acct)
        {
            String sURL = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+a.Id;
            Date tod = System.today();
            System.debug('%%%%%%%%% '+tod);
            Date contEnd = a.Contract_End_Date__c;
            System.debug('&&&&& '+contEnd);
            Integer dayDiff = contEnd.daysBetween(tod);
            System.debug('******* '+dayDiff);
            
            
            if(dayDiff == 60)
            {    
                Integer days = 60;
                
            	if(a.Contract_Amount__c >= 2500)
                {
                    String bod = 'The following account will need to be renewed in '+days+'.<br/>';
                    bod += 'Name: '+a.Name+ '<br/>';
                    bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                    bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                    bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                    bod += 'Link: '+sURL+ '<br/>';
                    
                    AlertOver.setSubject('ALERT: '+a.Name+ 'has a contract expiring in '+days+' days.');
                    AlertOver.setToAddresses(toAdd1);
                    AlertOver.setSaveAsActivity(False);
                    AlertOver.setHtmlBody(bod);
                    
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertOver
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                
                }
                else
                {
                    
                    String bod = 'The following account will need to be renewed in '+days+'.<br/>';
                    bod += 'Name: '+a.Name+ '<br/>';
                    bod += 'Contract Start Date: '+a.Contract_Start_Date__c+ '<br/>';
                    bod += 'Contract End Date: '+a.Contract_End_Date__c+ '<br/>';
                    bod += 'Contract Amount: $'+a.Contract_Amount__c+ '<br/>';
                    bod += 'Link: '+sURL+ '<br/>';
                    
                    AlertUnder.setSubject('ALERT: '+a.Name+ 'has a contract expiring in '+days+' days.');
                    AlertUnder.setToAddresses(toAdd2);
                    AlertUnder.setSaveAsActivity(False);
                    AlertUnder.setHtmlBody(bod);
                    
                    
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertUnder
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }
            }
            if(dayDiff == 45)
            {
                Integer days1 = 45;
                
                if(a.Contract_Amount__c >= 2500)
                {
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertOver
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }
                else
                {
                    try {
                            Messaging.sendEmail(new Messaging.Email[] {
                                AlertUnder
                            });
                        } catch (Exception e) {
                            System.debug('An unexpected error has occurred: ' + e.getMessage());
                        }
                    
                }    
            }
        }
    	
    }
}


Scheduler class:

global class RenewCredit implements Schedulable{
global void execute(SchedulableContext SC) {
        CreditRenewalNotification cred = new CreditRenewalNotification();
        cred.NotifyRenewal();
    }
}


Any help one this would be great! Thanks!