+ Start a Discussion
bouscalbouscal 

How to call method from trigger

With zero OOP experience Apex is turning out to be quite a challenge.  

I have the following trigger in the system already and want to call a new method from a new class.  How do I do that?
This is still being built so there's a lot of holes in the class file yet to be filled.  I have attempted to duplicate what is in there passing in either trigger.new or trigger.old but both fail.  It only needs to fire After and Update so I know that it only needs to be in the blcok immediately following the if statement.
trigger Cases on Case (after delete, after insert, after undelete, after update) {

  ClassNameOne cno = new ClassNameOne();
    
  if(Trigger.isAfter && Trigger.isUpdate) {
    cno.linkUpdate(Trigger.new, Trigger.old);
HOL_NA_CaseEscalated.processCase(Trigger.old); // THIS IS WHERE THE ERROR MESSAGE REFERS TO LINE 15
  }
  else if (Trigger.isAfter && Trigger.isDelete) {
    cno.linkUpdate(Trigger.old);
  }
  else if (Trigger.isAfter && Trigger.isInsert) {
    cno.linkUpdate(Trigger.new);  
  }
  else if (Trigger.isAfter && Trigger.isUndelete) {
        cno.linkUpdate(Trigger.new);
  }
}

Here's my Class
public class HOL_NA_CaseEscalated {
    public void processCase(Case[] cs) 
    {
// Create a lists to hold any new Cases to insert or existing Cases to update
		List<Case> newCases = new List<Case>();  // Cases to add  
        List<Case> myUpdates = new List<Case>();   // Cases to update

        // create a map of record types
        Map<string,id> rType = new Map<string,id>();
        for (RecordType rt : [SELECT id, developername FROM RecordType WHERE isactive=TRUE and sobjecttype='Case']){
         	rType.put(rt.DeveloperName, rt.Id);  // add key/value pairs to map so we can lookup ID by name
        }
		id rsc = rType.get('Recycling_Support_Case'); 
        id pdc = rType.get('HOL_NA_Escalated_Case'); 
        System.debug('***** Recycling Support Case is recordtypeid ' + rsc + ' *****'); 
        System.debug('***** Esclated Case is recordtypeid ' + pdc + ' *****'); 
        // Create a collections of ids
        Set<id> RecIds; 
        Set<id> EscIds; 

        for(Case c:cs){
            if(c.RecordTypeId==rsc){
            	RecIds.add(c.id);    // ERROR MESSAGE POINTS TO THIS 
            } 
            if (c.RecordTypeId==pdc){
                EscIds.add(c.id); 
            } 
        }

// List all child cases on the triggering Case
        List<Case> RecChildren = new List<Case>();
        RecChildren=[SELECT id,status, casenumber,parentid,subject, recordtypeid FROM Case WHERE parentid IN:RecIds];
        
        for(Case c:cs){
            if (c.recordtypeid==rsc && c.Escalated_To__c=='Escalated to Development'){ // This is a Recycling Support Case
                // check child cases for the existence of a HOL NA Escalated Case
                if(c.Is_a_Parent_Case__c==true && c.RecordTypeId==pdc){
                	// if one exists, edit it - set Status to Open
                	c.Status='Open';
                    myUpdates.add(c);  // Add to list of Cases to update later.
                } else {
                	// if one does not exist, create it  
					Case newCase = new Case();
                    newCase.RecordTypeId=pdc;
                    newCase.Status='New';
                    newCase.Subject='Escalation: ' + c.Subject;
                    newCase.Case_Name__c=c.Case_Name__c;
                    newCase.AccountId=c.AccountId;
                    newCase.ContactId=c.ContactId;
                    newCase.Case_Phone_Number__c=c.Case_Phone_Number__c;
                    newCase.Business_Impact_Frequency__c=c.Business_Impact_Frequency__c;
                    newCase.Business_Impact_Scope__c=c.Business_Impact_Scope__c;
                    newCase.Severity__c=c.Severity__c;
                    newCase.Priority=c.Priority;
					newCase.Defect_Escalation_Questions__c=c.Defect_Escalation_Questions__c;
                    newCases.add(newCase);
                }
            } else if (c.recordtypeid==pdc){ // This is a HOL NA Escalated Case
                // find the parent Case and update it
                
            } else {
                // do nothing
            }
        }
        
        if(newCases.size() > 0){
                insert newCases;
        }
        
        if(myUpdates.size()>0){
            update myUpdates;
        }
	}
}
And the text of my error message states; 
System.DmlException: Update failed. First exception on row 0 with id 500S0000007LVRdIAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Cases: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.HOL_NA_CaseEscalated.processCase: line 22, column 1
Trigger.Cases: line 15, column 1: []


 
Best Answer chosen by bouscal
bouscalbouscal
Thanks for the input Pratik but it turns out how I was calling it was correct all along.  The error is occurring elsewhere and so I'll post a new question for that.

All Answers

PratikPratik (Salesforce Developers) 
Hi Bouscal,

Here is the sample code example for calling method from trigger:


Trigger:
 
trigger createcontopp on account(after insert) {

list<id> accids = new list<id>();
    for(account a:trigger.new) {
    
    accids.add(a.id);
    
    }
    
    contopptrgr c = new contopptrgr();
    c.crtopp(accids);
    c.crtcont(accids);
    
    
}

Class:
 
global class contopptrgr {

list<opportunity> opplist=new list<opportunity>();
list<contact> conlist=new list<contact>();
    public void crtopp(list<id> accid){
         list<account> alist=[select id,name from account where id IN : accid];
         for(account a:alist) {
        opportunity opp=new opportunity();
        opp.accountid=a.id;
        opp.name='opportunity on account ' + a.name; 
        opp.stagename='Prospecting';
        opp.closedate= date.today();
        opplist.add(opp);
        
        }
        insert opplist;
    }
    
    Public void crtcont(list<id> accid){
         list<account> alist=[select id,name from account where id IN : accid];
         for(account at:alist) {
        
        contact c=new contact();
        c.accountid=at.id;
        c.firstname='account';
        c.lastname=at.name;
        conlist.add(c);   
    }
        insert conlist;
    }


}

Thanks,
Pratik
bouscalbouscal
Thanks for the input Pratik but it turns out how I was calling it was correct all along.  The error is occurring elsewhere and so I'll post a new question for that.
This was selected as the best answer