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

Trigger / Test Class Help - updating a field on an account from a child task record
Hi everyone. I have a trigger that currently works fine through the user interface. However I need to bulk update 85,000 task records and the trigger is causing failures.
Intended trigger purpose: Update the Last_SW_Activity__c field (date time field) on the account object with the last modified date of the record when a task record meets the following criteria:
- The Subject Line starts with "sw".
- The task is marked as completed.
trigger LastSWDate on Task (after insert, after update) { //To do - If the subject of a completed task contains "SW", put the date of the completed task in the //the "Last_SW_Activity__c" field on the account object //Create a set of related account ID's Set <ID> acctIDs = new Set <ID> (); //Create a list to hold the Updated Accounts List<Account> updAccts = new List<Account>(); //For every task, add it's related to account ID to the set for (Task t: Trigger.new){ acctIDs.add(t.accountID); //Create a map to match the task related to ID's with their corresponding account ID's Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Last_SW_Activity__c from Account where ID in :acctIDs]); //Create the account object Account acctRec = acctMap.get(t.accountID); //If the account ID isn't null, the subject line starts with "sw", and the task has been marked as completed If ((t.accountID != null) &&(t.subject.indexOf('sw')==0) && (t.Status == 'Completed')) //Check to see if the Last_SW_Activity__c field is current compared with the latest completed activity If (acctMap.get(t.accountID).Last_SW_Activity__c < t.LastModifiedDate || acctMap.get(t.accountID).Last_SW_Activity__c ==null){ //Update the Last_SW_Activity__c field on the account object with the task's end date acctrec.Last_SW_Activity__c = t.LastModifiedDate; updAccts.add(acctrec); } } update updAccts; }
Can somebody please help make the trigger code more efficient (bulkifying it) and help with creating a test class? I'm not a developer and could use some serious help here. Thanks!!
Okay, I guess the WhoId has to be for a contact, so change your test like this:
All Answers
To bulkify the trigger, just move the SOQL query out of the for loop like this:
And your test class could be something like this :
I may have missed a few fields, but that is most of it I believe.
If you want to test 85000, just change num to 85000, but that may still cause an error because you may need to do a batch job instead.
Hi
see this link
http://wiki.developerforce.com/page/Apex_Code_Best_Practices
Thanks for your help SLockard - I really appreciate it. The trigger appears to work fine but the test class is resulting in a failure with the following error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 001P000000Z4U1sIAF: [WhoId]
Okay, I guess the WhoId has to be for a contact, so change your test like this:
Worked perfectly, thank you so much!
No problem, I'm glad I could help!