0

My test class inserts test quote line items which sets off a trigger, that calls a Util class to pull a query of qli fields, 5 of which have their names stored in custom settings (I'm not 100% why this is necessary, this was Quote/Opportunity line item syncing that a consultant created for us previously). The debug log clearly shows this Util class pulling the names from the custom settings properly in all cases Except for my test class. ( I tried adding a quote line item in the tool, and running other existing test classes). The result of the incomplete query is a 'SObject row was retrieved via SOQL without querying the requested field' error for the missing fields, since they are referred later in the trigger.

When I turn on seeAllData on my class, the trigger queries the fields from the custom settings. However, I don't want to do this and so I'm wondering if there's another way to 'make visible' the custom settings to my test class? Otherwise, I am unable to insert any test QuoteLineItems or OpportunityLineItems without getting the error.

Please let me know if this question makes any sense.

Here's some of the code snippets:

part of the Util class pulling the field names from the Custom Setting (QuoteLineSyncField__c),

 public class QuoteSyncUtil {

public static boolean isRunningTest = false;  

private static Map<String, String> quoteFieldMap = new Map<String, String>();
private static Map<String, String> quoteLineFieldMap = new Map<String, String>();

static {
public static String getQuoteFieldsString() {    
    String fields = '';
    for (String field : getQuoteFields()) {
        fields += ', ' + field;
    }
    return fields; 
}
    Map<String, QuoteLineSyncField__c> quoteLineFields = QuoteLineSyncField__c.getAll();
    system.debug('KENNY '+quoteLineFields);

    for (String quoteLineField : quoteLineFields.keySet()) {
        QuoteLineSyncField__c quoteLineSyncField = QuoteLineSyncField__c.getInstance(quoteLineField);
        //FIXME - uppercase value cause NPE
        quoteLineFieldMap.put(quoteLineSyncField.Name, quoteLineSyncField.OppLineSyncField__c);
        system.debug('KENNY '+QuoteLineSyncField__c.getInstance(quoteLineField));
        //quoteLineFieldMap.put(quoteLineField, quoteLineSyncField.OppLineSyncField__c);            

    if(quotelinefield == 'Product_Type__c'){
        System.debug('********* ' + quoteLineField);
        System.debug('^^^^^^^^^ ' + quotelineSyncField.OppLineSyncField__c);
        }
    }
}

public static Set<String> getQuoteLineFields() {
    system.debug('KENNY '+quoteLineFieldMap.keyset());
    return quoteLineFieldMap.keySet();
}

part of the trigger that builds the query (qliQuery is the list of field names returned by the Util class):

List<String> oppLineFields = QuoteSyncUtil.getOppLineFields();

// pricebook entry ids in trigger
Set<Id> pbEntryIds = new Set<Id>();

// a Set of Pricebook2 Ids represented in Trigger.new
Set<Id> setPricebookId = new Set<Id>();

String oliFields = QuoteSyncUtil.getOppLineFieldsString();
String qliFields =  QuoteSyncUtil.getQuoteLineFieldsString();

String qliIds = '';
String quoteIdString = '';
for (QuoteLineItem qli : qlisInTrigger) {
    if (qliIds != '') qliIds += ', ';
    qliIds += '\'' + qli.Id + '\'';

    if (quoteIdString != '') quoteIdString += ', ';
    quoteIdString += '\'' + qli.QuoteId + '\'';

    pbEntryIds.add(qli.PricebookEntryId);
}

String qliQuery = 'SELECT Id, ' +
                    'QuoteId, ' +
                     'PricebookEntryId, ' +
                     'UnitPrice, ' +
                     'Quantity, ' +
                     'Discount, ' +
                     'ServiceDate, ' +
                     'SortOrder, ' + 
                     'PricebookEntry.Product2.Name, ' +
                     'PricebookEntry.Product2.Product_Type__c, ' +
                     'PricebookEntry.Product2.Approval_Required__c, ' + 
                     'PricebookEntry.Pricebook2Id, ' +
                     'ListPrice, ' +
                     'Discount_off_List__c ' +
                     qliFields + 
                     ' FROM QuoteLineItem where QuoteId in (' + quoteIdString + ')';

System.debug(qliQuery);

Finally, my test class errors out wherever I try insert a QuoteLineItem.

2

Yes. You can instantiate a custom setting like any other custom object. In this case you would create as many instances of QuoteLineSyncField__c as needed. After populating them with the data you need, insert them like a normal object as part of your test setup.

  • Thanks, I guess my question was short sighted. I initialized the custom settings in my test class but now the Util class still returns an empty set for QuoteSyncLineField__c.getAll() – k shen Oct 31 '14 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.