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 

Trigger to Update Account when Task Logged

Hey Guys,

 

Been busy, writing my second ever Trigger.

 

This one is a bit more complex than my first, but no where near advanced.

 

What I am trying to do, is when a task with 'purpose' X or Y is logged, it takes the Description field and adds it to a custom field in the Account.

 

My Trigger is: 

trigger LoworProCommunicationLoggingAutomated on Task (after insert) {
Set<Id> acctIds = new Set<Id>();
    String DescToUpdate;
    String Purp;

    
for(Task t: [SELECT Subject, WhatId, Status, Purpose__c, Description FROM TASK WHERE ID IN:trigger.new]){
	
		String wId = t.WhatId;
		if(wId!=null && wId.startsWith('001') && !acctIds.contains(t.WhatId) && t.Status == 'Completed' && (t.Purpose__c.Equals('Proactive Communication') || t.Purpose__c.Equals('Low-Redemption Communication'))){
		acctIds.add(t.WhatId);
		DescToUpdate = t.Description;
		Purp = t.Purpose__c;

}

}
    for(Account a:[SELECT Low_Redemption_Notes__c, Proactive_Communication_Notes__c FROM Account WHERE ID IN:acctIds]){
    	if(Purp == 'Proactive Communication'){
                 a.Proactive_Communication_Notes__c = DesctoUpdate;
        update a; 
    	}
        else if (Purp == 'Low-Redemption Communication'){
        	a.Proactive_Communication_Notes__c = DesctoUpdate;
        	a.Low_Redemption_Notes__c = DesctoUpdate;
        update a;
        }
        
    }  
}

 

My Test Class is:

@isTest
private class LowProCommTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        Account a = new Account(name='Test');
        insert a;
 
        Task t = new Task(subject='Test Activity', Status = 'Completed', Purpose__c = 'Proactive Communication', Description = 'I have Called This Dealership', whatId = a.id);
        insert t;
 
        a = [SELECT ID, Low_Redemption_Notes__c, Proactive_Communication_Notes__c  FROM Account WHERE ID = :a.id];
        System.assertEquals(t.Description, a.Proactive_Communication_Notes__c);
 
 
    }
 
}

 

 

Obviously, I have 0% coverage on my code.

 

However, before I attack that, I am running in to an issue that the notes field and the description are not equal as evidenced by my system.assertequals throwin an error.

 

I would love an actual description as to what I am doing wrong here so I can keep learning.

Best Answer chosen by Admin (Salesforce Developers) 
MBarnardMBarnard
trigger AccountUpdateonLogCall on Task (after insert, after update) {
	
	//To do - If the Purpose of a completed task contains "Low Redemption" or "Proactive", put the description of the completed task in the 
	//the "Low_Redemption_Notes__c" and/or "Proactive_Communication_Notes__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//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, Proactive_Communication_Notes__c, Low_Redemption_Notes__c  from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.whatID);

//If the account ID isn't null, the Purpose is Pro or Low, and the task has been marked as completed    
//Check to see if the which fields to update based upon the Purpose__c field.
  	  if (t.accountID != null && t.Purpose__c.Equals('Proactive Communication') && t.Status == 'Completed' && t.Type == 'Call'){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
  		  acctrec.Proactive_Communication_Notes__c = t.Description;
  		  update acctRec;
  	  }
  	  else if (t.accountID != null &&  t.Purpose__c == ('Low-Redemption Communication') && t.Status == ('Completed')  && t.Type == ('Call')){
  	  	  acctrec.Proactive_Communication_Notes__c = t.Description;
  	  	  acctrec.Low_Redemption_Notes__c = t.Description;
  		  update acctRec;
  	}
  }
}

 

Stupid, stupid mistake... Had to change 

 Account acctRec = acctMap.get(t.accountID);

' to "

 Account acctRec = acctMap.get(t.whatID);

All Answers

MBarnardMBarnard

Still working on this, I believe i need to completely re-attack this problem and start from a different angle.

 

I am missing any sort of mapping for the account values and in general, have some very poor practice in this code

MBarnardMBarnard

So, I re-wrote a lot of my code and am still having some issues. I suck at Test Classes

 

Trigger:

trigger AccountUpdateonLogCall on Task (after insert, after update) {
	
	//To do - If the Purpose of a completed task contains "Low Redemption" or "Proactive", put the description of the completed task in the 
	//the "Low_Redemption_Notes__c" and/or "Proactive_Communication_Notes__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//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, Proactive_Communication_Notes__c, Low_Redemption_Notes__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.Purpose__c.Equals('Proactive Communication') ||t.Purpose__c.Equals('Low-Redemption Communication') ) && (t.Status == 'Completed'))
//Check to see if the which fields to update based upon the Purpose__c field.
  	  if (t.Purpose__c == 'Proactive Communication'){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
  		  acctrec.Proactive_Communication_Notes__c = t.Description;
  		  update acctRec;
  	  }
  	  else if (t.Purpose__c == 'Low-Redemption Communication'){
  	  	  acctrec.Proactive_Communication_Notes__c = t.Description;
  	  	  acctrec.Low_Redemption_Notes__c = t.Description;
  		  update acctRec;
  	}
  }
}

 Test Class:

@isTest
private class TestClass {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        Account a = new Account(name='Test');
        insert a;
        Account b = new Account(name='Test2');
        insert b;
        
 		       
        Task t = new Task(subject='Test Activity', Status = 'Completed', Purpose__c = 'Low-Redemption Communication', Description = 'I have Called This Dealership', whatId = a.id);
        insert t;
        Task t2 = new Task(subject='Test Activity', Status = 'Completed', Purpose__c = 'Proactive Communication', Description = 'I have Called This Dealership2', whatId = b.id);
        insert t2;
        test.startTest();
        
        a = [SELECT ID, Low_Redemption_Notes__c, Proactive_Communication_Notes__c  FROM Account WHERE ID = :a.id];
        b = [SELECT ID, Low_Redemption_Notes__c, Proactive_Communication_Notes__c  FROM Account WHERE ID = :b.id];
        System.assertEquals(t.Description, a.Low_Redemption_Notes__c);
        System.assertEquals(t.Description, a.Proactive_Communication_Notes__c);
        System.assertEquals(t.Description, b.Proactive_Communication_Notes__c);
 		test.stopTest();
    }
}

 

Issues: Still getting an Assertion Failure: t.description and a.Low_Redemption_Notes don't match.

Still not test coverage.

 

Can someone give me some assistance? Still a huge n00b

MBarnardMBarnard
trigger AccountUpdateonLogCall on Task (after insert, after update) {
	
	//To do - If the Purpose of a completed task contains "Low Redemption" or "Proactive", put the description of the completed task in the 
	//the "Low_Redemption_Notes__c" and/or "Proactive_Communication_Notes__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//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, Proactive_Communication_Notes__c, Low_Redemption_Notes__c  from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.whatID);

//If the account ID isn't null, the Purpose is Pro or Low, and the task has been marked as completed    
//Check to see if the which fields to update based upon the Purpose__c field.
  	  if (t.accountID != null && t.Purpose__c.Equals('Proactive Communication') && t.Status == 'Completed' && t.Type == 'Call'){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
  		  acctrec.Proactive_Communication_Notes__c = t.Description;
  		  update acctRec;
  	  }
  	  else if (t.accountID != null &&  t.Purpose__c == ('Low-Redemption Communication') && t.Status == ('Completed')  && t.Type == ('Call')){
  	  	  acctrec.Proactive_Communication_Notes__c = t.Description;
  	  	  acctrec.Low_Redemption_Notes__c = t.Description;
  		  update acctRec;
  	}
  }
}

 

Stupid, stupid mistake... Had to change 

 Account acctRec = acctMap.get(t.accountID);

' to "

 Account acctRec = acctMap.get(t.whatID);
This was selected as the best answer