+ Start a Discussion

System.QueryException: List has no rows for assignment to SObject

Hi all,

Test class error on this line
OpportunityPipeline_Change__c pc = [select Stage_After__c from pportunityPipeline_Change__c where Opportunity__c = :O.Id];
I tried using list and limit.

Isn't the error self explanatory that you don't have any record to associated to pportunityPipeline_Change__c with Oppotuntiy.

Can you post the whole code as how you are doing it,I am assuming that you are not associating pportunityPipeline_Change__c record with correct Opportunity record.

HI Vinit,

private class OpportunityPipelineChangeTest { 
     static testMethod void myUnitTest() {
//COMMENT: First, prepare Dummy Opportunity
        Account a = new Account(Name='OpportunityPipelineChangeTest',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
        insert a;
        Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;
            Opportunity O = new Opportunity(Name='TestPipelineChange',AccountId=a.id,End_User__c=a.id,
            Contact__c=c.id,CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
        insert O;
//COMMENT: Inserting the record with should have caused trigger to fire which means you can move on to the verification steps
//COMMENT:  Verify correct action using System asserts (these make sure your trigger did what it is supposed to)
//COMMENT:  first, we locate the Pipeline Change record that has been added
       OpportunityPipeline_Change__c pc = [select Stage_After__c from OpportunityPipeline_Change__c
            where Opportunity__c = :O.Id];
//COMMENT:  then we assert that the field we updated equals the field it was updated from
        System.assert(pc.stage_after__c == 'Create');

Subhash GarhwalSubhash Garhwal
You need to insert OpportunityPipeline_Change__c record before your query in class.

OpportunityPipeline_Change__c oPC = new OpportunityPipeline_Change__c (Opportunity__c = o.Id, Other Required Fields);
insert oPC;

What is your trigger supposed to do Is it inserting the OpportunityPipeline_Change__c record or something else ??