+ Start a Discussion
NWhite123NWhite123 

Auto Create Case trigger

Hi, I am an administrator rather than a developer and i am trying to create a trigger that will automatically create a case (i.e. a case is created at 1st of every month) Can anyone help with me with the code for this or advice?

Many Thanks

zachelrathzachelrath

Can you give more details as to what you're trying to accomplish? I suspect that workflow might be a better solution for you, but would need more detail on your use case.

 

Triggers fire as the result of changes to existing records in Salesforce --- i.e. if an existing Case's Priority changes from "Low" to "Critical", the Case Trigger could take some action.

 

However, if you're trying to create a Case automatically at the start of every month, this would probably be best done through Scheduled Apex. 

 

Regards,

 

Zach

NWhite123NWhite123

Hi,

 

Our accountancy team (who use Salesforce) have a case raised at the beginning of every financial year that may require work being done every month (a report or journals or anything like that) we do not want the case being open for a year as the work is done say on the 1 st of each month and it may be slightly different than the work the month befoe.

 

What we are looking for is for one case to be raised on the 1st april (e.g.) and then that case to trigger off creating a new case each month that appears in the accountancy queue.

 

Hope that makes more sense.

Thanks

zachelrathzachelrath

Here's an example of a Scheduled Apex class which will creates a new Case assigned to the Accountancy Queue. Once you have created and saved this class in your Production org, you can schedule it to run at the beginning of every month by going to: Setup > App Setup > Develop > Apex Classes > Schedule Apex. Select "CaseCreator" as the Apex Class to run, and then schedule it to run at the desired date/time intervals.

 

// 
// CaseCreator.cls --- can be scheduled to run at any time interval
//

global class CaseCreator implements Schedulable {

    // Callable from a Unit Test
    public void execute() {
        // Find the Accountancy Queue,
        // so that we can assign a new Case to it
        Group accountancyQueue;
       try {
            accountancyQueue = 
                [select Id 
                from Group 
                where Type = 'Queue' 
                and Name = 'Accountancy Queue' 
                limit 1];
        } catch (Exception ex) {
            System.debug('***Could not find Accountancy Queue');
        }
        if (accountancyQueue != null) {
            // Create a new Case assigned to the Accountancy Queue
            Case c = new Case(
                OwnerId = accountancyQueue.Id,
                Priority = 'Medium',
                Status = 'New',
                Subject = 'New Accountancy Queue Case',
                Description = 'Case Description'
            );
            // Try to insert our Case
            try {
                insert c;
            } catch (DMLException ex) {
                // Handle the error
            }
        }
    }

    global void execute(SchedulableContext sc) {
        execute();
    }

    /////////////////////
    // UNIT TESTS
    /////////////////////
    
    private static testMethod void TestCaseCreation() {
        // Try to obtain a reference to our Accountancy Queue
        Group accountancyQueue;
        Exception exCaught;
       try {
            accountancyQueue = 
                [select Id 
                from Group 
                where Type = 'Queue' 
                and Name = 'Accountancy Queue' 
                limit 1];
        } catch (Exception ex) {
            exCaught = ex;
        }
        System.assertEquals(null,exCaught,
            'Accountancy Queue not found. Please create it, then rerun this test.');
        
        // Now, simulate executing our Scheduled Case Creator.
        CaseCreator cc = new CaseCreator();
        cc.execute();
        
        // Verify that a new Case was created in the Accountancy Queue
        List<Case> cases
            = [select id, Subject
                from Case
                where OwnerId = :accountancyQueue.Id
                and CreatedDate = TODAY
                order by CreatedDate DESC
                limit 1];
        System.assertEquals(1,cases.size(),'cases.size()');
        
    }


}

 

 

 

NWhite123NWhite123

Hi

We have tried to create a new Apex Class with the code that you sent us (which was really helpful thank you) however we are getting this error

 

Error: Compile Error: unexpected token: '<EOF>' at line 0 column -1

 

Can you help us resolve this please?

 

Many Thanks

 

Natalie

zachelrathzachelrath

This error probably means that you have unclosed curly braces, i.e. something like the following would cause the error you are getting:

 

global class CaseCreator {

   public void MyMethod() {

}

 If you post your code, we can help try to spot the problem.

Sameer AdvaniSameer Advani
Hey im using the exact code mentioned here and i am getting this error

Error: Compile Error: Test methods must be in test classes at line 48 column 36

Any idea why?
Sameer AdvaniSameer Advani
Oh, looks like salesforce prevents test methods from being implemented within regular classes (starting version 28).  Does anyone have a test class method that works with the new versions of Salesforce?
Melissa MMelissa M
Sameer - were you able to figure this out?