1

Currently I have trigger and class that add credit lines to a credit request object. This trigger is for after insert and after update. After an insert 8 credit lines are added to the credit request, which is expected. After an update and additional 8 credit lines are added thus totalling 16 credit lines. The intent of the code was to delete the prior 8 credit lines and add new credit lines after an update. Unfortunately the deletion of the prior credit lines is not occurring.

public class AddOrUpdateCreditLineTriggerHandler {

Map<Id, List<Credit_lines__c>> mapCRToCL = new Map<id, List<credit_lines__c>>();
map<id, Credit_Request__c> original; 
map<id,Credit_Request__c> modified;
List<Credit_Lines__c> associatedCreditLines;
List<Credit_lines__c> toDelete = new List<Credit_Lines__c>();
List<Credit_lines__c> toInsert = new List<Credit_lines__c>();

public AddOrUpdateCreditLineTriggerHandler(List<Credit_Request__c> original, List<Credit_Request__c> modified){
    if(original != null) {
        this.original = New Map<Id, Credit_Request__C>(original);
    }
    this.modified = New Map<Id, Credit_Request__c>(modified);
    associatedCreditLines = 
        [SELECT id, Product__C, Name, credit_request__c 
        FROM Credit_Lines__C 
        WHERE Credit_request__c in :modified ];
    for(Credit_lines__c cl: associatedCreditLines) {
        if(mapCRToCL.containsKey(cl.credit_request__c)){
            mapCRToCL.get(cl.credit_request__c).add(cl);
        } else {
            mapCRToCL.put(cl.credit_request__c, new List<Credit_Lines__c>{cl}); 
        }
    }
}

public void run(){ 
    if(Trigger.isInsert){
        afterInsert();
    } else if (Trigger.isUpdate){
        afterUpdate();
    }
}

private void afterInsert(){
    for(Credit_Request__c cr : this.modified.values() ){
        toInsert.addAll(createStandardCreditLines(cr));
    }
    insert toInsert;
}

private void afterUpdate(){
    for(Credit_Request__c cr : filterQtyChanged(this.modified.values())){
        toDelete.addAll(mapCRToCl.get(cr.id));
        toInsert.addAll(createStandardCreditLines(cr));
    }
   insert toInsert;
}

private List<Credit_lines__c> createStandardCreditLines(Credit_Request__c cr){
    List<Credit_Lines__c> created = new List<Credit_Lines__c>();
    for(AutomatedCreditLineRecords__c a: creditLines()){
        if(a.multiplierField__c != null && cr.get(a.multiplierField__c) == null){
            continue;
        }
        Double multiplier = 1;
        if(a.multiplierField__c != null && cr.get(a.multiplierField__c) != null){
            multiplier = (Double) cr.get(a.multiplierField__c);
        }
        created.add(
            New Credit_lines__c(
                Credit_Request__c = cr.Id,
                Quantity__c = (a.Qty__c * multiplier),
                Product__c = a.productId__c
            )
        );
    }
    return created;
}

private List<AutomatedCreditLineRecords__c> CreditLines(){
    return AutomatedCreditLineRecords__c.getall().values();
}   

private List<Credit_Request__c> filterQtyChanged(List<Credit_Request__c> possible){
    List<Credit_request__c> toUpdate = new List<Credit_Request__c>();
    for(Credit_Request__c cr : this.modified.values()){ 
        if(QuantityChanged(this.original.get(cr.id), cr)){
            toUpdate.add(cr);
        }
    }
    return toUpdate;
0
0

Look at you're code, it's not exactly symmetric:

private void afterUpdate(){
    for(Credit_Request__c cr : filterQtyChanged(this.modified.values())){
        toDelete.addAll(mapCRToCl.get(cr.id));
        toInsert.addAll(createStandardCreditLines(cr));
    }
   insert toInsert;
}

You add records to your toInsert collection, then call insert toInsert. You add records to your toDelete collection...but never call delete toDelete.

7
  • Thanks for the assistance that did it, just vetting it and going cross eyed. Jun 23 '17 at 20:29
  • are you on codementor Jun 23 '17 at 20:38
  • @ChrisSecrist Nope don't even know what that is.
    – Adrian Larson
    Jun 23 '17 at 20:38
  • Website where individuals like me with no experience can get live help for a certain price. (codementor.io) Like 15 dollars for 20 minutes or so for live session. You know apex extremely well and I may have more questions in future. Right now still vetting this class and trigger Jun 23 '17 at 20:42
  • Interesting... I wonder if there's much demand for help with Apex.
    – Adrian Larson
    Jun 23 '17 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.