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
Juan Miguel FranciaJuan Miguel Francia 

Need Help to finish my 1st afterInsert Trigger Handler

So what I need to do is 
1st.) Get the newly created MatterID
2nd.) Query newly created OpportunityAccount for where OpportunityId = Matter__c.Project_Name__c (OpptyId)
3rd.) Get List based on Query.
4th.) Make List listOfMatterAccounts.
5th.) insert the data to a for loop.
Can you please help me finish my code. I created a something but I feel like it is not the right way to do it.
This is the original pseudo code given to me.
 
Add Related Vendor records to the Opportunity.
    
    Create New Matter from Opportunity [New Matter]...
    
    Make selections on Matter edit page....  
    
    Save.
    
    PB or afterInsert Trigger Handler gets Opportunity Id from Save event.

    ----Start Code Here----

    Get new id for Matter.... <new matter id>
    
    Query newly created OpportunityAccount for where OpportunityId = Matter__c.Project_Name__c (OpptyId)
    
    Get List<Account> based on Query.
    
    Make List<MatterAccount__c> listOfMatterAccounts.
    
    for(Account a : listofOpportunityAccounts) {
    MatterAccount__c ma = new MatterAccount();
    ma.matterid = <new matter id>
    ma.accountid = a.id
    ma.opportunityid = 
    ma.Matter__c.Project_Name__c
    }
    
    listofMatterAccounts.insert(ma);


This is my code so far.

 

public static void RelatedVendor(){
        
        //get new MatterID
      ID RecordID;
      List<Matter__c> listMatter = new List<Matter__c>();
      List<OpportunityAccount__c> opporAccList = [Select id, opportunity__c from OpportunityAccount__c where id in : Trigger.new];
      List<MatterAccount__c> mattAccList = new List<MatterAccount__c>();
      
        for(Matter__c m :(List<Matter__c>)Trigger.new){
            RecordID = m.Id;
            for(OpportunityAccount__c oa :opporAccList){
                oa.Opportunity__c = m.Project_Name__c;
                opporAccList.add(oa);
            }	
            
            for(Account a : opporAccList){
                MatterAccount__c ma = new MatterAccount__c();
                ma.Matter__c = RecordID;
                ma.Vendor__c = a.ID;
                ma.Opportunity__c = m.Project_Name__c;
                mattAccList.add(ma);
            }
            
            insert mattAccList;
        }
            	
    }
Best Answer chosen by Juan Miguel Francia
Nayana KNayana K
Class :
public class MatterTriggerHandler
{
	public void onAfterInsert(List<Matter__c> lstNewMatter)
	{
		createMatterAccounts(lstNewMatter);
	}
	
	private void createMatterAccounts(List<Matter__c> lstNewMatter)
	{
		Set<Id> setIdOpp = new Set<Id>();
		Map<Id, Id> mapIdOppToAccountId = new Map<Id, Id>();
		List<MatterAccount__c> lstMatterAccount = new List<MatterAccount__c>();
		
		
		// collect opportunity ids from the matters
		for(Matter__c objMatter : Trigger.New)
		{
			if(objMatter.Project_Name__c != null)
				setIdOpp.add(objMatter.Project_Name__c);
		}
		
		// collect related account from the OpportunityAccounts
		if(!setIdOpp.isEmpty())
		for(OpportunityAccount__c objOA : [SELECT Id, Account__c, Opportunity__c FROM OpportunityAccount__c WHERE Opportunity__c IN: setIdOpp])
		{
			mapIdOppToAccountId.put(objOA.Opportunity__c, objOA.Account__c);
		}
		
		for(Matter__c objMatter : Trigger.New)
		{
			if(mapIdOppToAccountId.containsKey(objMatter.Project_Name__c))
			{
				MatterAccount__c objMA = new MatterAccount__c();
                objMA.Matter__c = objMatter.Id;
                objMA.Vendor__c = mapIdOppToAccountId.get(objMatter.Project_Name__c);
                objMA.Opportunity__c = objMatter.Project_Name__c;
                lstMatterAccount.add(objMA);
			}
				
		}
		
		if(!lstMatterAccount.isEmpty())
			insert lstMatterAccount;
		
	}
}

Trigger :
trigger MatterTrigger on Matter__c (after insert)
{
	MatterTriggerHandler objHandler = new MatterTriggerHandler();
	
	if(Trigger.isAfter && Trigger.isInsert)
	{
		objHandler.onAfterInsert(Trigger.New);
	}
}

 

All Answers

Nayana KNayana K
Class :
public class MatterTriggerHandler
{
	public void onAfterInsert(List<Matter__c> lstNewMatter)
	{
		createMatterAccounts(lstNewMatter);
	}
	
	private void createMatterAccounts(List<Matter__c> lstNewMatter)
	{
		Set<Id> setIdOpp = new Set<Id>();
		Map<Id, Id> mapIdOppToAccountId = new Map<Id, Id>();
		List<MatterAccount__c> lstMatterAccount = new List<MatterAccount__c>();
		
		
		// collect opportunity ids from the matters
		for(Matter__c objMatter : Trigger.New)
		{
			if(objMatter.Project_Name__c != null)
				setIdOpp.add(objMatter.Project_Name__c);
		}
		
		// collect related account from the OpportunityAccounts
		if(!setIdOpp.isEmpty())
		for(OpportunityAccount__c objOA : [SELECT Id, Account__c, Opportunity__c FROM OpportunityAccount__c WHERE Opportunity__c IN: setIdOpp])
		{
			mapIdOppToAccountId.put(objOA.Opportunity__c, objOA.Account__c);
		}
		
		for(Matter__c objMatter : Trigger.New)
		{
			if(mapIdOppToAccountId.containsKey(objMatter.Project_Name__c))
			{
				MatterAccount__c objMA = new MatterAccount__c();
                objMA.Matter__c = objMatter.Id;
                objMA.Vendor__c = mapIdOppToAccountId.get(objMatter.Project_Name__c);
                objMA.Opportunity__c = objMatter.Project_Name__c;
                lstMatterAccount.add(objMA);
			}
				
		}
		
		if(!lstMatterAccount.isEmpty())
			insert lstMatterAccount;
		
	}
}

Trigger :
trigger MatterTrigger on Matter__c (after insert)
{
	MatterTriggerHandler objHandler = new MatterTriggerHandler();
	
	if(Trigger.isAfter && Trigger.isInsert)
	{
		objHandler.onAfterInsert(Trigger.New);
	}
}

 
This was selected as the best answer
Juan Miguel FranciaJuan Miguel Francia
Thanks for the response, I appreciate it so much but is there any simple way to code it?
Nayana KNayana K
As per my knowledge, this code follows neat approach and is optimized. Here I have avoided unnecessary for inside for loops as the logic can be handled through collections like list,map, set. I can say performance wise it works well. Incase if you find better code, feel free to share here so that it helps others. 
Juan Miguel FranciaJuan Miguel Francia
Thanks Nayaka. It works perfectly fine. I have a question though what is the use of the public void and private void that you created stated above and why not just use a one public static void.
Nayana KNayana K
In my company, we follow this standard for triggers. As you can see, in trigger I am just calling objHandler.onAfterInsert(); so in the handler class it has to be public. As I am not calling createMatterAccounts() from trigger I kept private...Also instead of directly writing logic of creating matter accounts inside onAfterInsert, I used sperate method (createMatterAccounts). Reason is that once we develop code , if some extra logic has to be done by somewhere developer, they can create seperate method (private) and add inside onAfterInsert.    

I have seen some of the predeveloped code where they use static methods, it doesn't make any difference here. It is just the matter of keeping things organized.

 
Nayana KNayana K
Incase answer helped you, please mark this post as solved.
Juan Miguel FranciaJuan Miguel Francia
Thanks Nayana :)
 
Nayana KNayana K
Most welcome :)