0

first post so apologies if there's some issues.

I'm looking to build an Apex trigger that looks up an Account name that contains "DUNNES" in the title (among other rules), and execute an email class. We were originally using a Process for this, but there's some extra bits we have to add in on the email execution that isn't in Process Builder, so looking into Apex.

However, I get an error running this Apex trigger:

Apex trigger EmailTesting caused an unexpected exception, contact your administrator: EmailTesting: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.EmailTesting: line 4, column 1

The code we have is:

trigger EmailTesting on Service_Call__c (after insert, after update) {

    for (Service_Call__c sc1 : Trigger.new) {
        if(trigger.isUpdate && sc1.Customer__r.Name.contains('DUNNES') && (sc1.Equipment_Category__c != 'Ice Machine' || sc1.Equipment_Category__c != 'Hot Counter')){
            EmailClass.sendSingleMail();    
        }
    }
}

Any thoughts on this error?

Thanks

0

Field values of parent object isn't populated in trigger, They all are null. In order to get value of sc1.Customer__r.Name field, you need to query it first. What do you have now, that sc1.Customer__r.Name == null and you try to invoke contains method from null. That is an error. In addition before referencing parent object, it is needed to check if lookup isn't empty.

trigger EmailTesting on Service_Call__c (after insert, after update) {

    Set<Id> customersIds  = new Set<Id>();
    for(Service_Call__c serviceCall :Trigger.new){
        customersIds.add(serviceCall.Customer__c);
    }
    customersIds.remove(null);

    Map<Id, Account> acctByAcctId = new Map<Id, Account>(]
        select Id, Name
        from Account
        where Id in:customersIds
        ]);

    for (Service_Call__c sc1 : Trigger.new) {
        if(trigger.isUpdate && sc1.Customer__c != null acctByAcctId.get(sc1.Customer__c).contains('DUNNES') && (sc1.Equipment_Category__c != 'Ice Machine' || sc1.Equipment_Category__c != 'Hot Counter')){
            EmailClass.sendSingleMail();    
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.