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
mustapha L 1mustapha L 1 

Test class fails 1st line !

Hi All,
I did a Class/methods who seems to be working into Sand box.
I have started writting a test class, but i always have a fatal error :
FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject
what cannot be true...

I explain myself.
My Trigger is supposed to fire after insert/update on Quotelineitem
So I have a created an Opportunity / Quote / quotelineItem on SAND box.

I have runned some SQL to extract the ID of the QuotelineItem.

into my test class i did a basic stuff as :

        // 3_Insert QLI SRV- professional Service (check costs calculation)
        QuoteLineItem testqli= new QuoteLineItem(Id='0QL250000000VXFGXX', Quantity=2);
        update testqli;


which fire the next :
 for (QuotelineItem qli :quoteLines)   {
            QuoteLineItem newqli = new QuoteLineItem();
                        //initializing the new List           
               newqli.Id=qli.Id;      
                       //Extract the Product informations
            String ProdId = qli.Product2Id;                              //system.debug shows that qli.product2Id is : "01t25000000V73gAXX"
Product2 prod = [SELECT Id, ProductCode, Provider__c, Purchase_Purchase_isP__c, Is_Maintenance__c, PurchaseP__c, Purchase_Price__c, Extra__c, CurrencyIsoCode FROM Product2 WHERE Id = :ProdId LIMIT 1];

which cause the Fatal Error as no row returned.

however in DEV console /SQL :

SELECT product2Id from quotelineItem where id ='0QL250000000VXFGXX'
result is : '01t25000000V73gAXX'                       //what is the expected result

SELECT Id, ProductCode, Provider__c, Purchase_Purchase_isP__c, Is_Maintenance__c, PurchaseP__c, Purchase_Price__c, Extra__c, CurrencyIsoCode FROM Product2 WHERE Id = 01t25000000V73gAXX LIMIT 1
result is the expected one (productCode : SRV-CTRK-D, Provider__s : Test, CurrencyIsoCode : GBP...etc)

Why developper console returns a Row, but TestClass doesn't ...for same SQL...no sense ???

could you help please ?
Many thanks

 
Best Answer chosen by mustapha L 1
David Holland 6David Holland 6
As you are using a test method, the data in your org is not available, so hardcoding the Id will not work in test methods.

You need to either recreate that variable within your test, or add (SeeAllData = true) onto your test method - note, this is not good as moving between testing orgs may make this fail.

All Answers

David Holland 6David Holland 6
As you are using a test method, the data in your org is not available, so hardcoding the Id will not work in test methods.

You need to either recreate that variable within your test, or add (SeeAllData = true) onto your test method - note, this is not good as moving between testing orgs may make this fail.
This was selected as the best answer
Tarun_KhandelwalTarun_Khandelwal

Hi,

Please add "@isTest(SeeAllData=true)" parmater before your test method. for example, if your test method name is test_Method_1 then,

@isTest(SeeAllData=true)
private static void test_Method_1() {
//
// your code of test class
//
}

Let me know if you have any query.
David Holland 6David Holland 6
Tarun, we shouldn't really be encouraging people the easy option of adding "SeeAllData" surely?

This should be given as a last resort, but really, people should be recreating their test data and not hardcoding any Ids
mustapha L 1mustapha L 1
Hi all, thanks for your reeply first !
next, i'm not sure to undesrtand, you mean that the data (ie: product) i have created into my SandBox cannot be used...i have to right paex code in order to create the environment (opportunity, quote, product...) event the trigger only fire for QuoteLineItem ?
thank you very much
David Holland 6David Holland 6
Mustapha

Yes, when you run a test method, the system has no access to data already in the org, unless you use the "SeeAllData=true" flag.

Therefore if you wish to run tests, then you need to create all the records you need.
Tarun_KhandelwalTarun_Khandelwal
Hi Mustapha,

Use following link for test data creation - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm
mustapha L 1mustapha L 1
you are PEERFECT both !
On thing more, i did a small change on my query, and now i have the expected result (so : 1 row)

original query :
SELECT Id, ProductCode, Provider__c, Purchase_Purchase_isP__c, Is_Maintenance__c, PurchaseP__c, Purchase_Price__c, Extra__c, CurrencyIsoCode FROM Product2 WHERE Id = 01t25000000V73gAXX LIMIT 1

(where Id is the id of the Product2 which is linked to the QuoteLineItem)

new query:
SELECT Product2.ProductCode, Product2.Provider__c, Product2.Purchase_Purchase_isP__c, Product2.Is_Maintenance__c, Product2.PurchaseP__c, Product2.Purchase_Price__c, Product2.Extra__c, Product2.CurrencyIsoCode FROM QuoteLineItem WHERE Id = blablabla LIMIT 1];

It worck fine...i have add the next to anssure that i could read the product2 fields via the QuoteLineItem Query and it works :
system.debug('Maintenance  : ' + newqli.Product2.Is_Maintenance__c);

that's perfect...i still doesn't understand why with That way, test can read Product2 info from org (i have not add the "SeeAllData"

any way GREAT THANK to you both, I better understand now.
Sindhu1234Sindhu1234
Mustapha,

Don't forget to mark the best answer.