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
AichaSFAichaSF 

Test Class Running - Error Exception 'System.QueryException: List has no rows for assignment to SObject'

Hello, 
I created this class 
public class LNC01_Opp_Bonlivraison {    
    @AuraEnabled
    public static String getAccountWeather(Id oppid) {
        try {
            //Recupetation Opportunity Id
            Opportunity opp = [SELECT id, AccountId, createddate, OwnerId, Traitement_Fiscal__c,Pricebook2Id FROM Opportunity WHERE Id=:oppid];           
            system.debug('oppid  '+opp.id);
            
            //Recuperation de type de transaction Bon de livraison
            s2cor__Sage_INV_Trade_Document_Type__c typetransaction = [select id from s2cor__Sage_INV_Trade_Document_Type__c where name = :label.LNC_BonLivraison];
            if (opp != null || Test.isRunningTest())
            { //Creation Nouvelle transaction
               s2cor__Sage_INV_Trade_Document__c Newtransaction = new s2cor__Sage_INV_Trade_Document__c();
            Date myDate = date.newinstance(opp.createddate.year(), opp.createddate.month(), opp.createddate.day());
            
            Newtransaction.s2cor__Account__c= opp.AccountId;
            Newtransaction.Opportunite__c= oppid;
            Newtransaction.s2cor__Date__c= myDate;
            Newtransaction.OwnerId= opp.OwnerId;            
            Newtransaction.s2cor__Tax_Treatment__c= opp.Traitement_Fiscal__c;       
            Newtransaction.s2cor__Trade_Document_Type__c= typetransaction.id;         
            Newtransaction.s2cor__Price_Book__c = opp.Pricebook2Id;          
                
            
            insert Newtransaction; 
            
            system.debug('Newtransaction  '+Newtransaction.id);
            
            //Creation nouvelles lignes de transaction             
            List<OpportunityLineItem> Opplineitem = [select id,Product2Id,Quantity,Dimension_Tag_Analytique__c,Dimension_Tag_budget__c,Dimension_Tag_BUDGET1__c,Dimension_Tag_BUDGET2__c
                                                     from OpportunityLineItem where OpportunityId= :opp.id];
            system.debug('Opplineitemsize  '+Opplineitem.size()); 
            
            id Product_Tag = NULL;
            
            list <s2cor__Sage_INV_Trade_Document_Item__c> listtransactionlinetoupdate = new list <s2cor__Sage_INV_Trade_Document_Item__c>();
            for (OpportunityLineItem  Opplineit : Opplineitem){
                
                s2cor__Sage_INV_Trade_Document_Item__c transactionlinetoupdate = new s2cor__Sage_INV_Trade_Document_Item__c();
                transactionlinetoupdate.s2cor__Trade_Document__c= Newtransaction.id;
                transactionlinetoupdate.s2cor__Product__c=  Opplineit.Product2Id;
                transactionlinetoupdate.s2cor__Quantity__c = Opplineit.Quantity;              
                listtransactionlinetoupdate.add(transactionlinetoupdate);
                 }
            
             system.debug('listtransacsizetionlinetoupdate  '+listtransactionlinetoupdate.size()); 
            insert listtransactionlinetoupdate;
              	
        } 
            return '{"succes": "}'; 
            }catch(Exception ex){
             system.debug('Exception  '+ex); 
            return '{"error": "}';
        }
    }
}

and her test class: 
@istest
public class LNC01_Opp_Bonlivraison_Test {
static testMethod void theTests(){
Opportunity Opp = new Opportunity();
Opp.name = 'testopp1';
Opp.StageName = '1 Pre-Qualified'; 
Opp.CloseDate= system.today();
Opp.AccountId = '001D0000024cW2MIAU';   
insert Opp;   
LNC01_Opp_Bonlivraison.getAccountWeather(Opp.id);
}    
}

I have this error when running test class: 
Exception  System.QueryException: List has no rows for assignment to SObject
 
Gopal ChatGopal Chat
Hi
Can you give youe Cridentials
AichaSFAichaSF
Hello, 
Thank you for you're response.
But I can't understand you.


 
Raj VakatiRaj Vakati
Try this code and Add other required fields  into the objeccts .. the error you are getting becasue of no data in the test class 
 
@istest
public class LNC01_Opp_Bonlivraison_Test {
static testMethod void theTests(){
Opportunity Opp = new Opportunity();
Opp.name = 'testopp1';
Opp.StageName = '1 Pre-Qualified'; 
Opp.CloseDate= system.today();
Opp.AccountId = '001D0000024cW2MIAU';   
insert Opp;   

// insert the s2cor__Sage_INV_Trade_Document_Type__c 

s2cor__Sage_INV_Trade_Document_Type__c  ins = new s2cor__Sage_INV_Trade_Document_Type__c () ;
ins.Name =System.label.LNC_BonLivraison;
// Add other required fields  here 
insert ins ; 



s2cor__Sage_INV_Trade_Document__c  trade = new s2cor__Sage_INV_Trade_Document__c () ;
// Add other required fields  here 

insert trade;

Product2 pro2 = new Product2(Product_Line__c='DIE',Name='BXCD',Product_Code_Item_Number__c='BXCD24', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id,
                                     Newextension__c=0.02);
 insert OPplineitem2;
 
 
 s2cor__Sage_INV_Trade_Document_Item__c docItem = new s2cor__Sage_INV_Trade_Document_Item__c();
 docItem.Name='werwer';
 // Add other required fields  here 

 insert docItem ; 
 

LNC01_Opp_Bonlivraison.getAccountWeather(Opp.id);
}    
}

 
AichaSFAichaSF
Thank you for you're response.
I changed test class like this :
@istest
public class LNC01_Opp_Bonlivraison_Test {
static testMethod void theTests(){
  Profile p = [SELECT Id FROM profile WHERE name='Administrateur système'];
        User usr = new user( Username= 'test.user@psa.com',ProfileId=  p.Id, IsActive= true, LastName= 'test', Email = 'test@test.com', Alias = 'test', TimeZoneSidKey= 'Europe/Paris',
                            LocaleSidKey = 'fr_FR_EURO', EmailEncodingKey = 'UTF-8' , LanguageLocaleKey= 'en_US'); 
    system.runAs(usr){  
    
Opportunity Opp = new Opportunity();
Opp.name = 'testopp1';
Opp.StageName = '1 Pre-Qualified'; 
Opp.CloseDate= system.today();
Opp.AccountId = '001D0000024cW2MIAU';   
Opp.Ownerid= '00520000001L9fzAAC'; 
opp.Traitement_Fiscal__c= 'a3hD0000001uZcPIAU';
insert Opp;   

// insert the s2cor__Sage_INV_Trade_Document_Type__c 

s2cor__Sage_INV_Trade_Document_Type__c  ins = new s2cor__Sage_INV_Trade_Document_Type__c () ;
ins.Name =System.label.LNC_BonLivraison;
// Add other required fields  here 
insert ins ; 
system.debug('ins  '+ins.id);
    
    
Product2 pro2 = new Product2(Name='BXCD', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=Opp.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id
                                    );
 insert OPplineitem2;
OpportunityLineItem OPplineitem3 = new OpportunityLineItem (Quantity=2, OpportunityId=Opp.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id
                                    );
 insert OPplineitem3;

   s2cor__Sage_INV_Trade_Document__c Newtransaction = new s2cor__Sage_INV_Trade_Document__c();

LNC01_Opp_Bonlivraison.getAccountWeather(Opp.id);
}    
}
    }

I have 58% of coverage and this error in log:
|Exception  System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Attempt to de-reference a null object: []

Thank you.