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
sml9099sml9099 

Apex class to Fetch selected records from custom object

Hey Guys, 

I am new to Apex development. I am trying to write a VF page in which user will enter values in certain fields. And, then they will click the FETCH button. Based on the information entered in the fields the FETCH button will get the records from cutom object which meet the the critirea. I know it looks simple but I cannot figure out the logic. In general I can fetch the records but not those records which meet the critirea. I am posting the work I have done. I would really appreciate if some can help. I am getting this error.

List has no rows for assignment to SObject

VF page:

<apex:page controller="newsiteplacement1" tabStyle="Site_Placements__c">
   <apex:form >
    <apex:pageMessages />
        <apex:pageBlock >
                <center>
                <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!Fetch}" value="Fetch"/>
               
                </apex:pageBlockButtons>
                </center>
                <apex:pageBlockSection title="Initial Input Information"
collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Passed CPM Floor" />
                    <apex:inputField value="{!splace.Passed_CPM_Floor__c}"/>
                </apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Auto Play" />
                    <apex:inputField value="{!splace.Auto_Play__c}"/>
                </apex:pageBlockSectionItem>
               </apex:pageBlockSection>
        </apex:pageBlock>
   </apex:form>
</apex:page>

Apex Class:

public class newsiteplacement1
{
    private final Site_Placements__c splace;
    //public List<Site_Placements__c> splace {get; set;}
   
        public newsiteplacement1()
           
            {
              
                splace=[SELECT Id,Name FROM Site_Placements__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
               
            }
           
            public Site_Placements__c getsplace() {
        return splace;
    }
    public PageReference Fetch() {
        //update splace;
        return null;
    }
           
}
Sonam_SFDCSonam_SFDC
I understand that you wish to use the CPM floor and Auto Play fields to search the records from the database object - to do that you will have to use them in the query you are wrting in the controller.

Check the following post which gives sample code and explains how this can be acheived:
http://salesforce.stackexchange.com/questions/7836/fetch-records-on-standard-object
NK@BITNK@BIT
You can do like that...

----------Apex Class----------

public class GmailFetch{
   
    public NKBIT__Gmail__c gm{get;set;}
    public List<NKBIT__Gmail__c> gmailRec{get;set;}
   
    public GmailFetch(){
        gm=new NKBIT__Gmail__c();
    }
   
    public void FetchGmailRec(){
       
        gmailRec=[select NKBIT__Phone__c, NKBIT__User_Name__c, NKBIT__Email__c from NKBIT__Gmail__c where NKBIT__User_Name__c=:gm.NKBIT__User_Name__c limit 1];
        if(gmailRec.size()>0)
        {
            gm.NKBIT__Email__c=gmailRec[0].NKBIT__Email__c;
            gm.NKBIT__Phone__c=gmailRec[0].NKBIT__Phone__c;
        }
    }
   
    public pageReference saveGmailRec(){
        gmailRec=new List<NKBIT__Gmail__c>();
        gmailRec=[select NKBIT__Phone__c, NKBIT__User_Name__c, NKBIT__Email__c from NKBIT__Gmail__c where NKBIT__User_Name__c=:gm.NKBIT__User_Name__c limit 1];
        if(gmailRec.size()>0)
        {
            gmailRec[0].NKBIT__Email__c=gm.NKBIT__Email__c;
            gmailRec[0].NKBIT__Phone__c=gm.NKBIT__Phone__c;
           
            update gmailRec;
            gm.clear();  
        }
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'Record Updated Successfully!'));
        return null;
    }
}

-----------Visualforce Page-----------

<apex:page controller="GmailFetch">
   
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock>
             <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="User Name" for="Username"/>
                    <apex:inputField value="{!gm.NKBIT__User_Name__c}" id="Username"/>
                </apex:pageBlockSectionItem>
               
                <apex:commandButton value="Fetch" action="{!FetchGmailRec}"/>
             </apex:pageBlockSection>
            
             <apex:pageBlockSection title="Gmail Record" Collapsible="false">
            
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Email" for="email"/>
                    <apex:inputField value="{!gm.NKBIT__Email__c}" id="email"/>
                </apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Phone" for="phone"/>
                    <apex:inputField value="{!gm.NKBIT__Phone__c}" id="phone"/>
                </apex:pageBlockSectionItem>
               
                <center><apex:commandButton value="Update" action="{!saveGmailRec}"/></center>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I think it would help you..
sml9099sml9099
Hey guys , 

Thanks for your reply. I did read the blog post and also NK@BIT reply. It surely hellped me and also took help from some friends but it worked out. I was just wondering, do you guys also have test class for it ?

Thanks