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
Doug Traster 4Doug Traster 4 

need help with trigger to fill in junction object

Hi all;

I have 3 accounts (name territory update, accounts for territories, and a junction account).  when you update name and terrtiory it posts to the junction.  what I want to have is a trigger that then updates the accounts based on info (state, org code, territorynameid) update the junciton.  Here is what I have so far, please help!

trigger addAcctsToJunc on Territory_Account_Junction__c (after insert, after update) {
    
   Set<string> accountIds = new Set<string>();
    for(Territory_Account_Junction__c taj : trigger.new)
        if(taj.TerritoryNameId__c !=Null)
        accountIds.add(taj.TerritoryNameId__c);
        System.debug('get all of the states ' + accountIds);
        
    
    Map<String,Accts_for_Territories__c> acountMap = new Map<String, Accts_for_Territories__c>([Select Name__c, accountID__c
                                                      FROM Accts_for_Territories__c where TerritoryNameId__c in :accountIds]);
    system.debug('all accounts '  + acountMap);
    
    Account thisAccount;
    
    
    List<Territory_Account_Junction__c> acctTerrRep = new List<Territory_Account_Junction__c>();
    
    //system.debug('get info from list ' + acctTerrRep);
    for(Territory_Account_Junction__c taj : trigger.new)
        
       
    {
        if(taj.Billing_State__c !=NULL)
        {
            
            thisAccount = acctTerrRep.get(taj.TerritoryNameID__c);
            //thisAccount = acctTerrRep.get(taj.)
            
            system.debug('info in This Account ' +thisAccount);
            If(thisAccount != NULL){
                
                if(thisAccount.Name !=Null)
                    acctTerrRep.add(thisAccount.Name);
                
                taj.Account__c = String.join(acctTerrRep, ',');
            }
        }
    }