Happy Business Starts Here

Re: PopulateDefaultFieldValuePlugin throws System.TypeException: Invalid conversion error


PopulateDefaultFieldValuePlugin throws System.TypeException: Invalid conversion error

I'm just getting started with Quote component library. My test class throws an error that I can't seem to figure out.


The test class, which throws "System.TypeException: Invalid conversion from runtime type zqu.PropertyComponentController.ParentController to zqu.CreateQuoteController" when instantiating the class. Why?


// Sample code:
// https://github.com/zuora/Z-Force/blob/master/Plugin%20SampleCode/z_DefaultValuesControllerTest.cls

global class CreateQuoteDefaultValuesTest extends zqu.CreateQuoteController.PopulateDefaultFieldValuePlugin {
    public static final String acctId = '12345678901234567890123456789012';
    public static final String subId = '23456789012345678901234567890123';

    static Final String Sales_Opportunity_Developer_Name = 'Sales_Opportunity';
    static Final RecordType Sales_Opportunity_RT = [Select Id from RecordType 
                            Where SObjectType = 'Opportunity'
                            And DeveloperName = :Sales_Opportunity_Developer_Name];

    static Account testAccount = (Account)TestFactory.createSObject(new Account(), True);
    static Contact testContact = (Contact)TestFactory.createSObject(new Contact(AccountId = testAccount.Id), True);
    static Opportunity testOpp = (Opportunity)TestFactory.createSObject(
      new Opportunity(
      , True); 

    private static void testSalesOppNoInvoiceContact() {
        zqu__Quote__c quote = createTestQuote(testAccount, testOpp, null, null, true, true);
        CreateQuoteDefaultValues controller = new CreateQuoteDefaultValues();
        PageReference createQuote = Page.zqu__CreateQuote;
        createQuote.getParameters().put('oppId', testOpp.Id);
        createQuote.getParameters().put('quoteType', 'Subscription');
        createQuote.getParameters().put('stepNumber', '2');


        // System.TypeException: Invalid conversion from runtime type zqu.PropertyComponentController.ParentController to zqu.CreateQuoteController
        controller.populateDefaultFieldValue(quote, new zqu.PropertyComponentController.ParentController());


    //Create a test Quote
    public static zqu__Quote__c createTestQuote(Account acc, Opportunity opp, Contact billTo, Contact soldTo, Boolean isNewSub, Boolean doInsert) {
        isNewSub = (isNewSub != null) ? isNewSub : true;

        zqu__Quote__c quote = new zqu__Quote__c();
        quote.Name = 'Test Quote';
        quote.zqu__SubscriptionType__c = (isNewSub) ? 'New Subscription' : 'Amend Subscription';
        quote.zqu__StartDate__c = System.today();
        quote.zqu__ValidUntil__c = System.today();
        quote.zqu__Subscription_Term_Type__c = 'Termed';
        quote.zqu__InitialTerm__c = 12;
        quote.zqu__RenewalTerm__c = 12;
        quote.zqu__Account__c = (acc != null) ? acc.Id : null;
        quote.zqu__Opportunity__c = (opp != null) ? opp.Id : null;
        quote.zqu__BillToContact__c = (billTo != null) ? billTo.Id : null;
        quote.zqu__SoldToContact__c = (soldTo != null) ? soldTo.Id : null;
        quote.zqu__ExistSubscriptionID__c = (isNewSub) ? null : subId;
        if (doInsert) {
            insert quote;
        return quote;


The plugin class:


// Sample code:
// https://github.com/zuora/Z-Force/blob/master/Plugin%20SampleCode/z_DefaultValuesController.cls

global class CreateQuoteDefaultValues 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('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', 'Check');

        // Retrieve the opp ID from the quote
        Id oppId = (Id) record.get('zqu__Opportunity__c');

        Opportunity theOpp;
        Contact billToContact;
        Contact soldToContact;
        zuora__customerAccount__c soldToAccount;
        for (Opportunity o : [Select Id, 
                              from Opportunity where Id = :oppId]) {
            theOpp = o;
            record.put('zqu__Currency__c', theOpp.CurrencyIsoCode);
            if (theOpp.Invoice_Contact__r.AccountId == theOpp.AccountId) {
                billToContact = theOpp.Invoice_Contact__r;
                soldToContact = theOpp.Invoice_Contact__r;

                if (billToContact != Null) {
                    record.put('zqu__BillToContact__c', theOpp.Invoice_Contact__r.Id);
                    record.put('zqu__SoldToContact__c', theOpp.Invoice_Contact__r.Id);

        if (theOpp.RecordType.DeveloperName == 'Channel_Opportunity') {
            for (zuora__customerAccount__c a : [Select Id, Name, Zuora__Account__c
                                                From zuora__customerAccount__c
                                                Where zuora__account__c = :theOpp.AccountId
                                                And currencyisocode = :theOpp.CurrencyIsoCode]) {
                soldToContact = theOpp.Technical_Contact__r;
                if (soldToContact != Null) {
                    record.put('zqu__SoldToContact__c', soldToContact.Id);
                soldToAccount = a;
                record.put('zqu__InvoiceOwnerName__c', soldToAccount.Id);

        // Before retrieving  the lookup  options, needs to populate the map first

        // Now retrieve the lookup component options
        if (billToContact != Null) {
            zqu.LookupComponentOptions billToOptions = super.getLookupOption('zqu__BillToContact__c');
            billToOptions.targetId = billToContact.Id;
            billToOptions.targetName = billToContact.Name;
        if (soldToContact != Null) {
            zqu.LookupComponentOptions soldToOptions  = super.getLookupOption('zqu__SoldToContact__c');
            soldToOptions.targetId = soldToContact.Id;
            soldToOptions.targetName = soldToContact.Name;
        if (soldToAccount != Null) {
            zqu.LookupComponentOptions invoiceOwnerOptions  = super.getLookupOption('zqu__InvoiceOwnerName__c');
            invoiceOwnerOptions.targetId = soldToAccount.Id;
            invoiceOwnerOptions.targetName = soldToAccount.Name;



Re: PopulateDefaultFieldValuePlugin throws System.TypeException: Invalid conversion error

In my tests I pass null for zqu.PropertyComponentController.ParentController.

In my populateDefaultFieldValue method I do not call the super.


Here is the code I use


 if(pcc != null) { // Tests and Batch jobs shall call this directly with null pcc
            // so do not call super for these cases
            super.populateDefaultFieldValue(record, pcc);
            System.debug(debugStr +'Super populate default values: ' + record);




  • Your test class should not extend the component.
  • Note that the quote will not be in the database when your method is called so don't insert it if you are trying to test as it happens in the UI.  I test with and without inserting quote since I call this method when I create quotes from Apex
  • I also do not call getLookupOption when pcc is null I will put the code below that I use

I know I spent a lot of time getting this to work.


 private void setLookupFieldOptions(SObject record,
                                       String fieldName,
                                       String fieldLabel,
                                       String value,
                                       String valueName){

     // Now retrieve the lookup component option for this field
     // Note: this field needs to be added to the record before calling this method
        if(!Test.isRunningTest()){ // Tests and Batch jobs should not call this directly with null pcc

            zqu.LookupComponentOptions options = super.getLookupOption(fieldName);
            options.objectName = fieldName;
            options.targetId = value;
            options.targetName = valueName;
            options.isRequired = true;
            options.popupWindowTitle = fieldLabel + ' Lookup';
            System.debug('### lookup options ' + options);
Maggie Longshore