0

I am trying to update a account using the below method called from an after update, after insert trigger. However I am getting an error - "Attempt to de-reference a null object" while using Account acc = null; in the below code..AND if I use Account acc = new Account() , I am getting the error - missing_argument id not specified in an update call in salesforce.Kindly assist.

   Trigger:              
   trigger testTrigg on CustObj(after insert,after update) 
   {
       for (CustObj cr1 : Trigger.new)
       {
           myClass.updateAccount(cr1);
       }
   }
 Class:
  public static myClass{
    public static void updateAccount(CustObj cr)
    {   
       List<Account> acclist = new List<Account>();
       for (Account ac : soql query)
      {               
         acclist.add(ac);
      }    
      Account acc = null;
      if (acclist.size() > 0)
      {
         acc = acclist[0];
         acc.Name = cr.Account_Name__c; 
         acc.Other_title_information__c = cr.JobTitle__c ;   
      }
     Database.SaveResult sr= Database.update(acc);
    }
  }
  • 1
    can you please post complete code? what object is cr? – C0DEPirate Oct 28 '15 at 13:34
  • @NickSFDC : added the code – CRM Guy Oct 28 '15 at 13:46
  • what is the relationship between CustObj and Account and what are you trying to achieve? from your logic you will be able to update only first account from the acclist – C0DEPirate Oct 28 '15 at 14:20
  • @NickSFDC - CustObj and Account are in lookup relationship..yes...I dont need to bulkify as there will not be any dataload on CustObj ..users will submit records from UI only. – CRM Guy Oct 28 '15 at 16:21
  • which is parent account or custobj? – C0DEPirate Oct 28 '15 at 20:04
1

This change should stop the exception because it ensures that the update is only done when there is an Account value to update:

if (acclist.size() > 0)
{
    Account acc = acclist[0];
    acc.Name = cr.Account_Name__c; 
    acc.Other_title_information__c = cr.JobTitle__c ;   
    Database.SaveResult sr = Database.update(acc);
    // Do something with sr otherwise why assign it?
}

But your design is doing a query and an update per CustObj and so will break when large bulk updates of CustObj happen. Google about and apply trigger bulkification techniques.

|improve this answer|||||

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.