You need to sign in to do that
Don't have an account?
MBarnard
Apex Trigger for Account Details into Activity - Error and Code issues
Hey Guys,
I am 90% sure I have this trigger a step away from completion.
This is the trigger:
trigger ActivityAccountDetails on Task (before insert, before update) { // Goal: Find the Account Status of the Account that a task is related to // and update the Account_Status__c field on the task object with the value // If related to an Account, query to find out the Account Status // Create collection of tasks that are related to an Acct (where the Acct is listed only once) Set<Id> acctIds = new Set<Id>(); for(Task t : trigger.new){ String wId = t.WhatId; if(wId!=null && wId.startsWith('001') && !acctIds.contains(t.WhatId)){ acctIds.add(t.WhatId); } } // Pull in acct ids and related field to populate task record List<Account> taskAcct = [Select Id, Account_Status__c from Account where Id in :acctIds]; Map<Id, Account> acctMap = new Map<Id, Account>(); for(Account a : taskAcct){ acctMap.put(a.Id,a); } // Update custom task field with custom opp field for(Task t : trigger.new){ String wId = t.WhatId; if(wId!=null && wId.startswith('001')){ Account thisAcct = acctMap.get(t.WhatId); if(thisAcct!=null){t.Account_Status__c = thisAcct.Account_Status__c;} } }
This is my Test Class:
@isTest private class MyTest { static testMethod void myUnitTest() { // TO DO: implement unit test Account testAccount = new Account(name='Test Company Name'); insert testAccount; testAccount.Account_Status__c='Prospect: Open'; update testAccount; Task testTask = new Task(Subject= 'Test Task', WhatID = testAccount.Id); Account accountInfo = [SELECT Account_Status__c FROM Account WHERE Id = :testAccount.Id]; System.assertEquals(accountInfo.Account_Status__c, testtask.Account_Status__c); insert testTask; System.assertEquals(testtask.AccountId, testAccount.Id); } }
I have two issues.
1: My test class isn't covering the trigger, at all. I have no idea how to fix that.
2: I have an error saying 'accountInfo.Account_Status__c is not equal to testtask.Account_Status__c' (or the syntax equivalent to that error)
I need a solution (or two) so that I can get this test coverage to apply to my trigger and why that error is occuring.
Thanks!
first ever Apex Trigger, so expect the craziness
This test class give error as you have not insert task and call System.assertEquals(accountInfo.Account_Status__c, testtask.Account_Status__c).
it is not able to find testtask Id, then how it will compare.
To cover your trigger in test class I am modifying your code below
I hope it will work
regards
Sagarika Rout
SFDC developer
AWSOME that worked.
I did have to change:
to
to make it pass.
Hey Guys,
Running in to an issue when someone tries to 'delete' a task
The error being thrown is:
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger qc.TaskTrigger caused an unexpected exception, contact your administrator: qc.TaskTrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: (qc)".
any help, GREATLY appreciated.
Thanks
This is what is logged in the Debug
Apex Debug Log Detail
Try this Trigger