+ Start a Discussion
tyler_jenningstyler_jennings 

Problem updating affected record from Trigger

This trigger does _not_ save 'Hello!' to the Area__c field on the case, I can't figure out why.  There are no errors.  Here's the log output:

*** Beginning copyFieldsOnCreate on Case trigger event bulk AfterInsert for 500T0000000n9Tf

20070731134615.308:Trigger.copyFieldsOnCreate: line 1, column 1: TriggerBody: copyFieldsOnCreate
20070731134615.308:Trigger.copyFieldsOnCreate: line 2, column 3: SelectLoop:Static: name: Trigger.new, type: LIST:SOBJECT:Case
20070731134615.308:Trigger.copyFieldsOnCreate: line 2, column 31: Block with 3 statements
20070731134615.308:Trigger.copyFieldsOnCreate: line 6, column 5: DeclareVar: Local: name: c, type: SOBJECT:Case with initial value(s): InlineQuery: [select c.id, c.Area__c from Case c where c.Id = :ca.Id]
20070731134615.308:Trigger.copyFieldsOnCreate: line 6, column 14: SOQL query with 1 row finished in 13 ms
20070731134615.308:Trigger.copyFieldsOnCreate: line 6, column 5: initial value: Case:{Id=500T0000000n9TfIAI}
20070731134615.308:Trigger.copyFieldsOnCreate: line 7, column 5: SOBJECT:Case.Area__c assigned Literal: Hello!
20070731134615.308:Trigger.copyFieldsOnCreate: line 8, column 5: Update: SOBJECT:Case
20070731134615.308:Trigger.copyFieldsOnCreate: line 8, column 5: DML Operation executed in 352 ms

Cumulative resource usage:
Number of SOQL queries: 1 out of 20
Number of query rows: 1 out of 1000
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of transaction control statements: 0 out of 0
Number of script statements: 3 out of 10200
Maximum heap size: 0 out of 100000


*** Ending copyFieldsOnCreate on Case trigger event bulk AfterInsert for 500T0000000n9Tf
Can anyone point out my flaw?

Thanks!

tyler_jenningstyler_jennings
I tried to use the code block, ate my source.  Here it is with no special formatting:

trigger copyFieldsOnCreate on Case (after insert) {
for (Case ca : Trigger.new) {
//Account a = [select a.Area__c from Account a where a.id = :ca.accountId];
//Case c = new Case(Id = ca.Id);
//c.Area__c = a.Area__c;
Case c = [select c.id, c.Area__c from Case c where c.Id = :ca.Id];
c.Area__c = 'Hello!';
update c;
}
}

I discovered that if I hardcode the value of the case id in the SOQL it works as expected.
Still no luck getting the real version running.

Thanks again!

SuperfellSuperfell
this should be all you need.

trigger copyFieldsOnCreate on Case (before insert) {
for (Case ca : Trigger.new) {
ca.Area__c = 'Hello!';
}
}
tyler_jenningstyler_jennings
Unfortunately, it is a little more complicated than that.  I'm trying to copy an account field onto the case.  What I *think*
I discovered is that reference IDs are not yet populated on a before insert event.  If you look at the three commented
lines in the tigger you'll see what I'm really trying to do.

Thanks for the help!
Ron HessRon Hess
This works for me (sandbox:10.0 api),

grab the account with your field
the ca variable already has the new (yet to be created) case
so just assign the account value to the case member.

and note, no need for update() this occurs automaticaly
note: this code does a query for each case that you insert, so probably needs to be optimized to get all
the accounts that it needs in one query not N queries.

Code:
trigger copyFieldsOnCreate on Case bulk(before insert) {
for (Case ca : Trigger.new) {
Account a = [select a.Description from Account a where a.id = :ca.accountId];
ca.Description = a.Description;
}
}

tyler_jenningstyler_jennings
Thanks for the response, this was very helpful. 

It does indeed work for me when I create a new case or update an existing one.  However,
This trigger throws an exception when inserting a new case from the customer portal.  I apologize for
not mentioning our use of the customer portal earlier, I thought it was irrelevant.  From what I can gather
from the exceptions / logs the Account is not defined when the before insert event fires the trigger. 

So, that's what lead me toward the after insert approach.  I *think* the Account should be defined at that point. 
So I think we're back to square one:  How do I get an after insert trigger working that updates the trigger-firing
object?

Apex trigger copyFieldsOnCreate_test caused an unexpected exception, contact your
administrator: copyFieldsOnCreate_test: execution of BeforeInsert caused by:
System.QueryException: List has no rows for assignment to SObject:
Trigger.copyFieldsOnCreate_test: line 3, column 17
Ron HessRon Hess
the case is read-only in the "after insert" trigger, since it's already in the database.

i'm not sure why the account id is empty when entering the trigger, probably because the contact needs to be matched up, and this may be happening after the before trigger.

can you check for that ID in the after triger

something like this:

system.assert(ca.accountId != null) ;


Also, find out if the ContactId is set or empty in your trigger?

it's possible is the portal is creating the record with this empty, then calling 'update' to write in the account ID


TheBombTheBomb
Did anyone work out what the issue was with APEX code being called from the self-service portal?

I've just hit something very similar.  The APEX code works when creating a Case from the salesforce interface, but it fials to select objects when called from the self-service portal.

My code is (for debugging my issue is):

trigger Azuro_auto_assign_case on Case (before insert) {
    For (Case c : Trigger.new) {
        //based on the AccountID, assign to that Accounts creator
        c.addError('Everything is ok');
        system.assert(c.AccountId != null);
        ID account_creator_id=[SELECT CreatedById FROM account WHERE Id= :c.AccountId].CreatedById;
        If(account_creator_id!= null) {
                c.OwnerId=account_creator_id;
        }  else {
                c.addError('Did not find a account_creator_id');
        }
        //c.addError('account_id='+c.AccountId);
    }
}


When creating from in salesforce I get the following error (effectively a pass!):
Error: Invalid Data.
Review all error messages below to correct your data.
Everything is ok

When creating a case from the self-service api I get:
Error:
Apex trigger Azuro_auto_assign_case caused an unexpected exception, contact your administrator: Azuro_auto_assign_case: execution of BeforeInsert caused by: System.Exception: Assertion Failed: Trigger.Azuro_auto_assign_case: line 5, column 9





Message Edited by TheBomb on 09-07-2007 07:17 AM

TheBombTheBomb
Those quick witted people out there will notice that this was erroring because the accountid was null.  However, the contact id isn't null, but I can't query for the contact!!!

The following code:

trigger Azuro_auto_assign_case on Case (before insert) {
    For (Case c : Trigger.new) {
        //based on the Contact, assign to the contact Account's creator
        c.addError('Everything is ok');
        system.assert(c.ContactId != null);
        ID account_id=[SELECT AccountId FROM Contact WHERE Id=:c.ContactId].AccountId;
        system.assert(account_id != null);
        ID account_creator_id=[SELECT CreatedById FROM account WHERE Id=:account_id].CreatedById;
        If(account_creator_id!= null) {
                c.OwnerId=account_creator_id;
        }  else {
                c.addError('Did not find a account_creator_id');
        }
    }
}

Gives me an ok from in Salesforce:
Error: Invalid Data.
Review all error messages below to correct your data.
Everything is ok

But an error of having no rows from the self-service portal:
Error:
Apex trigger Azuro_auto_assign_case caused an unexpected exception, contact your administrator: Azuro_auto_assign_case: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.Azuro_auto_assign_case: line 6, column 23