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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Why am I getting this error message? Compile Error: Variable does not exist: BTLog at line 16 column 33

Hello,

I'm new to Salesforce Apex. I keep getting this error message when I try save my code eventhough I've declared the variable already..

User-added image

Compile Error: Variable does not exist: BTLog at line 16 column 33    ​
Trigger AutocreateBTWeightRecordsAndMatchitToAccount on bodytrace__c (after insert) {
    for (BodyTrace__c BTLog : Trigger.new) {
    
    Bodytrace_weight__c BTWeight = new Bodytrace_Weight__c();
 
    BTWeight.IMEI__c             =  BTLog.IMEI__c;
    BTWeight.Weight__c           =  BTLog.Values_weight__c;
    BTWeight.Date_of_reading__c  =  BTLog.CreatedDate;
    BTWeight.bodytrace_log__c    =  BTLog.ID;
         
    Insert BTWeight;
      
    }
    
        For (Account Accnt : Trigger.new) {
            if(accnt.IMEI__c == BTLog.IMEI__c){
            BTWeight.Account__c = account.ID;
            }
        }
}
Background on what I'm trying to do:

I'm trying to build a trigger that create a weight reading records (Bodytrace_Weight__c) based of weight logs records (Bodytrace_Weight__c). After that I'd like to auto-assign (Bodytrace_Weight__c) to the patient who got weighted based on the serial number that is found on both; account object and Bodytrace_Weight__c object.

Please let me know what's the best way to build this. Thanks!
Best Answer chosen by Mahmoud Coudsi 1
Suraj PSuraj P
Sorry. Change your code to below:
 
Trigger AutocreateBTWeightRecordsAndMatchitToAccount on bodytrace__c (after insert) {
   Bodytrace_Weight__c[] btWeightList = new Bodytrace_Weight__c[]{};
    for (BodyTrace__c BTLog : Trigger.new) {
    
    Bodytrace_weight__c BTWeight = new Bodytrace_Weight__c();
 
    BTWeight.IMEI__c             =  BTLog.IMEI__c;
    BTWeight.Weight__c           =  BTLog.Values_weight__c;
    BTWeight.Date_of_reading__c  =  BTLog.CreatedDate;
    BTWeight.bodytrace_log__c    =  BTLog.ID;
    BTWeight.Account__r = new Account(IMEI__c=BTLog.IMEI__c);     
    btWeightList.add(BTWeight);
      
    }
    
insert btWeightList;
      
}

 

All Answers

Suraj PSuraj P
BTLog variable is outside the first for loop, hence it no longer exists. Also, your code can soon run into governor limit issues, since your inserts are not outside the for loops. Also, since this is a trigger on BodyTrace__c, Trigger.New will never give you an Account record, as you have attempted to do in your code.

If IMEI__c is not already an external id on Account, make it so. And then use the following code:

 
Trigger AutocreateBTWeightRecordsAndMatchitToAccount on bodytrace__c (after insert) {
   Bodytrace_Weight__c[] btWeightList = new Bodytrace_Weight__c[]{};
    for (BodyTrace__c BTLog : Trigger.new) {
    
    Bodytrace_weight__c BTWeight = new Bodytrace_Weight__c();
 
    BTWeight.IMEI__c             =  BTLog.IMEI__c;
    BTWeight.Weight__c           =  BTLog.Values_weight__c;
    BTWeight.Date_of_reading__c  =  BTLog.CreatedDate;
    BTWeight.bodytrace_log__c    =  BTLog.ID;
    BTWeight.Account__c = new Account(IMEI__c=BTLog.IMEI__c);     
    btWeightList.add(BTWeight);
      
    }
    
insert btWeightList;
      
}
If this helps, please mark it as the best answer
 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Surj P,

Thanks for taking the time to review the code and add on it. I tried to the code you wrote above but I got back this error message:
Error: Compile Error: Illegal assignment from Account to Id at line 11 column 5
 
Trigger AutocreateBTWeightRecordsAndMatchitToAccount on bodytrace__c (after insert) {
   Bodytrace_Weight__c[] btWeightList = new Bodytrace_Weight__c[]{};
    for (BodyTrace__c BTLog : Trigger.new) {
    
    Bodytrace_weight__c BTWeight = new Bodytrace_Weight__c();
 
    BTWeight.IMEI__c             =  BTLog.IMEI__c;
    BTWeight.Weight__c           =  BTLog.Values_weight__c;
    BTWeight.Date_of_reading__c  =  BTLog.CreatedDate;
    BTWeight.bodytrace_log__c    =  BTLog.ID;
    BTWeight.Account__c = new Account(IMEI__c=BTLog.IMEI__c);     
    btWeightList.add(BTWeight);
      
    }
    
insert btWeightList;
      
}

User-added image

 
Suraj PSuraj P
Sorry. Change your code to below:
 
Trigger AutocreateBTWeightRecordsAndMatchitToAccount on bodytrace__c (after insert) {
   Bodytrace_Weight__c[] btWeightList = new Bodytrace_Weight__c[]{};
    for (BodyTrace__c BTLog : Trigger.new) {
    
    Bodytrace_weight__c BTWeight = new Bodytrace_Weight__c();
 
    BTWeight.IMEI__c             =  BTLog.IMEI__c;
    BTWeight.Weight__c           =  BTLog.Values_weight__c;
    BTWeight.Date_of_reading__c  =  BTLog.CreatedDate;
    BTWeight.bodytrace_log__c    =  BTLog.ID;
    BTWeight.Account__r = new Account(IMEI__c=BTLog.IMEI__c);     
    btWeightList.add(BTWeight);
      
    }
    
insert btWeightList;
      
}

 
This was selected as the best answer
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Suraj,

Thanks for the quick turnaround. I was finally able to save the code, when I tried to create a test Bodytrace log record, I got this error message: 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AutocreateBTWeightRecordsAndMatchitToAccount caused an unexpected exception, contact your administrator: AutocreateBTWeightRecordsAndMatchitToAccount: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, Field name provided, IMEI__c is not an External ID or indexed field for Account: []: Trigger.AutocreateBTWeightRecordsAndMatchitToAccount: line 16, column 1

How can fix this issue?
Mahmoud Coudsi 1Mahmoud Coudsi 1
I figured it out. The IMEI_c field on the account object was a number type but wasn't an external ID, so  just had to recreate it and check the external ID checkbox. 

Thanks for your help!