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
Christine KleinChristine Klein 

Trigger Assistance Needed

I'm creating a custom object which will be a related list on Accounts.  The object is called Account Coverage.  It will act similar to account teams where you can list several account owners in the related list.  Unfortunately we can't customize Account Teams which is why we have to create this custom object.

Coverage fields were added to the account object and get updated based on users added to the Account Coverage object.  If a user is added to Account Coverage and the coverage checkbox is checked and your market unit meets the specific criteria, then your name is added to the corresponding account coverage fields on the account object.

Everything seems to be working ok, I'm just stuck on updating the coverage fields if the coverage checkbox is unchecked.  Any insight on how I can remove names from the coverage fields once the coverage checkbox is unchecked would be greatly appreciated.

Thanks!


public with sharing class AccountCoverageTriggerHandler {
	private boolean triggerIsExecuting = false;
   
    private integer batchSize = 0;
				
 	public boolean isTriggerContext{
        get { return triggerIsExecuting;}
    }

    public boolean isVisualforcePageContext{
        get { return !isTriggerContext;}
    }

    public boolean isWebServiceContext{
        get { return !isTriggerContext;}
    }

    public boolean isExecuteAnonymousContext{
        get { return !isTriggerContext;}
    }
    
  	public AccountCoverageTriggerHandler(boolean isExecuting, integer size){
    	triggerIsExecuting = isExecuting;
    	BatchSize = size;
  	}  	
  	
  	public void UpdateAccountCoverageInformation(list<Account_Coverage__c> Account_Coverage)
  	{
  		list<string> accts = new list<string>();
  		list<string> accountCoverage = new list<string>();
  		
  		for(Account_Coverage__c coverage : Account_Coverage)
  		{
  			accts.add(coverage.Account_Name__c);
  			accountCoverage.add(coverage.ID);
  		}
  		
  		list<Account> acct = [SELECT ID,Coverage_BPM__c,Coverage_Enterprise__c,Coverage_FGM__c
  										,Coverage_Global__c,Coverage_Inside_Sales__c,Coverage_IX__c
  										,Coverage_Portfolio__c,Coverage_Services__c
  										,(SELECT ID,Account_Name__c,Coverage__c,Account_Owner__r.FirstName,Account_Owner__r.LastName
  													,Account_Owner__c,Name,MU__c,BU__c,SR__c 
  													FROM Account_Coverage__r) 
  										FROM Account WHERE ID in : accts];										
  													
  		system.debug('accts = ' + acct);
  		system.debug('Account_Coverage = ' + Account_Coverage);
  		
  		for(Account accountForUpdate : acct)	
  		{
  			string coverageEnterprise = '';
  			string coverageIX = '';
  			string coverageBPM = '';
  			string coverageCorporateGlobal = '';
  			string coveragePortfolio = '';
  			string coverageFGM = '';
  			string coverageProfessionalServices = '';
  			string coverageMarketDevelopment = '';
  			
 			if(accountForUpdate.Account_Coverage__r == null ||  accountForUpdate.Account_Coverage__r.size() <1)
 			{
 				coverageEnterprise = '';
 				coverageIX = '';
  				coverageBPM = '';
  				coverageCorporateGlobal = '';
  				coveragePortfolio = '';
  				coverageFGM = '';
  				coverageProfessionalServices = '';
  				coverageMarketDevelopment = ''; 				
 			}
 			else
 			{
	  			for(Account_Coverage__c accCov : accountForUpdate.Account_Coverage__r)
	  			{
	  					system.debug('accCov' + accCov);
	
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('Enterprise'))
	  					{
	  						if(coverageEnterprise == '')
	  						{
	  							coverageEnterprise =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('ENT Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('ENT Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageEnterprise = coverageEnterprise + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('ENT Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('ENT Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('IX (Information Exchange)'))
	  					{
	  						if(coverageIX == '')
	  						{
	  							coverageIX =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('IX Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('IX Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageIX = coverageIX + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('IX Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('IX Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					} 
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('BPM'))
	  					{
	  						if(coverageBPM == '')
	  						{
	  							coverageBPM =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('BPM Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('BPM Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageBPM = coverageBPM + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('BPM Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('BPM Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					} 
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('Market Development'))
	  					{
	  						if(coverageMarketDevelopment == '')
	  						{
	  							coverageMarketDevelopment =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('MD Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('MD Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageMarketDevelopment = coverageMarketDevelopment + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('MD Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('MD Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('Professional Services'))
	  					{
	  						if(coverageProfessionalServices == '')
	  						{
	  							coverageProfessionalServices =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('PS Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('PS Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageProfessionalServices = coverageProfessionalServices + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('PS Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('PS Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}	
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('Portfolio'))
	  					{
	  						if(coveragePortfolio == '')
	  						{
	  							coveragePortfolio =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('Portfolio Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('Portfolio Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coveragePortfolio = coveragePortfolio + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('Portfolio Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('Portfolio Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}	
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('Corporate Global'))
	  					{
	  						if(coverageCorporateGlobal == '')
	  						{
	  							coverageCorporateGlobal =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('Corp Global Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('Corp Global Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageCorporateGlobal = coverageCorporateGlobal + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('Corp Global Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('Corp Global Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}	  
	  					if(accCov.Coverage__c == true && accCov.MU__c.contains('FGM'))
	  					{
	  						if(coverageFGM == '')
	  						{
	  							coverageFGM =  accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	  							system.debug('FGM Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('FGM Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName);
	  						}
	  						else
	  						{
	  							coverageFGM = coverageFGM + accCov.Account_Owner__r.FirstName + ' ' +  accCov.Account_Owner__r.LastName  + ';';
	   							system.debug('FGM Account Owner First Name = '+ accCov.Account_Owner__r.FirstName);
	  							system.debug('FGM Account Owner Last Name Name = '+ accCov.Account_Owner__r.LastName); 							
	  						}
	  					}		  										  					  						  						  					
	  			}
 			}
  			
  			accountForUpdate.Coverage_Enterprise__c = coverageEnterprise;
  			accountForUpdate.Coverage_IX__c = coverageIX;
  			accountForUpdate.Coverage_BPM__c = coverageBPM;
  			accountForUpdate.Coverage_FGM__c = coverageFGM;
  			accountForUpdate.Coverage_Global__c = coverageCorporateGlobal;
  			accountForUpdate.Coverage_Inside_Sales__c = coverageMarketDevelopment;
  			accountForUpdate.Coverage_Portfolio__c = coveragePortfolio;
  			accountForUpdate.Coverage_Services__c = coverageProfessionalServices;
  		}
  		
  		if(acct.size()>0)
  		{
  			update acct;
  		}																	
  		
  	} 	
 	/*
  	public void onBeforeInsert(Account_Coverage__c[] newAccountCoverage){

    }
    */
    /*
    @future public static void onAfterInsertAsync(Set<ID> newAccountCoverageIDs){

    }
    */
 	
 	public void OnAfterInsert(Account_Coverage__c[] oldAccountCoverage, Account_Coverage__c[] updatedAccountCoverage, Map<ID, Account_Coverage__c> oldAccountCoverageMap) {
		UpdateAccountCoverageInformation(updatedAccountCoverage);  
 	}
	
 	/*
 	public void onBeforeUpdate(Account_Coverage__c[] oldAccountCoverage, Account_Coverage__c[] updatedAccountCoverage, Map<ID, Account_Coverage__c> oldAccountCoverageMap) {
		 
    }
	*/
	
    public void onAfterUpdate(Account_Coverage__c[] oldAccountCoverage, Account_Coverage__c[] updatedAccountCoverage, Map<ID, Account_Coverage__c> oldAccountCoverageMap) {
		UpdateAccountCoverageInformation(updatedAccountCoverage);
    }
	/*
    @future public static void onAfterUpdateAsync(Set<ID> updatedAccountCoverage){
		
    }
    */
    /*
    public void onBeforeDelete(Account_Coverage__c[] AccountCoverageToDelete, Map<ID, Account_Coverage__c> AccountCoverageMap){

    }
    */
    
    public void onAfterDelete(Account_Coverage__c[] deletedAccountCoverage, Map<ID, Account_Coverage__c> AccountCoverageMap){
		UpdateAccountCoverageInformation(deletedAccountCoverage); 
    }
    

    /*
    @future public static void onAfterDeleteAsync(Set<ID> deletedAccountCoverage){
        //to make this work like the on after delete you need to do a select statement into 
        //a list where ID in :deletedOpportunitySplits, this would recreate the deletedOpportunitySplits list
        //from there the code would work the same.
    }
    */

    /*
    public void onUndelete(Account_Coverage__c[] restoredAccountCoverage){

    }
    */

}


nitin sharmanitin sharma
Hi ,

Given below is the basic exmmple between contact and account object.I have text field on the account object.based on teh state of the checkbox field on the contact object I am either adding  value to the text fiedl or making it blank .

This might give you some idea about how u can do things.

If u do something like this then please consider the fact that you have to make use of Trigger.old variable ,You have to check the state of the checkbox field in the trigger.old variable.



trigger Criss on Contact (after update) {
map<id,contact> c=new map<id,contact>();
for(Contact cl:trigger.new)
{
c.put(cl.accountid,cl);
}

list<account>lac=new list<account>();
list<account>acc=[select id,name,newname__c from account where id in:c.keyset()];
for(account sap:acc)
{

Boolean b=c.get(sap.id).connect__c;

if(b==true)
{

sap.newname__c='Nothing is impossible';
lac.add(sap);
}
else
{
sap.newname__c='';
lac.add(sap);
}
}
update lac;


}