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
Takunda BurutsaTakunda Burutsa 

Execute Anonymous: Variable does not exist

Execute Anonymous: Variable does not exist
Hey Guys, 
I'm stuck on trying to debug this piece of code in Anonymous Window. I set the variables using a map on the Opportunity object, but the child object "Payment__c" is not recognizing the lookup to Opportunity record maps. It's a master-detail relationship. The error message I'm seeing when I try executing is: Line: 41, Column: 30. Variable does not exist: BudBeer. Which is not making sense to me, is there something I'm not seeing? 
I'm sure it's something very silly, here's the code:
 
/* Insert Account Map */
             Map<String, Account> Accts= new Map<String, Account>{};
                Accts.put('BudweiserFac', new Account(Name='Budweiser Co'));
                Accts.put('SamAdams', new Account(Name='Sam Adams Co'));  
            insert Accts.values(); 
         
        /* Insert Contact Map */
            Map<String, Contact> Con= new Map<String, Contact>{};
                Con.put('JohnSmith', new Contact(FirstName='John',LastName='Smith',AccountId=Accts.get('SamAdams').Id));
                Con.put('TakuBurutsa', new Contact(FirstName='Taku',LastName='Burutsa',AccountId=Accts.get('BudweiserFac').Id));
            insert Con.values(); 
            
        /* Instantiate Opportunity Record Type variables */
                Id GrantId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Grant').getRecordTypeId();
                    System.debug('Grant RecTypeId = '+GrantId);

                Id GiftId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Gift').getRecordTypeId();
                    System.debug('Gift RectypeId ='+GiftId);
 
        /* Insert Opportunity Map */
  
            Map<String,Opportunity> Opptys=new Map<String,Opportunity>{};
                Opptys.put('SamBeer', new Opportunity(Name='Sam Beer $',CloseDate=date.parse('07/17/2019'),StageName='Closed',
                	Amount=100,AccountId=Accts.get('SamAdams').Id,Primary_Contact__c=Con.get('TakuBurutsa').Id,RecordTypeId=GiftId));
                Opptys.put('BudBeer', new Opportunity(Name='Bud Beer $',CloseDate=date.parse('07/17/2019'),StageName='Closed',
                    Amount=100,AccountId=Accts.get('BudweiserFac').Id,
                    Primary_Contact__c=Con.get('JohnSmith').Id,
                    RecordTypeId=GrantId)); 
            insert Opptys.values();
System.debug('Gift RectypeId ='+GiftId);
/* Insert Payment Map */
            Map<String,Payment__c> Pmts=new Map<String,Payment__c>{};
                Pmts.put('BudPmt1', new Payment__c(Paid__c=TRUE,Refund__c=FALSE,Payment_Method__c='Credit Card',
                Check_Date__c=date.parse('07/17/2019'),Cash_Received_Date__c=date.parse('07/17/2019'),Payment_Amount__c=100,
                CONTRIBUTION__C=Opptys.get('BudBeer').Id));

				Pmts.put('SamPmt1', new Payment__c(Paid__c=TRUE,Refund__c=FALSE,Payment_Method__c='Credit Card',
                Check_Date__c=date.parse('08/17/2019'),Cash_Received_Date__c=date.parse('08/17/2019'),Payment_Amount__c=100,
                CONTRIBUTION__C=Opptys.get('SamBeer').Id));
            insert Pmts.values();  
                System.debug(BudBeer);
		System.debug(SamBeer);

 
Best Answer chosen by Takunda Burutsa
Maharajan CMaharajan C
Hi,

Replace the Line No 41, 42 with below lines:

        String BudBeer = Opptys.containsKey('BudBeer') ? String.valueOf(Opptys.get('BudBeer')) : 'No Record Found.';
        String SamBeer = Opptys.containsKey('SamBeer') ? String.valueOf(Opptys.get('SamBeer')) : 'No Record Found.';
        System.debug(BudBeer);
        System.debug(SamBeer);

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

Replace the Line No 41, 42 with below lines:

        String BudBeer = Opptys.containsKey('BudBeer') ? String.valueOf(Opptys.get('BudBeer')) : 'No Record Found.';
        String SamBeer = Opptys.containsKey('SamBeer') ? String.valueOf(Opptys.get('SamBeer')) : 'No Record Found.';
        System.debug(BudBeer);
        System.debug(SamBeer);

Thanks,
Maharajan.C
This was selected as the best answer
Takunda BurutsaTakunda Burutsa
Hi Maharajan,
Thanks so much for the feedback. My class was successful despite the errors (3 hour delay). Kinda weird. I'll try out the containsKey class if it fails again. 
Much appreciated :) 
-Taku