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
Himanshu GhateHimanshu Ghate 

What Wrong with my code?Showing the System.QueryException: List has no rows for assignment to SObject

public class QuoteHW {
 public CustomOpportunity__c opp{get;set;}
    public CustomQuote__c quo {get;set;}
    public Id oppid{get;set;}
    public CustomOpportunitylineitem__c oppli{get;set;}
    public List<CustomOpportunitylineitem__c> opplilist{get;set;}
    public CustomQuotelineitem__c quoli{get;set;}
    
    public QuoteHW()
        
        
    {   
     ApexPages.Pagereference pageref = ApexPages.currentPage();
         oppid = pageRef.getParameters().get('Id');
      
      
        opp = new CustomOpportunity__c();
        opp = [Select Name, AccountName__c,Quantity__c, ContactName__c, Amount__c from CustomOpportunity__c where Id=: oppid ];
        opplilist = [SELECT Name , ActualPrice__c,DiscountValue__c, CustomOpportunity__c, CustomProduct__c, Quantity__c, UnitPrice__c from CustomOpportunitylineitem__c where CustomOpportunity__c=: opp.Id];
        
        
    }
    public pagereference Save()
       {
         quo = new CustomQuote__c();
         quo.AccountName__c= opp.AccountName__c;
         quo.CustomOpportunity__c = oppid;
         quo.Quantity__c= opp.Quantity__c;
         quo.ContactName__c= opp.ContactName__c;
         quo.Amount__c= opp.Amount__c;
         quo.Name = opp.Name;
         insert quo;
         
         for( CustomOpportunitylineitem__c a : opplilist)
         {
            quoli = new CustomQuotelineitem__c();
            quoli.ActualPrice__c= a.ActualPrice__c;
            quoli.DiscountValue__c= a.DiscountValue__c;
            quoli.CustomProduct__c= a.CustomProduct__c;
            quoli.Quantity__c = a.Quantity__c;
            quoli.UnitPrice__c = a.UnitPrice__c ;
            quoli.CustomOpportunitylineitem__c = a.Id;
            quoli.CustomOpportunity__c = a.CustomOpportunity__c ;
            quoli.CustomQuote__c = quo.Id;
            quoli.name = a.name;
            insert quoli;
          }
            
          
         pagereference pr = new pagereference('/' + quo.Id);
         return pr;
         
         
         
                    
       
       }
    

}IN SOQL showing an error ,Can someone please me out form this code.<apex:page controller="QuoteHW"  sidebar="false" >
 <apex:form >
  <apex:sectionHeader title="New Quote"/>
   <apex:pageBlock title="Edit Quote">
   
   <apex:pageBlockButtons >
    <apex:commandButton value="Save"  action="{!Save}"/>
    <apex:commandButton value="Cancel" />
   </apex:pageBlockButtons>
   
    <apex:pageBlockSection title="Quote">
     <apex:outputField label="Account Name" value="{!opp.AccountName__c}"/>
     <apex:outputField label="Quantity" value="{!opp.Quantity__c}"/>
     <apex:outputField label="Contact Name" value="{!opp.ContactName__c}"/>
     <apex:outputField label="Amount" value="{!opp.Amount__c}"/>
     </apex:pageBlockSection>
     <apex:pageBlockSection title="Product Details">
     
     <apex:pageBLockTable value="{!opplilist}" var="opl" >
     
      <apex:column value="{!opp.AccountName__c}" headerValue="Account Name"/>
      <apex:column value="{!opl.CustomOpportunity__c}" headerValue="Opportunity Name"/>
      
      <apex:column value="{!opl.Name}" headerValue="Opportunity Line Item Name"/>
      
      <apex:column headerValue="Actual Price"> 
       <apex:inputField value="{!opl.ActualPrice__c}" />
       </apex:column>
      
      <apex:column headervalue="Discount Value" >
      <apex:inputField value="{!opl.DiscountValue__c}" />
      </apex:column>
        <apex:column headervalue="Product">
      <apex:inputField value="{!opl.CustomProduct__c}" />
      </apex:column>
      
      
      <apex:column headerValue="Quantity">
       <apex:inputField value="{!opl.Quantity__c}" />
      </apex:column>
      <apex:column headervalue="Unit Price">
      <apex:inputField value="{!opl.UnitPrice__c}" />
      </apex:column>
     
      
      
     </apex:pageBLockTable>
    
    </apex:pageBlockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page>


 
PrabhaPrabha

Short Answer: Because you didnt pass the Id in the URL. This error wont show up when you embed this Visualforce in the page.

Long answer: The error "System.QueryException: List has no rows for assignment to SObject" happens when you directly assign the query result to a single variable. In your case you are querying by  oppid which is coming from URL by pageRef.getParameters().get('Id'). When we look at the URL, I dont see that you are passing an Id value. The system can't find a record where Id is null, Hence the error.