1

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger InsertContact caused an unexpected exception, contact your administrator: InsertContact: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00190000019XMfWAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InsertContact: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.InsertContact: line 43, column 1: []: Trigger.InsertContact: line 43, column 1

if ( Trigger.isUpdate ) {
    //List<Account> acnt = new List<Account>();
    Account acc;
    for ( Account oldval : Trigger.old ) {
        if ( staticFun.runME ) {
            System.debug(+oldval.AccountNumber);
            System.debug(+oldval.Id);

            // Account ac = new Account(AccountNumberNew__c=Integer.ValueOf(oldval.AccountNumber));
            //acnt.add(ac);

            acc = [select AccountNumberNew__c from Account where Id =: oldval.Id];
            system.debug('account INFO:'+acc);
            acc.AccountNumberNew__c = Integer.ValueOf(oldval.AccountNumber);
            System.debug(acc.AccountNumberNew__c);

            //update acc;
            staticFun.runME = false;
        }
    }
    update acc;
    System.debug('AccountNumberNew Inserted successfully!');
}

I want to update standard AccountNumber field, while updating, old value has to be assigned to custom field AccountNumberNew__c. Kindly help me to resolve this.

Here I have used if ( staticFun.runME ) for prevent recursive trigger.

  • 1
    which line is line 43? – Novarg Dec 2 '14 at 7:04
  • Is this still a problem? If so, what did you try yourself to fix it? If you already fixed it, please accept an answer or write one yourself and accept that. – Willem Mulder Oct 8 '15 at 9:21
1

this is most probably happening when you try to execute this line:

update acc;

Most probably acc variable is still null at this point, which can happen because of 2 reasons:

  1. Your Trigger.old is empty, so you never get inside the loop
  2. staticFun.runMe is always false, so you never get inside this if-statement

Also, this line

acc = [select AccountNumberNew__c from Account where Id =: oldval.Id];

is a query inside the for-loop. The best practice is to move all queries outside the loops. Read about governor limits in Salesforce.

|improve this answer|||||
  • this will fail with null pointer exception as you are not instantiating the object. Since your are going to update the object with new value you need to instantiate the object. Good practice is to take the values in a List and then do a null check before doing a update. – nevrekar_amey Apr 21 '15 at 22:30

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.