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
Dave ShulmanDave Shulman 

Trigger update distributor Relationship on account code help

The goal here is to total the Sales Data related to each account for each distributor.  Then see which is the #1 distributor for each account then update the account to reflect that.  My code is throwing no errors, but it isnt updating the field.  Wheres the issue here? I cant find it at all.

Thought it was the SOQL query, but went throug hthat many times and thats definitely pulling the data i want.

Take a look.  Also im pretty new to this so stylistic recommendations are helpful too.
trigger DistributorIDs on Sales_data__c (after insert, after update) {
       Double Amount = 0;
       Double UpdateMap = 0;
       ID PrimaryDistributor;
       Date AfterDate =;
       Set<Id>accountIds = new Set<Id>(); //account in
       Map<ID, Double> DistributorsList = new Map<ID, Integer>(); //distributors in
       for (Sales_data__c Sd: {
           if(Sd.End_user__c!=null && Sd.Distributor__r!=null){
                accountIds.add(Sd.End_user__c); // adding accounts to set
                DistributorsList.put(Sd.Distributor__c, 0);  // adding distributors to map
       Map<ID, Double> DistributorsCopy = DistributorsList.clone(); //clone 0'd map

       List<Account> accountToUpdate = new List<Account>(); //list for DML
       List<Account> accList = new List<Account>([SELECT Id,(SELECT Distributor__c,Amount__c,Date__c FROM 
                                                 Sales_data1__r WHERE Date__c >= :AfterDate) // used sub query on Sales Data
                                                 FROM Account WHERE Id IN :accountIds]);
       for(Account acc : accList){ //looping through every account of above list
            for(Sales_Data__c sd : acc.Sales_Data__r){  // looping thorugh every child of the account
                if(sd.Amount__c !=null && sd.Distributor__c!=''){
                    UpdateMap = DistributorsList.get(sd.Distributor__c) + sd.Amount__c; //populate var w updated amt for ea dist
                    DistributorsList.remove(sd.Distributor__c);  //remove old value from map
                    DistributorsList.put(sd.Distributor__c, UpdateMap); //push new one value to map
            for (Id key: DistributorsList.keySet()) { //iterate through distributors map
                if(DistributorsList.get(key) > Amount){ //if greater than 0 to start, then greater than max
                    PrimaryDistributor = key;  //set primarydist = key if its greatest so far
                    Amount = DistributorsList.get(key); //set amount equal to value
            acc.Acct_Primary_Distributor__c = PrimaryDistributor;   // adding largest distributors ID to parent account
            DistributorsList.clear(); //returning map values to 0 before next iteration
      if(accountToUpdate.size() > 0){
        update accountToUpdate;

ShirishaShirisha (Salesforce Developers) 
Hi Dave,


Can you please try to capture the debug logs with the finest level to see the code execution to figureout which line is causing the issue.Also,I would suggest you to add the debug statements to see,if the SOQL queries returning the records as expected.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri