You need to sign in to do that
Don't have an account?
jmasl7x
Trigger works but is it efficient?
I've created the following Apex Trigger and tested it in our sandbox environment. SFA_Order__c is a custom object we use to capture Orders and SFA_Delivery_Location__c is a non-mandatory field which is defined as lookup(Account).
My question regards the retrieval of the Record Type name in order to make the comparison
It seems to me inefficient that the trigger should be querying the RecordType object (table?) every time it is executed. But is there a better way to do this? I thought perhaps I could specify
in my comparison, but this gives me a error at runtime:
System.NullPointerException
Any help appreciated.
Regards
John
Code:
trigger SFA_AU_setDeliveryAddress on SFA_Order__c (before update, before insert) { SFA_Order__c[] ords = Trigger.New; for (SFA_Order__c ord: ords) { String rt = ord.RecordTypeId; RecordType[] rts = [select Name from RecordType where id = :rt]; if (rts.size() > 0 && rts[0].Name.startsWith('SFA AU')) { String acid; if (ord.SFA_Delivery_Location__c != null) acid = ord.SFA_Delivery_Location__c; else acid = ord.SFA_Account__c; Account[] ac = [Select BillingStreet,BillingPostalCode,BillingCity from Account where Id = :acid]; if (ac.size() > 0) { ord.SFA_Account_Street__c = ac[0].BillingStreet; ord.SFA_Account_ZIP_Code__c = ac[0].BillingPostalCode; ord.SFA_Account_City__c = ac[0].BillingCity; } } } }
My question regards the retrieval of the Record Type name in order to make the comparison
rts[0].Name.startsWith('SFA AU')
ord.RecordType.Name
in my comparison, but this gives me a error at runtime:
System.NullPointerException
Any help appreciated.
Regards
John
thanks for the information.
Is there any way to predict how many records will be loaded in bulk per invocation of the trigger?
I assume if the trigger runs with 20 (or more) records in the Trigger.New array then it would still fail because I'd be running 20 queries to Account and 1 to RecordType (?)
Should I be building a Map of Accounts at the start of the process in a similar way that you showed me for RecordTypes?
Regards
John