Skip to main content
added 17 characters in body
Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

Currently I have trigger which add line items to the credit request. Each line items has a qty field which is multipledmultiplied by QTY in the credit request object. This

This is the multiplier field. This works as long as QTY has a value. If I attempt to create a credit request with QTY 0, line are added to the request when they shouldn't be. I

I believe this is due to using a custom setting definition. The customer setting definition has productid, qty, and multiplier field. Not all products need a value in the multiplier field since their qty is the always the same. These are the products that create credit lines even when the high level QTY is 0. Need

Need help to address the logic in the following code to prevent these from being created when QTY is 0. QTY field is used in the multiplier field for some products.

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));
    }
   delete toDelete;
   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;
}

// Helper methods
private Boolean QuantityChanged(Credit_Request__c original, Credit_Request__c modified){
    return (original.RS_cell_qty__c != modified.RS_Cell_Qty__c) || 
        (original.SEQUEL_cell_qty__c != modified.Sequel_cell_qty__c);
} 
}

}

Currently I have trigger which add line items to the credit request. Each line items has a qty field which is multipled by QTY in the credit request object. This is the multiplier field. This works as long as QTY has a value. If I attempt to create a credit request with QTY 0, line are added to the request when they shouldn't be. I believe this is due to using a custom setting definition. The customer setting definition has productid, qty, and multiplier field. Not all products need a value in the multiplier field since their qty is the always the same. These are the products that create credit lines even when the high level QTY is 0. Need help to address the logic in the following code to prevent these from being created when QTY is 0. QTY field is used in the multiplier field for some products.

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));
    }
   delete toDelete;
   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;
}

// Helper methods
private Boolean QuantityChanged(Credit_Request__c original, Credit_Request__c modified){
    return (original.RS_cell_qty__c != modified.RS_Cell_Qty__c) || 
        (original.SEQUEL_cell_qty__c != modified.Sequel_cell_qty__c);
} 

}

Currently I have trigger which add line items to the credit request. Each line items has a qty field which is multiplied by QTY in the credit request object.

This is the multiplier field. This works as long as QTY has a value. If I attempt to create a credit request with QTY 0, line are added to the request when they shouldn't be.

I believe this is due to using a custom setting definition. The customer setting definition has productid, qty, and multiplier field. Not all products need a value in the multiplier field since their qty is the always the same. These are the products that create credit lines even when the high level QTY is 0.

Need help to address the logic in the following code to prevent these from being created when QTY is 0. QTY field is used in the multiplier field for some products.

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));
    }
   delete toDelete;
   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;
}

// Helper methods
private Boolean QuantityChanged(Credit_Request__c original, Credit_Request__c modified){
    return (original.RS_cell_qty__c != modified.RS_Cell_Qty__c) || 
        (original.SEQUEL_cell_qty__c != modified.Sequel_cell_qty__c);
} 
}
Source Link
Chris
  • 33
  • 7

Current trigger and class is creating line items when it is not expected

Currently I have trigger which add line items to the credit request. Each line items has a qty field which is multipled by QTY in the credit request object. This is the multiplier field. This works as long as QTY has a value. If I attempt to create a credit request with QTY 0, line are added to the request when they shouldn't be. I believe this is due to using a custom setting definition. The customer setting definition has productid, qty, and multiplier field. Not all products need a value in the multiplier field since their qty is the always the same. These are the products that create credit lines even when the high level QTY is 0. Need help to address the logic in the following code to prevent these from being created when QTY is 0. QTY field is used in the multiplier field for some products.

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));
    }
   delete toDelete;
   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;
}

// Helper methods
private Boolean QuantityChanged(Credit_Request__c original, Credit_Request__c modified){
    return (original.RS_cell_qty__c != modified.RS_Cell_Qty__c) || 
        (original.SEQUEL_cell_qty__c != modified.Sequel_cell_qty__c);
} 

}