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
Kumar DavidKumar David 

Trigger not working- Variable does not exist

I have trigger which is working but I need to update it and it is giving me an error ( only bold one is newly added)

I had two fields and based on the combination it was working but now I need to add another checbox called Active__c and if current condition and if Active__c is true then only trigger the trigger. It is giving me "Variable does not exist" error on Line 28

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null) {
               accountsIds.add(item.Customerforlogiconly__c);
               System.debug(accountsIds);
            }
            if(item.Disposal_Location_on_Ticket_item__c!= null) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
                 System.debug(wellsIds);
            }
        }
        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c, Active__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c,di);
            }
            System.debug (di.direct_Bill_Customer__c);
            System.debug (di.Disposal_Location__c);
            System.debug (di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c);          
        }
        for(FX5__Ticket_Item__c item:items)       
         {
         System.debug (item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c);
           if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && di.Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }
            else {
                item.Direct_Bill_Customer__c = False;
            }
                
        }
        
    }
}
 
Best Answer chosen by Kumar David
Vladimir SaturaVladimir Satura
my bad:

if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && directInvoicesMap.get(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c).Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }

All Answers

Vladimir SaturaVladimir Satura
Variable di exists only within for loop in lines 17-24.

In line 28 your Obj_Direct_Bill_Customer__c record is no longer in variable di, but in your map directInvoicesMap:

... directInvoicesMap.get(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c).Active__c == true ...
Kumar DavidKumar David
Hi Vladimir,
I wanted to fire the trigger and update  " item.Direct_Bill_Customer__c = True;" only if both the condition matches
  • if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c)
  • Active__c on Obj_direct_bill_customer__c is True
           If condition does not match, do nothing. So technically I don't need else condition as well.

Thank you
Vladimir SaturaVladimir Satura
yes, you don't need else condition:

if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c).Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }
Kumar DavidKumar David
Hi Vladimir
It gave me this error message "Variable does not exist: Active__c" on line 28.
Thank  you
 
Vladimir SaturaVladimir Satura
my bad:

if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && directInvoicesMap.get(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c).Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }
This was selected as the best answer
Kumar DavidKumar David
Thank you Vladimir,

Its working now as intended. Thank you