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
Venkat Reddy sfdVenkat Reddy sfd 

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

hi I am getting this Error Colud you Please Check... Error:System.QueryException: List has no rows for assignment to SObject
Class.Mysecondcontroller.<init>: line 4, column 1


here is My Apex Class and VF Code
public class Mysecondcontroller {
 Public Opportunity opp;
  Public Mysecondcontroller(){
    Opp=[select id, name,CloseDate,StageName from 
          Opportunity where id=:ApexPages.currentpage().getParameters().get('Id')];
          
}
 Public Opportunity getOpportunity(){
   return opp;
  }
       Public pageReference SaveMethod(){
       Update Opp;
         return null;
  } 
}
<apex:page controller="Mysecondcontroller" tabStyle="Opportunity">
<apex:form >
<apex:pageBlock title="Opportunity Detail">
<apex:pageBlockButtons >
<apex:commandButton value="Save The Opportunity" action="{!SaveMethod}"/>
</apex:pageBlockButtons>
<apex:pageblockSection title="Opportunity" Columns="2" collapsible="False">
<apex:inputField value="{!Opportunity.Name}"/>
<apex:inputField value="{!Opportunity.CloseDate}"/>
<apex:inputField value="{!Opportunity.StageName}"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


 
Jagannatha Reddy BandaruJagannatha Reddy Bandaru
Hi Venkat,

i think you no need to write query
just create instance for opportunity.then you will get expected output.

public class Mysecondcontroller
{
 Public Opportunity opp{get;set;}

  Public Mysecondcontroller()
  {
  opp=new Opportunity();
   }
   Public pageReference SaveMethod(){
       Update Opp;
        return null;
  }

}


Thanks,
Jagan
jagan.srit@gmail.com
Deepak Kumar ShyoranDeepak Kumar Shyoran
Use below constructor code to remove your exception
Public Mysecondcontroller(){
	
	
   List<Opportunity> oppList =[select id, name,CloseDate,StageName from
          Opportunity where id=:ApexPages.currentpage().getParameters().get('Id')];

	if(oppList.size() > 0)
		opp = oppList[0] ;
}