Ask Search:
Stephen CraneStephen Crane 

Reference A Field In a Record.put APEX code

We just got Zuora, and I'm trying to update the Quote name to include today's date. I was told I could do that with extending the class - but I'm having trouble with it. Current code is below, but basically, how do I get the Quote Name to be = "Quote For OpportunityName__c Date"...I have the "quote for" and the "date" part down, but anytime I try referencing the field Opportunity Name it either gives me an error or just spits out the API variable name. Full code below.

global class DefaultValues 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  
      
      record.put('Name', 'Quote For' + ' ' + zqu__Quote__c.Opportunity_Name__c + ' ' + 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');
    

      // 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 Stephen Crane
Stephen CraneStephen Crane
Zuora helped out with the following code in case anyone stumbles across this
Id OpptId = (Id) record.get('zqu__Opportunity__c');    
      List<Opportunity> Opportunity= [SELECT Id, Name FROM Opportunity WHERE Opportunity.Id = :OpptId ]; 

All Answers

Usman AliUsman Ali
Hi Stephen,

How many characters are being generated by this expression? 'Quote For' + ' ' + zqu__Quote__c.Opportunity_Name__c + ' ' + Date.today().format())
if these are more than 80 then Quote name will split, because Name field can hold up to 80 characters. 
Usman AliUsman Ali
One thing more I think you should use zqu__Quote__r.Opportunity_Name__c instead of zqu__Quote__r.Opportunity_Name__c to get parent Opportunity name. 
 
Stephen CraneStephen Crane
The Opportunity name has to be no more than 60 characters with the other things in there, but I don't think that's it. For instance when I try to create a new quote the name I get is "Quote For Opportunity_Name__c 11/20/2019"
Stephen CraneStephen Crane
if I change it to 'zqu__Quote__r.Opportunity_Name__c then I get an error: "Compile Error: Variable does not exist: zqu__Quote__r at line 9 column 46" 
Stephen CraneStephen Crane
Zuora helped out with the following code in case anyone stumbles across this
Id OpptId = (Id) record.get('zqu__Opportunity__c');    
      List<Opportunity> Opportunity= [SELECT Id, Name FROM Opportunity WHERE Opportunity.Id = :OpptId ]; 
This was selected as the best answer