Activity Forums Salesforce® Discussions Test Database.query

  • Test Database.query

    Posted by Nitish on April 30, 2016 at 5:32 PM

    We have our managed package and we need to install in another org. And there are many custom fields which already exist there. So we cannot create the custom field in our org because it will cause the duplicate issue. So how should i write the test method to cover the dynamic query custom fields?

    I am using dynamic query to query records like

    List<Account> accList = Database.query(‘select Name,customfield__C from Account’);

    as customfield__C is not present in my org it’s value getting retrieved dynamically , so how to give customfield__C value in my test class.

    Gourav replied 10 years ago 2 Members · 1 Reply
  • 1 Reply
  • Gourav

    Member
    June 13, 2016 at 2:46 PM

    You cannot query fields that are not existent in your org. First you’d have to create it either manually or using the api. Anyway in your test class you also need to create the records you want to test the query on. So you’d have something like:

    List<Account> acclist = new List<Account>();

    for(Integer i=0 ;i<200;i++){
    Account a = new Account(Name=’test’+i, customfield__c=’abcd’);
    acclist.add(a);
    }

    insert acclist;
    List<Account> accList = Database.query(‘select Name,customfield__C
    from Account’);
    System.debug(accList);

Log In to reply.