You need to sign in to do that
Don't have an account?

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:
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
All Answers
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.
Thanks Kyle.
When i tried this code, it gives me Invalid foreign key relationship error.
Try this... you may want to add some conditions to check the date to make sure you aren't updating to a prior date.
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:
Kyle,
I tried your previous code. Contacted__c and Last_Contacted__c are both custom fields on Account.
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:
so this code isn't working for you? It is currenlty compile and working for me in a dev environment.
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:
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
Its working.
Thank you. Thanks a lot Kyle. You saved my day!! :)
Glad to help... also, I made an additon to my last post, note the update.
Ok will do. thanks again.
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.
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).
without making that change, here is the code I have.
If you were to implement the formula field you could change that big if statement to something like
I hope that helps
Kyle,
I corrected the error 2 of my triggers are working great because of your help.
Thanks again.
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?
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"