+ Start a Discussion
Kiran Kumar GottulaKiran Kumar Gottula 

Code Coverage problem

When I run test class, i faces these exception of below,

 

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

 

How to overcome from this error..

 

plz help me anyone....

 

 

public with sharing class rampschedularcls {

public opportunity opp{get;set;}
public boolean check {get;set;}
public string opportunityid = ApexPages.currentPage().getParameters().get('oppid');
public opportunity opp1 = [select id,Ramp_schedule_start_with_current_month__c from opportunity where id=:opportunityid ];

 



//This is the constructor of rampschedularcls
public rampschedularcls(){

if(opp1.Ramp_schedule_start_with_current_month__c == true )
check = true;
else
check = false;
}

//updating the current opportunity
public pagereference save(){

//opp = [select id,Ramp_schedule_start_with_current_month__c from opportunity where id=:opportunityid ];
if(check == true){
opp1.Ramp_schedule_start_with_current_month__c =true;
}else{
opp1.Ramp_schedule_start_with_current_month__c = false;
}
update opp1;

return (new pagereference('/'+opportunityid).setredirect(true));
}


// Test Class
static testMethod void rampschedularcls() {

account acc = new account();
acc.Name = 'test account';
acc.Account_Type__c = 'test type';
acc.New_Existing_Client__c = 'test client';
acc.Prospect_Source__c = 'test source';
acc.Market__c = 'test market';
acc.CurrencyIsoCode = 'EUR';
insert acc;

opportunity opp = new opportunity();
opp.Name = 'test opportunity';
opp.AccountId = acc.id;
opp.Language__c = 'English';
opp.StageName = 'test stage';
opp.Signed_Contract_Attached__c = 'NO';
opp.AHR__c = 3;
opp.Ramp_schedule_start_with_current_month__c = true;
opp.CurrencyIsoCode = 'EUR';
opp.CloseDate = System.today();
insert opp;

opportunity opp1 = new opportunity();
opp1.Name = 'test opportunity';
opp1.AccountId = acc.id;
opp1.Language__c = 'English';
opp1.StageName = 'test stage';
opp1.Signed_Contract_Attached__c = 'NO';
opp1.AHR__c = 3;
opp1.Ramp_schedule_start_with_current_month__c = false;
opp1.CurrencyIsoCode = 'EUR';
opp1.CloseDate = System.today();
insert opp1;

rampschedularcls rampcls = new rampschedularcls();
rampcls.opportunityid = opp.Id;
rampcls.check = true;
rampcls.save();

rampschedularcls rampclss = new rampschedularcls();
rampclss.opportunityid = opp1.Id;
rampclss.check = false;
rampclss.save();

}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
Sorry, my mistake
Parameter name would be "oppid"
ApexPages.currentPage().getParameters().put('oppid', opp.Id);

All Answers

Bhawani SharmaBhawani Sharma
You have create test data for opportunity, but you have not set opportunity id in URL. That's why code is upnable to eftch the record.
Do this after inseting opprotunity in your test method
ApexPages.currentPage().getParameters().put('id', opp.Id);
Kiran Kumar GottulaKiran Kumar Gottula

Now also I face  same error message..

 

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

Bhawani SharmaBhawani Sharma
Sorry, my mistake
Parameter name would be "oppid"
ApexPages.currentPage().getParameters().put('oppid', opp.Id);
This was selected as the best answer
Kiran Kumar GottulaKiran Kumar Gottula

Thank you tech force.......

 

keep on post best answers for us....

 

Bhawani SharmaBhawani Sharma
My pleasure. I am here to help only :)
Kiran Kumar GottulaKiran Kumar Gottula

Dear mate..

 

Show me best way to adding field values in a list using single line

 

list<string > lststr = new list<>(string);

 

opportunity lstoppfields = [select A__c,B__c  from opportunity where id IN :Trigger.newMap.keySet()];

 

the fallowing way is correct or not..!

 

lststr.add(lstoppfields.A__c);

laststr.add(lstoppfields.B__c);