function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Pal 33Abhishek Pal 33 

How to cover SOQL query in test cases?

I want to cover the below line in my test case but I am not able to do that. I have created the campaign ID and have set properly in Order_credit__c but in actual apex class the below query is not coverage in test cases.

In Test case:-

Before update Order_Credit__c:{RecordTypeId=012E00000005hDGIAY, Status__c=Submitted, Approval_Required_Reason__c=Test, Account__c=0012200000B6V66AAF, Id=a0A22000000GW5qEAG, Campaign__c=7012200000017LjAAI, Order_Type__c=Suggested}

When control goes to below code in actual class it doesnt find anything.

Code:

for(Order_Credit__c orderCredit: [SELECT id, Order_Type__c FROM Order_Credit__c WHERE Order_Campaign__c IN :campaignIdSet  AND Status__c = 'Submitted'])

NOTE: I can understand the data is generated locally and is available only in scope of that class but is there any way that I can cover the above query in test class.

Please help, thanks in advance.

-Abhishek
Pruthvi KankunthalaPruthvi Kankunthala
@Abhishek : Yes you can cover / You have to insert a Order_Campaign__c record with all the fields available in the query and also it should satisfy the criteria . Can you post the code here , so that I can take a look ?
Abhishek Pal 33Abhishek Pal 33
Hi Pruthvi,

I guess its not possible apart from only one option (See All Data=true) which I tried to avoid. The reason being though I have created all Order_Campaign__c record with all the fields but that will be available only till the test case will run.

But in this query we are trying to run a query based on the ID which is not there at all in database(dummy test database).

Code which I am trying to cover in test case:-

List<Order_Credit__c> updateOrderCredits= new List<Order_Credit__c>();
        Set<Id> campaignIdSet = new Set<Id>();
        for(Campaign camp: triggerNew) {
            if(Camp.Send_Orders_via_EDI__c == true) {
                campaignIdSet.add(camp.id);     
            }
        }
        System.debug('campaignIdSet '+campaignIdSet);
        if(!campaignIdSet.isEmpty()) {
           
            for(Order_Credit__c orderCredit: [SELECT id, Order_Type__c FROM Order_Credit__c WHERE Order_Campaign__c IN :campaignIdSet      AND Status__c = 'Submitted']) {
              // System.debug('Before If Order credit :' +orderCredit);
                if(orderCredit.Order_Type__c == 'Suggested') {
                    System.debug('In IF ');
                    orderCredit.Order_Type__c = 'Distributor';
                    updateOrderCredits.add(orderCredit);
                }
            }