Miscellaneous Ramblings on Hacking

August 13, 2008

Salesforce.com APEX – new record trigger

Filed under: apex, salesforce.com — Zack Bethem @ 4:53 pm

Bouncing around a little bit now, I’ve found myself on a Salesforce.com project (dare I say “trapped”?). Anyway, I did have some fun coding with the Force.com IDE.

In this scenario, I had to write my very first trigger. The requirement was to create a case upon the creation of a new record. My code ended up looking like this for the trigger.

trigger createCaseFromBroker on BrokerShop__c (after insert) {
    
    List<Group> sfdcGroup = [select Id, name from Group where Name = 'Support Queue' limit 1];

        if(Trigger.new.size() == 1){
        for(BrokerShop__c b:Trigger.new){
          //for each broker, b, create a new DueDiligence case
              Case newCase = new Case(
                Subject = 'Perform Due Diligence',
                Broker_Shop__c = b.Id,
                Status = 'Open',
                Priority = 'Medium',
                Category__c = 'Pre-Approval',
                Subcategory__c = 'How to become a Broker',
                Origin = 'Web'  //is there an origin? required in UI
              );
              
              //if broker has a primary contact
              if(b.Primary_Contact__c != null){
                  newCase.ContactId = b.Primary_Contact__c;
              }
              if(!sfdcGroup.isEmpty()){
                  newCase.OwnerId = sfdcGroup[0].Id;
              }
              insert newCase;
        }                  
        } 
}

A couple of notes on this:

  • This is the main trigger set to fire ‘after insert’
  • The trigger is based on a custom SFDC object

On to the unit testing. I will give credit to Salesforce on this one. Requiring unit tests with a minimum coverage of 75% is commendable. Annoying for those of us that just hack our way (me), but still commendable. On to the tests. I encapsulated all of the test cases into my own class file.

public class CreateCase {

  static testMethod void test_createCaseFromBroker(){
      
      //create contact record
      Contact contact = new Contact();
      contact.FirstName = 'Test 1 - contact';
      contact.LastName = 'Dummy';
      insert contact;
      String contactId = contact.Id;
      
    //create broker record #1
    BrokerShop__c broker1 = new BrokerShop__c();
    broker1.Name = 'Test 1 - broker';
    broker1.Street_Address__c = '123 Test St';
    broker1.City__c = 'Test';
    broker1.State__c = 'California';
    broker1.Zip__c = '00000';
    broker1.Primary_Contact__c = contactId;

    insert broker1;
    String broker1Id = broker1.Id;
    
    //validate case was created
    List<Case> bsCase = [
        SELECT Id,Broker_Shop__c
            FROM case 
            WHERE Broker_Shop__c = :broker1Id
            LIMIT 1
        ];
    for( Case c:bsCase ){
        System.assertEquals(broker1Id,c.Broker_Shop__c);
    }
    
    
  }
  
}

So there you have it. A insert trigger within Salesforce to create a related case record.

Blog at WordPress.com.