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
Leonardo Girgenti 5Leonardo Girgenti 5 

Help with a Trigger

Hi there,
We at UNICEF require a little help with the creation of a Trigger and related TestClass for the following scenario:

We have a custom object called Due Diligence (Due_Diligence__c).
This is an object that has a MasterDetail relation to Account.
 
Due Diligence records are managed by researchers and the process ends with a DD Outcome assigned to the Due Diligence Record (PickList field called: DD_Phase_1_Outcome__c) at the DD record level.
The record is then put through a standard Approval Process.
 
We would like that at the Approval, when DD record is approved the approval process updates a field on the Due Diligence record called DD Record Status (Approval_status__c) with a status of “Phase 1 Review Completed”, the DD record then updates the Account in the relevant field in the Account record (DD_Outcome_Phase_1__c) with the outcome coming from the DD Records.
 
We would also want to pass the Approval Date also from the DD Record to the Account record.

Currently we are handling that via a series of Workflow but we only have the ability to update the Account record after 1 hour (we are using Time Based Field Updates) 
Thanks,
L
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Leonardo,

  Take a look at below code and try to use it:

NOTE:
  
Assuming the approval date on account objects api name is Approval_Date__c (as you havent mentioned any, if not replace it in the class code). This code works if the status is 


Trigger code:
/* Apex Trigger */
Trigger  DueDiligenceTrigger on Due_Diligence__c(before insert,before update, after insert, after update,before delete, after delete, after undelete){


    if((trigger.isInsert || trigger.isUpdate) && trigger.isAfter){
        
        DueDeligenceHandler.UpdateAccount(trigger.newMap);


    }

}

Apex Calss That handles requests from trigger:
 
/* Apex Class That Handles The Requests From Trigger*/

Public Class DueDeligenceHandler{
//Method that handles the account update

  public static void UpdateAccount(Map<Due_Diligence__c> DueDiligenceNewMap){
		
		List<Account>AccountListForUpdate=new List<Account>();
	for(Due_Diligence__c DueDiligenceRecord: DueDiligenceNewMap.values()){
		
		if(DueDiligenceRecord.Approval_status__c=='Phase 1 Review Completed'){
			AccountListForUpdate.add(new Account(id=DueDiligenceRecord.Account__c,DD_Outcome_Phase_1__c=DueDiligenceRecord.DD_Phase_1_Outcome__c,Approval_Date__c=Date.Today()));
		}
	}
	
	If(AccountListForUpdate!=Null && AccountListForUpdate.size()>0){
		try{
			Update AccountListForUpdate;
		}catch(Exception e){
			System.debug('Error Occured ***'+e.getMessage());
			
		}
	}
	
	
  }

}

Test Class:
 
//Test Class

@isTest
Public Class DueDiligenceTrigger_isTest{

	public static testMethod void testAccountUpdate(){
		
		Account testAcc=new Account(Name='Some Sample Name'+Date.Today());
		
		insert testAcc;
		
		Due_Diligence__c testDueDiligence=new Due_Diligence__c(Name='Some Diligence Name',Account__c=testAcc.id, DD_Outcome_Phase_1__c='Sample Out Come',Approval_status__c='Phase 1 Review Completed');
		
		insert testDueDiligence;
		
		update testDueDiligence;
		
		
	}
	
}

Hope it helps,

 If you find any compile erros, post the error back, i will reply to it .,

  UNICEF keep rocking!!

Thanks,
Balaji C Garapati
Balaji Chowdary GarapatiBalaji Chowdary Garapati
a minor change in class code:
 
/* Apex Class That Handles The Requests From Trigger*/

Public Class DueDeligenceHandler{
//Method that handles the account update

  public static void UpdateAccount(Map<id,Due_Diligence__c> DueDiligenceNewMap){
		
		List<Account>AccountListForUpdate=new List<Account>();
	for(Due_Diligence__c DueDiligenceRecord: DueDiligenceNewMap.values()){
		
		if(DueDiligenceRecord.Approval_status__c=='Phase 1 Review Completed'){
			AccountListForUpdate.add(new Account(id=DueDiligenceRecord.Account__c,DD_Outcome_Phase_1__c=DueDiligenceRecord.DD_Phase_1_Outcome__c,Approval_Date__c=Date.Today()));
		}
	}
	
	If(AccountListForUpdate!=Null && AccountListForUpdate.size()>0){
		try{
			Update AccountListForUpdate;
		}catch(Exception e){
			System.debug('Error Occured ***'+e.getMessage());
			
		}
	}