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
LaaralLaaral 

If the oldcase&updatedcase.relatedsetup is null

This code isn't quite right, but I don't know how should I put it in this. The program should not make anything if those values (marked red) are null.

 

trigger CaseServiceAvailabilityTrigger on Case (after insert) {
    if (Trigger.isInsert) {
        for (Case updatedCase:System.Trigger.new) {
            System.Debug('CaseServiceAvailabilityTrigger : NEW CASE: ' + updatedCase.Id);
            
            // TODO: What to do if there is no old Case?
            Case oldCase = new Case();
            if (System.Trigger.oldMap != null) {
                oldCase = System.Trigger.oldMap.get(updatedCase.Id);
            } else {
                oldCase = updatedCase;
            }   

            // TODO: What happens if the case doesn't have a Setup?
            if(oldCase.Related_Setup__c == null && updatedCase.Related_Setup__c == null ) {

            System.Debug('CaseServiceAvailabilityTrigger: NO Setup defined for Case: ' + updatedCase.Id);

           
            // Add SA object to Case            
            ServiceAvailability saHelper = new ServiceAvailability();                        
            RecordType recordTypeI = [select Id, Name from RecordType where SObjectType = 'Service_Availability__c' and Name = 'Incident' LIMIT 1];
            saHelper.InsertServiceAvailabilityObjectToCase(updatedCase, System.now(), recordTypeI, true);             
            
            // Add SA object also to related Setup
            if (updatedCase.Related_Setup__c != null) {
                Setup__c relatedSetup = [select Id, Cost_Center__c, Name, Service_Availability__c from Setup__c where Id =: updatedCase.Related_Setup__c];
                if (relatedSetup != null) {                 
                    // Check if there are any previous SAs for corresponding Setup            
                    List<Service_Availability__c> oldSAs = [select Name, Status__c, Status_explanation__c, Duration__c, Start_DateTime__c from Service_Availability__c
                                                            where Setup__c =: updatedCase.Related_Setup__c                                                    
                                                            order by Name desc];
                    
                    if (oldSAs.Size() == 0) {
                        RecordType recordTypeS = [select Id, Name from RecordType where SObjectType = 'Service_Availability__c' and Name = 'Setup' ];                
                        saHelper.InsertServiceAvailabilityObjectToSetup(updatedCase, System.Now(), recordTypeS, updatedCase.Fault_Classification__c);
                    } else {
                        // Process the SA as if the Fault Classification was changed (manually)
                       saHelper.ProcessCaseServiceAvailabilityUpdate(updatedCase, oldCase, true);
                    }                                                    
                } else {
                    System.Debug('CaseServiceAvailabilityTrigger: NO Setup defined for Case: ' + updatedCase.Id);
                }
            }                                                        
        }
    }
}

 

           

kirkevonphillykirkevonphilly

You're currently working with an after insert only trigger.  The oldMap Trigger Context isn't available for inserts, only for update and deletes, so you'll want to update it to be after insert, after update.  For bulkification, the first part of your trigger should check out all of the inserted/updated records for any that meet your criteria and add them to a list (casesForSetup) that will be looped through to create your related records.

 

list<Case> casesForSetup = new list<Case>();
for (Case c : trigger.new){
	if (trigger.isInsert){
		if (c.Related_Setup__c != null)
			casesForSetup.add(c);	    
	}
	else{
		if (trigger.oldMap.get(c.id).Related_Setup__c != null && c.Related_Setup__c != null)
			casesForSetup.add(c);
	}
}

if (!casesForSetup.isEmpty()){
	... bulkified busy work here
}