function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MBarnardMBarnard 

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

Sagarika RoutSagarika Rout

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
 

@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;
        //as the trigger is written on Task before insert and before update so you need to insert and update task in test class      
        Task testTask = new Task(Subject= 'Test Task', WhatID = testAccount.Id);
        insert testTask;
		testTask.Subject = 'Update Test Task';
		update testTask;
                 
        System.assertEquals(testtask.AccountId, testAccount.Id);
    }
} 

 I hope it will work

 

regards

Sagarika Rout

SFDC developer

MBarnardMBarnard

AWSOME that worked.

 

I did have to change: 

System.assertEquals(testtask.AccountId, testAccount.Id);

 to

System.assertEquals(testtask.WhatId, testAccount.Id);

 to make it pass.

MBarnardMBarnard

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

MBarnardMBarnard

This is what is logged in the Debug

 

Apex Debug Log Detail

 
Apex Debug Log Detail
User	test	Date	11/15/2013 5:02:44 PM EST
Status	Attempt to de-reference a null object	Application	Browser
Request Type	Application	Operation	/setup/own/deleteredirect.jsp
Duration (ms)	74	Log Size (bytes)	170
Log	
19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
17:02:44.042 (42701000)|ENTERING_MANAGED_PKG|qc

 

asish1989asish1989

Try this 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);
		}
	}
	if(!acctIds.isEmpty()){
		// 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
	if(!acctMap.isEmpty()){
	
		for(Task t : trigger.new){
			String wId = t.WhatId;
			if(wId!=null && wId.startswith('001')){
				Account thisAcct = acctMap.get(t.WhatId);
				if(thisAcct!=null && thisAcct.Account_Status__c !=null){
					t.Account_Status__c = thisAcct.Account_Status__c;
				} 
			}
		}
	}