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
David JoshuaDavid Joshua 

(simple ?) range problem. error: Variable does not exist

Hello, 

I have a range problem with one of my variables : 
String MCLU1 = 'BK2200 0015 -- 41';

        if (MCLU1 != null || MCLU1 != '') {
            String bk = null; 
            String bk_full = null;
            String bk_extension = '--'; 
            String [] bkAfterSplit = null;
    
            bk_full = MCLU1; 
            if (bk_full.contains(bk_extension)) {
                bkAfterSplit = bk_full.split(' -- ');
                bk = bkAfterSplit[0];
                System.debug(LoggingLevel.DEBUG, 'bk inside my if : ' + bk);
            } else {
                bk = bk_full;
                System.debug(LoggingLevel.DEBUG, 'bk inside my else : ' + bk);
            }
            
        } else {
            System.debug(LoggingLevel.DEBUG, 'MCLU1__c is empty');
        }
        String external_id = 'FRA-BLIP-' + bk;
        System.debug(LoggingLevel.DEBUG, 'bk outside my if/else : ' + bk);
        System.debug(LoggingLevel.DEBUG, 'external_id : ' + external_id);
line 22, I need to get my bk variable but i get a Variable does not exist: bk. And I don't seem to understand why. I try almost the same code in eclipse and it worked : 
public class BkDoesNotExist {

	public static void main(String[] args) {
		
		String MCLU1__c = "BK2200 0015 -- 41";
		
		String bk = null; 
		String bk_full = null;
		String bk_extension = "--"; 
		String [] bkAfterSplit = null;
		
        if (MCLU1__c != null || MCLU1__c != "") {
            
        // variables nécessaires pour split mon BK proprement 

            bk_full = MCLU1__c; 
            if (bk_full.contains(bk_extension)) {
                bkAfterSplit = bk_full.split(" -- ");
                bk = bkAfterSplit[0];
                System.out.println("bk inside my if : " + bk);
            } else {
                bk = bk_full;
            }
            
        } else {
            System.out.println("MCLU1__c is empty");
        }

        String external_id = "FRA-BLIP-"+bk; 
        System.out.println("bk outside my if : " + bk);
        System.out.println("external_id : " + external_id);
	}

}
result in console : 
bk inside my if : BK2200 0015
bk outside my if : BK2200 0015
external_id : FRA-BLIP-BK2200 0015
Any idea on what I am missing here ? 

 

 
Ashish Arun WaghmareAshish Arun Waghmare
Hi  David ,

If you could declare the variable String bk = null;  outside the if statement below String MCLU1 = 'BK2200 0015 -- 41';

for example your code would look something like below

String MCLU1 = 'BK2200 0015 -- 41';
String bk = null; 
String bk_full = null;
String bk_extension = '--'; 
String [] bkAfterSplit = null;
        if (MCLU1 != null || MCLU1 != '') {
            bk_full = MCLU1; 
            if (bk_full.contains(bk_extension)) {
                bkAfterSplit = bk_full.split(' -- ');
                bk = bkAfterSplit[0];
                System.debug(LoggingLevel.DEBUG, 'bk inside my if : ' + bk);
            } else {
                bk = bk_full;
                System.debug(LoggingLevel.DEBUG, 'bk inside my else : ' + bk);
            }
            
        } else {
            System.debug(LoggingLevel.DEBUG, 'MCLU1__c is empty');
        }
        String external_id = 'FRA-BLIP-' + bk;
        System.debug(LoggingLevel.DEBUG, 'bk outside my if/else : ' + bk);
        System.debug(LoggingLevel.DEBUG, 'external_id : ' + external_id)


This should work..

Hope this helps !!!
David JoshuaDavid Joshua
Damn, you're right. I found it 2 minutes ago too... after losing more then 1h on this newbee mistake ! 
Thank you !