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
Chris SecristChris Secrist 

Need help to complete apex class. Found some issues


Looking for some help.  New to apex and just need some help to finish this code off.  The product is located in a custom settings with qty and multiplier field.  Some fo the multiplier fields have no value.  Currently, have three issues.

1.  If multiplier field = 0, than I want the credit to be created withour credit lines.   Currently this does not happen.
2.  If there are no credit lines, and an update is made it fails because it is attempting to delete lines that do not exist.  Need the delete to optional based on if credit lines exist.

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) {
            } else {
                mapCRToCL.put(cl.credit_request__c, new List<Credit_Lines__c>{cl});
    public void run(){
        } else if (Trigger.isUpdate){
    private void afterInsert(){
        for(Credit_Request__c cr : this.modified.values() ){
        insert toInsert;
    private void afterUpdate(){
        for(Credit_Request__c cr : filterQtyChanged(this.modified.values())){
       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){
            Double multiplier = 1;
            if(a.multiplierField__c != null && cr.get(a.multiplierField__c) != null){
                multiplier = (Double) cr.get(a.multiplierField__c);
             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)){
        return toUpdate;