-2

I am getting a final exception on below trigger while creating data . Also I need to put a check for Ispersonaccount. Only if the concerned account is a personaccount , the trigger would work.Can someone help me here?

Review all error messages below to correct your data. Apex trigger ACCOUNT_AFTER_INSUPD_NAME caused an unexpected exception, contact your administrator: ACCOUNT_AFTER_INSUPD_NAME: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ACCOUNT_AFTER_INSUPD_NAME: line 16, column 1

trigger ACCOUNT_AFTER_INSUPD_NAME on Account (before insert , before update) {

 /*List<Account> acc = new List<Account>();
acc = [SELECT Id,FirstName,LastName,Middle_vod__c FROM Account WHERE Id IN:Trigger.oldMap.keySet() LIMIT 1];*/
for(Account a: trigger.new{
String f = a.FirstName;
List<String> FName= f.split(' ');
 for(Integer i=0;i<FName.size();i++){
   FName[i] = (FName[i].substring(0,1)).toUppercase() +(FName[i].substring(1,FName[i].length()));
   a.FirstName=a.FirstName+' '+FName[i];
 }
 String m = a.Middle_vod__c;
if(m != null){
  List<String> MName= m.split(' ');
  for(Integer i=0;i<MName.size();i++){
     MName[i] = (MName[i].substring(0,1)).toUppercase() +(MName[i].substring(1,MName[i].length()));
     a.Middle_vod__c=a.Middle_vod__c+' '+MName[i];
  }
}
String l = a.LastName;
List<String> LName= l.split(' ');
for(Integer i=0;i<LName.size();i++) {
  LName[i] = (LName[i].substring(0,1)).toUppercase() +(LName[i].substring(1,LName[i].length()));
  a.LastName=a.LastName+' '+LName[i];
}
  a.Name=a.FirstName+' ' +a.Middle_vod__c+' '+a.lastname;
  update a;
 }
}
6

Please do not update the same sObjectType in the After Triggers. You need to use Before Triggers in this case. This would resolve the System.FinalException: Record is read-only error message.

For person account , please use "isPersonAccount" field of Account, if it is enabled for your Org.

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.