1

I need to be able to set the account on a case when the case is created. I have the following trigger. The debug statements show as expected, but the account is not getting updated on the case. What am I doing wrong?

trigger SetAccount on Case (before insert, before update) {
    Account acc = [select id from account where account.name='ABC'];
     for (Case caseObj:Trigger.new) {
        system.debug('caseObj.account before: '+caseObj.account);
        if(caseObj.account == null){
            caseObj.account = acc;
            system.debug('caseObj.account after: '+caseObj.account);
        }          
    } 
}  

1 Answer 1

3

Try doing the following. Notice I am using Case.AccountId instead of Case.Account. Also, need to make sure that the initial query is returning something, or else you will get an error further along.

trigger SetAccount on Case (before insert, before update) {
    Account acc = [select id from account where account.name='ABC'];
    if(acc != null)
    {
     for (Case caseObj:Trigger.new) {
        system.debug('caseObj.account before: '+caseObj.accountId);
        if(caseObj.accountId == null){
            caseObj.accountId = acc.Id;
            system.debug('caseObj.account after: '+caseObj.accountId);
        }          
     }
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .