function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TinkuTinku 

Trigger Error

This is the trigger i have written on Case object which updates a field in Account object. When i am closing a case, the following error shows up.

 

 

 for(Case ca:Trigger.New)
    {
      Id1=ca.AccountId;    
      Account acct=[Select a.id, a.Name, a.Last_Contacted_date__c, a.Contacted__c from Account a where a.Id=:ID1];
        if(ca.Subject=='New Member Outreach' && ca.Status=='Closed' && ca.AccountId==acct.Id)
        {
            acct.Contacted__c=True;
            acct.Last_Contacted_date__c=ca.ClosedDate;

        } 
                 database.update(acct);   --> Error: at this line       
    }
         
}

Error is: updateAccount_Premium: execution of AfterUpdate
caused by: System.DmlException: Update failed. First exception on row 0
with id 001P000000GIss5IAD; first error:
INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on
insert/update call: Name: [Name]: Trigger.updateAccount_Premium:
Best Answer chosen by Admin (Salesforce Developers) 
kyle.tkyle.t

The error appears to be saying there is an issue with the "Name" column.  easiest thing might be to just get rid of that in the query since it isn't being used.

 

**Update, probably also want to make sure you have somthing in the account list

 

trigger updateAccount on Case (after insert, after update) {
    List<Account> acctList = new List<Account>();
    Set<Id> acctIds = new Set<Id>();
    for(Case c : trigger.new){
        if(c.AccountId<>null)
            acctIds.add(c.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>();
    //removed Name from the query below
for (Account a : [select Id, Last_Contacted_Date__c, Contacted__c from Account where id IN :acctIds]){ acctMap.put(a.id,a); } for(Case c : trigger.new){ if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null){ Account a = acctMap.get(c.AccountId); a.Contacted__c = True; a.Last_Contacted_Date__c = c.ClosedDate; acctList.add(a); } }
if(!acctList.isEmpty()){
 update acctList;
}
}

 

 

All Answers

kyle.tkyle.t

Off the bat there are a few issues here.  First, you are performing a query in a for loop so you risk hitting govenor limits pretty quickly.  Second, you need to access the account field through the relationship to the case.  Something like this should get you close.

 

 

List<Account> acctList = new List<Account>();
for(Case c : trigger.new){
	if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null){
		c.Account__r.Contacted__c = True;
		c.Account__r.Last_Contacted_Date__c = c.CloseDate;
		acctList.add(c.Account);
	}
}
update acctList;

 

 

TinkuTinku

Thanks Kyle.

 

When i tried this code, it gives me Invalid foreign key relationship error.

kyle.tkyle.t

Try this... you may want to add some conditions to check the date to make sure you aren't updating to a prior date.

 

 

trigger updateAccount on Case (after insert, after update) {
    List<Account> acctList = new List<Account>();
    Set<Id> acctIds = new Set<Id>();
    for(Case c : trigger.new){
        if(c.AccountId<>null)
            acctIds.add(c.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>();
    for (Account a : [select Id, Name, Last_Contacted_Date__c, Contacted__c from Account where id IN :acctIds]){
        acctMap.put(a.id,a);
    }
        
    for(Case c : trigger.new){
        if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null){
            Account a = acctMap.get(c.AccountId);
            a.Contacted__c = True;
            a.Last_Contacted_Date__c = c.ClosedDate;
            acctList.add(a);
        }
    }
    update acctList;
}

 

 

TinkuTinku

Kyle,

 

The trigger is saving fine now.

 

But when i try to close the case, it gives me this error: at the line update acclist;

 

Apex trigger updateAccount caused an unexpected exception, contact your administrator: updateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001P000000GJAVGIA5; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]: Trigger.updatePreimumAccount:

TinkuTinku

Kyle,

I tried your previous code. Contacted__c  and Last_Contacted__c are both custom fields on Account. 

 

trigger updateAccount_Premium on Case (after insert,after update) 
{
    
    List<Account> acctList = new List<Account>();
    for(Case c : trigger.new)
    {
    if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null)
    {
     
       c.Account.Contacted__c = True; --> Error line 11
        c.Account.Last_Contacted_Date__c = c.ClosedDate; -->error line 12
        acctList.add(c.Account);
    }
}
update acctList;

But now it gives Null Point Exception error for line 11 and 12.

 

This is the error i get when i try to close the case: updateAccount: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateAccount:

 

kyle.tkyle.t

so this code isn't working for you?  It is currenlty compile and working for me in a dev environment.

 

 

trigger updateAccount on Case (after insert, after update) {
    List<Account> acctList = new List<Account>();
    Set<Id> acctIds = new Set<Id>();
    for(Case c : trigger.new){
        if(c.AccountId<>null)
            acctIds.add(c.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>();
    for (Account a : [select Id, Name, Last_Contacted_Date__c, Contacted__c from Account where id IN :acctIds]){
        acctMap.put(a.id,a);
    }
        
    for(Case c : trigger.new){
        if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null){
            Account a = acctMap.get(c.AccountId);
            a.Contacted__c = True;
            a.Last_Contacted_Date__c = c.ClosedDate;
            acctList.add(a);
        }
    }
    update acctList;
}

 

 

TinkuTinku

Kyle,

 

This code is saving fine in my dev organization. The trigger should fire when i close a case. So when i am closing a case, it gives me this error:

 

Apex trigger updateAccount caused an unexpected exception, contact your administrator: updateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001P000000GJAUGIA5; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]: Trigger.updateAccount: line 22, column 5

 

The error is at line: 

    update acctList;
kyle.tkyle.t

The error appears to be saying there is an issue with the "Name" column.  easiest thing might be to just get rid of that in the query since it isn't being used.

 

**Update, probably also want to make sure you have somthing in the account list

 

trigger updateAccount on Case (after insert, after update) {
    List<Account> acctList = new List<Account>();
    Set<Id> acctIds = new Set<Id>();
    for(Case c : trigger.new){
        if(c.AccountId<>null)
            acctIds.add(c.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>();
    //removed Name from the query below
for (Account a : [select Id, Last_Contacted_Date__c, Contacted__c from Account where id IN :acctIds]){ acctMap.put(a.id,a); } for(Case c : trigger.new){ if(c.Subject=='New Member Outreach' && c.Status=='Closed' && c.AccountId <> null){ Account a = acctMap.get(c.AccountId); a.Contacted__c = True; a.Last_Contacted_Date__c = c.ClosedDate; acctList.add(a); } }
if(!acctList.isEmpty()){
 update acctList;
}
}

 

 

This was selected as the best answer
TinkuTinku

Its working.

 

Thank you. Thanks a lot Kyle. You saved my day!! :)

kyle.tkyle.t

Glad to help... also, I made an additon to my last post, note the update.

TinkuTinku

Ok will do. thanks again.

TinkuTinku

Kyle,

 

I have one more Similar trigger: I am updating the Account Owner based on some criterias in Opportunity and Account's Billing State.  When i try to change the criterias in opportunity to fire this trigger, it gives me this error at line: update aactList;

 

Apex trigger AccountAssignment caused an unexpected exception, contact your administrator: AccountAssignment: execution of AfterUpdate caused by: System.ListException: DML statment found null SObject at position 0: Trigger.AccountAssignment

 

I know there is something wrong with the trigger i have written. Suggestions please.

 

 

trigger AccountAssignment on Opportunity (after insert,after update)
{
 

  List<Account> acctlist = new List<Account>();
  Set<Id> acctIds= new Set<Id>();
  
  for(Opportunity opp: trigger.new)
  {
   if(opp.AccountId<>null)

   acctIds.add(opp.AccountId);

  }
  
  Map<Id, Account> acctMap = new Map<Id, Account>();
  for(Account acct : [Select BillingState, OwnerId, Accounts_To_Be_Assigned__c from Account where id IN :acctIds])
  {
    acctMap.put(acct.Id, acct);
  }
        
  
      for(Opportunity opp : trigger.New)
       {
                            Account acct = acctMap.get(opp.AccountId);
             if(((opp.StageName=='Contract')&&(opp.Number_of_Recruiters__c=='10+'))||(acct.BillingState == ''))

             {

                 acct.ownerID =[select ID from User where name = 'Bryan McShane'].id;
                 acct.Accounts_to_be_assigned__c = True;
                 //This line is giving error. changed it. updated line in Blue.
                 acctList.add(opp.Account);  
acctList.add(acct);
             }
 
             
             if((opp.StageName=='Contract')&&(opp.Number_of_Recruiters__c=='5-9') && (acct.BillingState == 'ME'||acct.BillingState == 'NH'

             ||acct.BillingState == 'VT'||acct.BillingState == 'NY'||acct.BillingState == 'PA'||acct.BillingState == 'DC'||acct.BillingState == 'MD'
             
             ||acct.BillingState == 'DE'||acct.BillingState == 'NJ'||acct.BillingState == 'CT'||acct.BillingState == 'RI'||acct.BillingState == 'MA'
             
             ||acct.BillingState == 'ND'||acct.BillingState == 'SD'||acct.BillingState == 'NE'||acct.BillingState == 'KS'||acct.BillingState == 'IK'
             
             ||acct.BillingState == 'MN'||acct.BillingState == 'IA'||acct.BillingState == 'MO'||acct.BillingState == 'AR'||acct.BillingState == 'WI'
             
             ||acct.BillingState == 'IL'||acct.BillingState == 'MI'||acct.BillingState == 'IN'||acct.BillingState == 'OH'))

             {

                 acct.ownerID =[select ID from User where name = 'Holly Esposito'].id;
                  //This line is giving error. Changed it. Updated line in blue.
acctList.add(opp.Account);
acctList.add(acct);
                
             }
}
                     update acctlist;
}
kyle.tkyle.t

I believe that you are passing an empty list to update so you need to check first that the list isn't empty. I also pulled your SOQL queries out of the for loop because you will hit govenor limits if you are doing a bulk upload.  I moved the OR statements around for my own readability, but I acutally suggest you implement that piece via a formula field. that you can reference in the trigger.  Create a formula field on the account using the CASE formula and return some value you can reference (I threw in North and South).

 

 

IF(BillingState == 'ME'||
BillingState == 'NH'||
BillingState == 'VT'||
BillingState == 'NY'||
BillingState == 'PA'||
BillingState == 'DC'||
BillingState == 'MD'||
BillingState == 'DE'||
BillingState == 'NJ'||
BillingState == 'CT'||
BillingState == 'RI'||
BillingState == 'MA'||
BillingState == 'ND'||
BillingState == 'SD'||
BillingState == 'NE'||
BillingState == 'KS'||
BillingState == 'IK'||
BillingState == 'MN'||
BillingState == 'IA'||
BillingState == 'MO'||
BillingState == 'AR'||
BillingState == 'WI'||
BillingState == 'IL'||
BillingState == 'MI'||
BillingState == 'IN'||
BillingState == 'OH',"North","South")

 

 

without making that change, here is the code I have.

 

trigger AccountAssignment on Opportunity (after insert,after update)
{
	List<Account> acctlist = new List<Account>();
	Set<Id> acctIds= new Set<Id>();
	  
	for(Opportunity opp: trigger.new)
	{
		if(opp.AccountId<>null)
			acctIds.add(opp.AccountId);
	}
	  
	Map<Id, Account> acctMap = new Map<Id, Account>();
	for(Account acct : [Select BillingState, OwnerId, Accounts_To_Be_Assigned__c from Account where id IN :acctIds])
	{
		acctMap.put(acct.Id, acct);
	}
        
  
	Id bryanId =[select ID from User where name = 'Bryan McShane'].id;
	ID hollyId = [select ID from User where name = 'Holly Esposito'].id;
	
	for(Opportunity opp : trigger.New)
	{
		Account acct = acctMap.get(opp.AccountId);
		if(((opp.StageName=='Contract')&&(opp.Number_of_Recruiters__c=='10+'))||(acct.BillingState == '')){
			acct.OwnerId = bryanId;
			acct.Accounts_to_be_assigned__c = True;
			acctList.add(acct);   
		 } 

		if((opp.StageName=='Contract')&&(opp.Number_of_Recruiters__c=='5-9') && 
		    (acct.BillingState == 'ME'||
			 acct.BillingState == 'NH'||
			 acct.BillingState == 'VT'||
			 acct.BillingState == 'NY'||
			 acct.BillingState == 'PA'||
			 acct.BillingState == 'DC'||
			 acct.BillingState == 'MD'||
			 acct.BillingState == 'DE'||
			 acct.BillingState == 'NJ'||
			 acct.BillingState == 'CT'||
			 acct.BillingState == 'RI'||
			 acct.BillingState == 'MA'||
			 acct.BillingState == 'ND'||
			 acct.BillingState == 'SD'||
			 acct.BillingState == 'NE'||
			 acct.BillingState == 'KS'||
			 acct.BillingState == 'IK'||
			 acct.BillingState == 'MN'||
			 acct.BillingState == 'IA'||
			 acct.BillingState == 'MO'||
			 acct.BillingState == 'AR'||
			 acct.BillingState == 'WI'||
			 acct.BillingState == 'IL'||
			 acct.BillingState == 'MI'||
			 acct.BillingState == 'IN'||
			 acct.BillingState == 'OH'))
		{

			 acct.ownerID = hollyId;
			 acctList.add(acct);
			 
		 } 
	}
	if(!acctlist.isEmpty()){
		update acctlist;
	}
}

 

If you were to implement the formula field you could change that big if statement to something like

if(opp.StageName=='Contract' &&
   opp.Number_of_Recruiters__c=='5-9' &&
   acct.region_formula_field__c == 'North'){
     acct.ownerID = hollyId;
     acctList.add(acct)
}

 I hope that helps

 

TinkuTinku

Kyle,

 

I corrected the error 2 of my triggers are working great because of your help.

 

Thanks again.

TinkuTinku

I have a DOUBT Kyle.

 

SOQL statements inside a For loop will hit Governer Limits. But in your solution you have written a SOQL statement as the condition of FOR loop. Wont that hit governer limits?

 

For Ex:

will this for loop hit governer limit?

 

 

for(Account acct : [Select BillingState, OwnerId, Accounts_To_Be_Assigned__c from Account where id IN :acctIds])
  {
    acctMap.put(acct.Id, acct);
  }
kyle.tkyle.t

Excellent question.  This will not hit a govenor limit.  have a look at this documentation to understand why.

 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm  Search for "SOQL For Loops"