+ Start a Discussion
StephenCraneStephenCrane 

Pulling Account Fields Into An Apex Class

Hey there, I'm new to development and APEX classes, so apologies if this is easy. But basically we have Zuora package (for billing) and I'm updating one of the classes for default values when creating a quote. Most of the code works, except when I add in a reference to Billing Country on the Account it says it doesn't exist. My code for that particular part is below, and full code below that.

Error: 
Error: Compile Error: Variable does not exist: billingcountry at line 23 column 20
// Retrieve the account ID from the quote
      Id accountId = (Id) record.get('zqu__Account__c');
      
      List<Account>accounts = [SELECT Id, BillingCountry FROM Account WHERE Account.ID = :accountID];
      
      if (accounts.billingcountry == 'Canada')
      {
      record.put('zqu__PaymentGateway__c', 'Standard');
      } else {
      record.put('zqu__PaymentGateway__c', 'USD2');
      }
Full code:
global class ZuoraDefaultValues extends zqu.CreateQuoteController.PopulateDefaultFieldValuePlugin{  
   global override void populateDefaultFieldValue
(SObject record, zqu.PropertyComponentController.ParentController pcc)
   {    
      super.populateDefaultFieldValue(record, pcc);  
      
      //Populate default values in the quote header  
      Id OpptId = (Id) record.get('zqu__Opportunity__c');    
      List<Opportunity> Opportunity= [SELECT Id, Name FROM Opportunity WHERE Opportunity.Id = :OpptId ]; 
       
      record.put('Name', 'Quote For ' + Opportunity[0].Name + ' ' + Date.today().format());
      record.put('zqu__InitialTerm__c', 12);    
      record.put('zqu__RenewalTerm__c', 12);    
      record.put('zqu__ValidUntil__c', Date.today().addDays(30));    
      record.put('zqu__StartDate__c', Date.today());
      record.put('zqu__PaymentMethod__c', 'Credit Card');  
      
      // Retrieve the account ID from the quote
      Id accountId = (Id) record.get('zqu__Account__c');
      
      List<Account>accounts = [SELECT Id, BillingCountry FROM Account WHERE Account.ID = :accountID];
      
      if (accounts.billingcountry == 'Canada')
      {
      record.put('zqu__PaymentGateway__c', 'Standard');
      } else {
      record.put('zqu__PaymentGateway__c', 'USD2');
      }
    

      // Find the contacts associated with the account         
      List<Contact>contacts = [SELECT Id, Name FROM Contact WHERE Account.Id = :accountId];
      
      // Assuming the contacts are present set the billTo and soldTo to the first contact
      if  (contacts.size() > 0) {
         // System.debug('mp: about to add ' + contacts[0].Id + ' as a contact ID');
         record.put('zqu__BillToContact__c', contacts[0].Id);
         record.put('zqu__SoldToContact__c', contacts[0].Id);

         // Beforeretrieving  the lookup  options, needs to populate the map first
         super.setLookupOptions(pcc);
      
         // Now retrieve the lookup component options
         zqu.LookupComponentOptions billToOptions = super.getLookupOption('zqu__BillToContact__c');
         billToOptions.targetId = contacts[0].Id;
         billToOptions.targetName = contacts[0].Name;
         zqu.LookupComponentOptions soldToOptions  = super.getLookupOption('zqu__SoldToContact__c');
         soldToOptions.targetId = contacts[0].Id;
         soldToOptions.targetName = contacts[0].Name;
      }
   }
}

 
Best Answer chosen by StephenCrane
Dushyant SonwarDushyant Sonwar
HI Stephen,

You can try with this code. 
List<Account>accounts = [SELECT Id, BillingCountry FROM Account WHERE AccountID = :accountId];
if(accounts.size() > 0){
      if (accounts[0].billingcountry == 'Canada')
      {
           record.put('zqu__PaymentGateway__c', 'Standard');
      }
      else{
           record.put('zqu__PaymentGateway__c', 'USD2');
      }
}

I think it can be done without for loop as i see he is using the account id and there will be only one record fetch will get from database.


Hope this helps!
 

All Answers

Anthony McDougaldAnthony McDougald
Good Afternoon StephenCrane,
Hope that your day is off to an amazing start. You're getting that error because you are referencing a field in a list. In order to check the said condition that you're trying to check, you will need to iterate through the list and check for the said condition. The code edit is below, please test and report back at your earliest possible convenience. Hope this helps and may God bless you abundantly.
// Retrieve the account ID from the quote
      Id accountId = (Id) record.get('zqu__Account__c');
      
      List<Account>accounts = [SELECT Id, BillingCountry FROM Account WHERE Account.ID = :accountID];
 //Iterate through the list to get the record, you can name acts something else if you desire
for(Account acts : accounts){

//Check each account in accounts, abbreviated as acts, to see if the billing country is //Canada
if (acts.billingCountry == 'Canada'){
      record.put('zqu__PaymentGateway__c', 'Standard');
      } 
else {
      record.put('zqu__PaymentGateway__c', 'USD2');
      }

}//end of for loop

Best Regards,
Anthony McDougald​​​​​​​
Dushyant SonwarDushyant Sonwar
HI Stephen,

You can try with this code. 
List<Account>accounts = [SELECT Id, BillingCountry FROM Account WHERE AccountID = :accountId];
if(accounts.size() > 0){
      if (accounts[0].billingcountry == 'Canada')
      {
           record.put('zqu__PaymentGateway__c', 'Standard');
      }
      else{
           record.put('zqu__PaymentGateway__c', 'USD2');
      }
}

I think it can be done without for loop as i see he is using the account id and there will be only one record fetch will get from database.


Hope this helps!
 
This was selected as the best answer
StephenCraneStephenCrane
Thanks everyone!