I am currently developing the code that will update al the children accounts related to this parent account if this parent account gets 's updated. gets updated.
I have two questions:
- What could be a problem based on the error message a system puts out after account update attempt. Here is the error message:
Error:Apex trigger PTFT caused an unexpected exception, contact your administrator: PTFT: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()
- is there any way I can create a variable for the records in account hierarchy rather than define each hierarchy like I have in my code.
I am quite new in apex coding and any help would be appreciated. Please see my code below.
trigger PTFT on Account (before update) {
if (trigger.isBefore){
if (trigger.isUpdate || trigger.isInsert){
list<Account> listAcc = new list <Account>();
set<id> idAcc = new set<id>();
for(Account acc: trigger.new){
Account oldAcc = trigger.oldMap.get(acc.id);
listAcc.add(acc);
idAcc.add(acc.id);
list <Account> acc2bUpdated = [select id, Name, CR_Parent_Fax__c, upaLevels__c,
ParentId,
Parent.Name,
Parent.CR_Parent_Fax__c,
Parent.ParentId,
Parent.Parent.Name,
Parent.Parent.CR_Parent_Fax__c,
Parent.Parent.ParentId,
Parent.Parent.Parent.Name,
Parent.Parent.Parent.CR_Parent_Fax__c,
Parent.Parent.Parent.ParentId,
Parent.Parent.Parent.Parent.Name,
Parent.Parent.Parent.Parent.CR_Parent_Fax__c,
Parent.Parent.Parent.Parent.ParentId,
Parent.Parent.Parent.Parent.Parent.Name,
Parent.Parent.Parent.Parent.Parent.CR_Parent_Fax__c
from
Account
where
ParentId != null
AND id IN: listAcc];
if (!oldAcc.CR_Parent_Fax__c && acc.CR_Parent_Fax__c){
if(acc2bUpdated.size() > 0) {
for(Account b: acc2bUpdated){
if( b.upaLevels__c == 2){
b.Parent.parent.CR_Parent_Fax__c = true;
b.Parent.CR_Parent_Fax__c = true;
}
else{
if(b.upaLevels__c == 3){
b.Parent.Parent.Parent.CR_Parent_Fax__c =true;
b.Parent.Parent.CR_Parent_Fax__c =true;
b.Parent.CR_Parent_Fax__c =true;
}
else{
if( b.upaLevels__c == 4) {
b.Parent.Parent.Parent.Parent.CR_Parent_Fax__c = true;
b.Parent.Parent.Parent.CR_Parent_Fax__c = true;
b.Parent.Parent.CR_Parent_Fax__c = true;
b.Parent.CR_Parent_Fax__c = true;
}
}
}
}
}
}
}
}
}
system.debug(logginglevel.WARN, '^^^ event last modified: '+trigger.new[0].LastModifiedById);
system.debug(logginglevel.WARN, '^^^ userinfo: '+userinfo.getUserId());
{}
code format button) to make your question readable. I fixed your formatting, but your next edit overwrote the changes.