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
Shawn Reichner 29Shawn Reichner 29 

Trigger not updating record

Hello Awesome Developers.....

I have the following trigger that should be updating an Apttus Quote Record based on Line Items that are added.  

Ultimately what I am trying to achieve is when a new Apttus config file is created (gets created when a cart is finalized) and is marked finalized for its status, look for the line Items associated with that Apttus config record, and if any of those items has a particualr product code through the product selected to be used and its quantity is greater that 3 then add those records to a list and then for that list update the master Quote record to have a checkbox checked true which will then require an approval to be done.  

Looks like through my debug statements I can see that the records and lists are being treated fine up until line 45 as the master quote record does not get updated.  

Any help on this would be greatly appreciated, and if you need any mor einformation, please ask,

Thanks again,

Shawn

Trigger Code:
 
trigger ColocationCrossConnectApproval on Apttus_Config2__ProductConfiguration__c (before insert, before update) {

    List<Apttus_Config2__LineItem__c> colocationList = new List<Apttus_Config2__LineItem__c>();
    	system.debug('colocationList Size Initial -'+colocationList.size());
    List<Apttus_Config2__LineItem__c> crossConnectList = new List<Apttus_Config2__LineItem__c>();
    	system.debug('crossConnectList Size Initial -'+crossConnectList.size());
    List<Apttus_Config2__ProductConfiguration__c> configRecords = new List<Apttus_Config2__ProductConfiguration__c>();
    	system.debug('configRecords Size Initial -'+configRecords.size());
    
    For(Apttus_Config2__ProductConfiguration__c config : Trigger.new){
        
        If(config.Apttus_Config2__Status__c == 'Finalized'){
                configRecords.add(config);
            	system.debug('configRecords Size After -'+configRecords.size());
        } // End of Status = Finalized If Statement
     } // End of Trigger.New For Loop
    
    If(configRecords.size()>0){
  
    	For(Apttus_Config2__ProductConfiguration__c configToWork : configRecords){
        
            List<Apttus_Config2__LineItem__c> colocationItems = [SELECT ID, Name, Apttus_Config2__OptionId__c,Apttus_Config2__OptionId__r.ProductCode,
                                                                 Apttus_Config2__ConfigurationId__c,Apttus_Config2__Quantity__c,
                                                                 Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c,
                                                                 Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c 
                                                                 FROM Apttus_Config2__LineItem__c 
                                                                 WHERE Apttus_Config2__ConfigurationId__c IN : configRecords];
            system.debug('colocationItems Size -' + colocationItems.size());
            
            For(Apttus_Config2__LineItem__c coLoItems : colocationItems){
                
                If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-001' && coLoItems.Apttus_Config2__Quantity__c > 3){
                    colocationList.add(coLoItems);
                    	system.debug('colocationList Size After -'+colocationList.size());
                } // End Of Colocation If Statement To Add CoLocation Line Items to colocationList
                
                else If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-002' && coLoItems.Apttus_Config2__Quantity__c > 3) {
                    crossConnectList.add(coLoItems);
                    	system.debug('crossConnectList Size After -'+crossConnectList.size());
                } // End of Else If Statement to add Cross Connect Lines to crossConnectList   
            } // End of Line For Loop   
    	} // End of configToWork For Loop
    } // End Of Bulkify If Statement
    
    If(colocationList.size()>0){
        For(Apttus_Config2__LineItem__c finalcoLoList : colocationList){
            finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c = True;
            Database.update(finalcoLoList);
        } // End of finalcoLoList For Loop
    } // End of colocationList size If Statement
    
    If(crossConnectList.size()>0){
        For(Apttus_Config2__LineItem__c finalcrossList : crossConnectList){
            finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c = True;
            Database.update(finalcrossList);
        } // End of finalcrossList For Loop
    } // End of crossconnectList size If Statement    
} // End Of Trigger

 
Best Answer chosen by Shawn Reichner 29
Damon ShawDamon Shaw
Hi Shawn,

You are not able to update related records like this, Database.update(finalcoLoList); will try to update the Apttus_Config2__LineItem__c record, not the Apttus_QPConfig__Proposald__c record.

You will need to get a list of the Apttus_QPConfig__Proposald__c records to update the fields on those.

Unrelated but still an issue, on line 22 you are doing a select inside a loop which you should always try to avoid, if you ever have more that 100 records in the trigger you will start to hit limits, try your best to avaoid these.

From what I can see though, the select will always get the same list of records each time so this loop is not needed.

here's my attempt at your trigger if I understand what you're tying to update
trigger ColocationCrossConnectApproval on Apttus_Config2__ProductConfiguration__c (before insert, before update) {

    List<Apttus_Config2__LineItem__c> colocationList = new List<Apttus_Config2__LineItem__c>();
    List<Apttus_Config2__LineItem__c> crossConnectList = new List<Apttus_Config2__LineItem__c>();

    // a map of proposals so you can update specific ones
    Map<Id, Apttus_QPConfig__Proposald__c> proposals = new Map<Id, Apttus_QPConfig__Proposald__c>();
    // a set of proposal Ids so you can select the right records
    Set<Id> proposalIds = new Set<Id>();

    List<Apttus_Config2__ProductConfiguration__c> configRecords = new List<Apttus_Config2__ProductConfiguration__c>();    
    for(Apttus_Config2__ProductConfiguration__c config : Trigger.new){
        
        if(config.Apttus_Config2__Status__c == 'Finalized'){
            configRecords.add(config);
        }
     }
    
    if(configRecords.size()>0){

        List<Apttus_Config2__LineItem__c> colocationItems = [SELECT ID, Name, Apttus_Config2__OptionId__c,Apttus_Config2__OptionId__r.ProductCode,
                                                             Apttus_Config2__ConfigurationId__c,Apttus_Config2__Quantity__c,
                                                             Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c,
                                                             Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c 
                                                             FROM Apttus_Config2__LineItem__c 
                                                             WHERE Apttus_Config2__ConfigurationId__c IN : configRecords];

        for(Apttus_Config2__LineItem__c coLoItems : colocationItems){           
            
            if(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-001' && coLoItems.Apttus_Config2__Quantity__c > 3){
                colocationList.add(coLoItems);
                proposalIds.add(coLoItems.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)
            }
            
            else If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-002' && coLoItems.Apttus_Config2__Quantity__c > 3) {
                crossConnectList.add(coLoItems);
                proposalIds.add(coLoItems.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)
            }
        }
    }
    
    //look up the proposal records and put them into the may using their Id
    if(proposalIds.size() > 0){
        for(Apttus_QPConfig__Proposald__c p :[SELECT Id, ColocationApprovalRequired__c, CrossConnectApprovalRequired__c FROM Apttus_QPConfig__Proposald__c WHERE Id IN :proposalIds]){
            proposals.put(p.Id, p);
        }
    }

    if(colocationList.size()>0){
        for(Apttus_Config2__LineItem__c finalcoLoList : colocationList){
            //if the map contains this propsal update it's ColocationApprovalRequired__c field
            if(proposals.containsKey(finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)){
                proposals.get(finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c).ColocationApprovalRequired__c = True;
            }
            
        }
    }
    
    if(crossConnectList.size()>0){
        for(Apttus_Config2__LineItem__c finalcrossList : crossConnectList){
            //if the map contains this propsal update it's CrossConnectApprovalRequired__c field
            if(proposals.containsKey(finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)){
                proposals.get(finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c).CrossConnectApprovalRequired__c = True;
            }
        }
    }

    if(proposals.size() > 0){
        // update all of the selected proposals
        update proposals.values();
    }  
}

All Answers

Damon ShawDamon Shaw
Hi Shawn,

You are not able to update related records like this, Database.update(finalcoLoList); will try to update the Apttus_Config2__LineItem__c record, not the Apttus_QPConfig__Proposald__c record.

You will need to get a list of the Apttus_QPConfig__Proposald__c records to update the fields on those.

Unrelated but still an issue, on line 22 you are doing a select inside a loop which you should always try to avoid, if you ever have more that 100 records in the trigger you will start to hit limits, try your best to avaoid these.

From what I can see though, the select will always get the same list of records each time so this loop is not needed.

here's my attempt at your trigger if I understand what you're tying to update
trigger ColocationCrossConnectApproval on Apttus_Config2__ProductConfiguration__c (before insert, before update) {

    List<Apttus_Config2__LineItem__c> colocationList = new List<Apttus_Config2__LineItem__c>();
    List<Apttus_Config2__LineItem__c> crossConnectList = new List<Apttus_Config2__LineItem__c>();

    // a map of proposals so you can update specific ones
    Map<Id, Apttus_QPConfig__Proposald__c> proposals = new Map<Id, Apttus_QPConfig__Proposald__c>();
    // a set of proposal Ids so you can select the right records
    Set<Id> proposalIds = new Set<Id>();

    List<Apttus_Config2__ProductConfiguration__c> configRecords = new List<Apttus_Config2__ProductConfiguration__c>();    
    for(Apttus_Config2__ProductConfiguration__c config : Trigger.new){
        
        if(config.Apttus_Config2__Status__c == 'Finalized'){
            configRecords.add(config);
        }
     }
    
    if(configRecords.size()>0){

        List<Apttus_Config2__LineItem__c> colocationItems = [SELECT ID, Name, Apttus_Config2__OptionId__c,Apttus_Config2__OptionId__r.ProductCode,
                                                             Apttus_Config2__ConfigurationId__c,Apttus_Config2__Quantity__c,
                                                             Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c,
                                                             Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c 
                                                             FROM Apttus_Config2__LineItem__c 
                                                             WHERE Apttus_Config2__ConfigurationId__c IN : configRecords];

        for(Apttus_Config2__LineItem__c coLoItems : colocationItems){           
            
            if(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-001' && coLoItems.Apttus_Config2__Quantity__c > 3){
                colocationList.add(coLoItems);
                proposalIds.add(coLoItems.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)
            }
            
            else If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-002' && coLoItems.Apttus_Config2__Quantity__c > 3) {
                crossConnectList.add(coLoItems);
                proposalIds.add(coLoItems.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)
            }
        }
    }
    
    //look up the proposal records and put them into the may using their Id
    if(proposalIds.size() > 0){
        for(Apttus_QPConfig__Proposald__c p :[SELECT Id, ColocationApprovalRequired__c, CrossConnectApprovalRequired__c FROM Apttus_QPConfig__Proposald__c WHERE Id IN :proposalIds]){
            proposals.put(p.Id, p);
        }
    }

    if(colocationList.size()>0){
        for(Apttus_Config2__LineItem__c finalcoLoList : colocationList){
            //if the map contains this propsal update it's ColocationApprovalRequired__c field
            if(proposals.containsKey(finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)){
                proposals.get(finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c).ColocationApprovalRequired__c = True;
            }
            
        }
    }
    
    if(crossConnectList.size()>0){
        for(Apttus_Config2__LineItem__c finalcrossList : crossConnectList){
            //if the map contains this propsal update it's CrossConnectApprovalRequired__c field
            if(proposals.containsKey(finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c)){
                proposals.get(finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c).CrossConnectApprovalRequired__c = True;
            }
        }
    }

    if(proposals.size() > 0){
        // update all of the selected proposals
        update proposals.values();
    }  
}
This was selected as the best answer
Shawn Reichner 29Shawn Reichner 29
Damon, Thank you very much for your explanation and code example.  I placed the code and after some sytax issues which I corrected, this worked like a charm.  Thanks again!