Thursday 27 December 2012

Retrieving values from Records using query:- 

Validation using code:-

Here we have a scenario , When we Enter Username and Password from input, 
when we click on Validate Button, It should search all contact Records if those values 
are present in any record it will give Success message otherwise Error Message Should show.

Follow this code:-

<apex:page controller="Validation">
      <apex:form >
          <apex:pageBlock >
          <apex:pagemessages ></apex:pagemessages>
             UserName: <apex:inputText value="{!username}"/>
              password: <apex:inputSecret value="{!password}"/>
            
          <apex:pageblockbuttons >
                  <apex:commandButton value="validate" action="{!validate}"/>
          </apex:pageblockbuttons>    
              
          </apex:pageBlock>
      </apex:form>
</apex:page>


public class Validation {


    public String password { get; set; }

    public String username { get; set; }
     List<Contact> varconObj;
    public pagereference validate() 
     {
         if(username!=''&& password !='')
         {
          varconObj=[select id,name,Email,Password__c from Contact where Email=:username And Password__c=:password ];
        
            if(varconObj.size()>0)
            {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'LOGIN Sucess!'));
                 pagereference varpageref=new pagereference('/apex/searchmail?id='+varconObj[0].id);
                 return varpageref;
                 //return null;
            }else
            {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'LOGIN FAILED!.....INVALID DETAILS!'));
                 return null;
            }
            
        }
        else
        {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter User Name and Password!'));
             return null;
        }   return null;   
          
      } 
}

Using Page Block Table:-

Here I am Using Standard Controller+Extension, Note That You should provide Record id along with URL,
and i have taken all custom objects here. Library__c is junction object of Persons__c and Books__c.
Provide Books__c record id along with url.
It will return column(persons) from Library.


<apex:page STandardController="Books__c" EXtensions="ReturnPersons">
     <apex:form >
         <apex:pageBlock >
             <apex:pageBlockButtons >
                <!-- <apex:commandButton value="ButtonName" action="{!ReturnVales}"/> -->
             </apex:pageBlockButtons>
            
             <apex:pageBlockTable value="{!ReturnVales}" var="Rv">
                 <apex:column value="{!Rv.Persons__c}"/>
             </apex:pageBlockTable>
             
            
         </apex:pageBlock>
     </apex:form>
</apex:page>



public Class ReturnPersons
{

    public ReturnPersons(ApexPages.StandardController controller) {

    }

    String id=ApexPages.currentPage().getParameters().get('id');
    public List<Library__c> getReturnVales()
    {
        List<Library__c> VarLib=[Select id,Books__c,Persons__c from Library__c where Books__c=:id];
        system.debug(VarLib.size());
        return VarLib;
    }
}

No comments:

Post a Comment