function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Keith Stephens 18Keith Stephens 18 

Trigger testing

Hello all,
I am new with Apex and Salesforce so bare with me.
Up until now all the triggers I have written work just fine in Dev/QA and production.
But now I have this one trigger that needs a test class, but I am not sure where to being.
If someone can help me develop a test class that for the trigger below, then I should be able to understand how to test my triggers in the future. How to get the data needed to test the triggers ect...
Thanks,
Keith.
trigger MostRecentCenter on Procedure__c (after insert, after update) {
   try{           
            
           Set<Id> procIds = new Set<Id>();
           Set<Id> caseIds = new Set<Id>();
       List<Case> lstCase = new List<Case>();
               
               for(Procedure__c proc : Trigger.New)
               {                 
                   if(proc.CreatedDate != null) 
                   { 
                       procIds.add(proc.LastModifiedById);  
                       caseIds.add(proc.Case__c);                      
                   }                        
               }                        
               
              Map<string, Procedure__c> procMap = new map<string, Procedure__c> ( [SELECT CenterID__c, Center__c from Procedure__c 
               where LastModifiedById IN: procIds Order by CreatedDate Desc] );     

              Map<string, Case> caseMap = new map<string, Case> ( [SELECT Id, Most_Recent_Center__c FROM Case WHERE Id IN: caseIds] );  
                         
                 for(Procedure__c pd: trigger.new) 
                 {       
                     //system.debug(pd.id);

                    if(procMap.containsKey(pd.id)) 
                    {                 
                      
                        //Id centerID = (Id)procMap.get(pd.id).Center__c;
                        //Case cs = (Case)[SELECT Id, Most_Recent_Center__c FROM Case WHERE Id = :pd.Case__c];
                        //cs.Most_Recent_Center__c = centerID;
            caseMap.get(pd.Case__c).Most_Recent_Center__c = procMap.get(pd.id).Center__c;
                        lstCase.add(caseMap.get(pd.Case__c));                      
                                                               
                    }       
                }
                //system.debug(lstCase.size());
        if(lstCase.size()>0)
          update lstCase;
                
          //  }    //End of trigger.isBefore
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('MostRecentCenter');
    }
    
}

 
Best Answer chosen by Keith Stephens 18
Hemant_SoniHemant_Soni
Hi Keith,
Try Below Code.
@isTest
public class MostRecentCenterTest{
@isTest	
				 public static void UnitTest(){
				
				case oCase = new Case();
				oCase.subject = 'Test Case';
				oCase.Status = 'Open';
				insert oCase;
				
				
				list<Procedure__c>lstProcedure = new list<Procedure__c >();
				Procedure__c oPro = new Procedure__c ();
				oPro.Name = 'Test';
				oPro.Case__c = oCase.Id;
				lstProcedure.add(oPro);
				insert lstProcedure;
				
				lstProcedure[0].Name = 'Test1';
				update lstProcedure[0];
				
				}					

}
IF this helps you then mark it solved.
Thanks
Hemant
 

All Answers

Hemant_SoniHemant_Soni
Hi Keith,
Try Below Code.
@isTest
public class MostRecentCenterTest{
@isTest	
				 public static void UnitTest(){
				
				case oCase = new Case();
				oCase.subject = 'Test Case';
				oCase.Status = 'Open';
				insert oCase;
				
				
				list<Procedure__c>lstProcedure = new list<Procedure__c >();
				Procedure__c oPro = new Procedure__c ();
				oPro.Name = 'Test';
				oPro.Case__c = oCase.Id;
				lstProcedure.add(oPro);
				insert lstProcedure;
				
				lstProcedure[0].Name = 'Test1';
				update lstProcedure[0];
				
				}					

}
IF this helps you then mark it solved.
Thanks
Hemant
 
This was selected as the best answer
Keith Stephens 18Keith Stephens 18
Had to remove both the xxx.Name because these were read only, but it worked.
thank you a bunch.
Keith.