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
developerTeamdeveloperTeam 

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

Hi ,

Am creating a VF Page for New/Edit page for my CustomObject and With the Following Controller am getting:

Visualforce Error

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

 

Class :

public class NewColorPaletteController{

  public Color_Palette_To_Color__c record;    
  public string b{get; set;} 

  public NewColorPaletteController() {
      record= [select id,Name,Color_Palette__c,Color__c FROM Color_Palette_To_Color__c where id=:ApexPages.currentPage().getParameters().get('Id')];
    }

  Public Color_Palette_To_Color__c  getRecord()
  { return record;
  }
    
  Public PageReference SaveMethod(){
       try{
            update record;
        }
       catch(exception e){
      //      // handle the exception
      }
        return new PageReference('/apex/relatedListColorPalette?id='+record.Color_palette__c);
  }

  Public PageReference cancelMethod() {
        return null;
    }

}

 Please provide Solution for this Error.

 

-Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97

probably because there is no id passed in the url??

 

you can put a try catch around tthe soql if you dont want the error

 

record= [select id,Name,Color_Palette__c,Color__c FROM Color_Palette_To_Color__c where id=:ApexPages.currentPage().getParameters().get('Id')];

All Answers

developerTeamdeveloperTeam

My VF page Code is below :

 

<apex:page showHeader="false" sidebar="true" Controller="NewColorController"  >
<head>
</head>
<body>
<apex:sectionHeader title="Color Edit"  subtitle="New Color"/>
<apex:form id="myForm">  
 <apex:pageBlock title="Color Edit" mode="edit">
   <Apex:pageBlockButtons >
    <apex:commandButton action="{!saveMethod}" value="Save" style="width:40px;"/>   
    <apex:commandButton action="{!cancelMethod}" value="Cancel" style="width:50px;"/>   
   </Apex:pageBlockButtons>
   <apex:pageBlockSection title="Information" columns="1"> 
               <br/>
               <apex:pageBlockSectionItem >
               <apex:outputLabel value="Color Palette" for="Color_Palette__c" />
                 <apex:outputfield value="{!record.Color_Palette__c}" id="Color_Palette__c" />
              </apex:pageBlockSectionItem>
              <br/>
              <apex:pageBlockSectionItem >
               <apex:outputLabel value="Color" for="Color__c" />
                 <apex:inputField id="Color__c" value="{!record.Color__c}"  />
              </apex:pageBlockSectionItem>             
    </apex:pageBlockSection> 
  </apex:pageBlock>
</apex:form>  
</body>
</apex:page>

 

testrest97testrest97

probably because there is no id passed in the url??

 

you can put a try catch around tthe soql if you dont want the error

 

record= [select id,Name,Color_Palette__c,Color__c FROM Color_Palette_To_Color__c where id=:ApexPages.currentPage().getParameters().get('Id')];
This was selected as the best answer