Tuesday, 12 April 2016

Soql And DML Example

Scenario :1
 1. Create a DynamicDMLExample
  1.DataMemebers
   1.Name : String
   2.Industry: String
   3.Phone   :String
   4.accs   :List<Account>
   5.flag   :Boolean
  2.Create a constructor
   assign flag as true
  3.create a method search()
   1.Fetch all the account records from Account which
    same name given in the VF page
  4.Create method create();
   1.This should take a new account record and returnto
   detail page of account new accountrecord creatd 

Class :

public class DynamicDMLExample {
    public String name   {set;get;}
    public String industry  {Set;get;}
    public String phone  {set;get;}
    public Boolean flag {set;get;}
    public List<Account> accs {Set;get;}
    public DynamicDMLExample(){
        flag=true;
    }
    public void search(){
        flag=false;
        accs=[select id,name,Industry from Account where name=:name];
    }
    public PageReference create(){
        Account a=new Account();
        a.name=name;
        a.industry=industry;
        a.phone=phone;
        insert a;
       PageReference p=new PageReference('/'+a.id);
        return p;
    }

}
Visualforce:
<apex:page controller="DynamicDMLExample" >
    <apex:form >
     <apex:pageBlock title="Account Create">
            <apex:pageBlockButtons >
             <apex:commandButton value="Search" action="{!search}"/>
                <apex:commandButton value="create" disabled="{! flag}" action="{!create}"/>
            </apex:pageBlockButtons>
         <apex:pageBlocksection  columns="1">
             <apex:pageBlocksectionItem >
                    <apex:outputLabel value="Account Name" />
                    <apex:inputText value="{!name}" />
                </apex:pageBlocksectionItem>
                <apex:pageBlocksectionItem >
                    <apex:outputLabel value="Industry" />
                    <apex:inputText value="{!industry}" />
                </apex:pageBlocksectionItem>
                <apex:pageBlocksectionItem >
                    <apex:outputLabel value="Phone" />
                    <apex:inputText value="{!phone}" />
                </apex:pageBlocksectionItem>
            </apex:pageBlocksection>
        </apex:pageBlock>
        <apex:pageBlock title="Account Records" rendered="{! !ISNULL(accs)}">
         <apex:pageBlockTable value="{!accs}" var="a">
             <apex:column>
                    <apex:commandLink value="select" action="{!URLFOR($Action.Account.edit,a.id)}"/>
                </apex:column>
                <apex:column value="{!a.name}" />
                <apex:column value="{!a.industry}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
         
========================================================================
Scenario :2 
 1.Create a wrapper  class AccountWrap with two datamemembers
  1.flag :Boolean
  2.acc  : Account
  3.AccountWrap(Account acc)

 2.Create a AccountLineItems class 
  1. accounts :List<AccountWrap>
  2.AccountLineItems(): 
   1.Fetch all the accounts records from Sobject :Account
   2. Create a list of AccoutWrap objects based on the 
      records fetched from soql

 3. Create a method deleteRecords()
  1. fetch all the account records which are selected  from 
     vf page 
  2.Delete the selected records
  3.run the soql query and recreate AccountWrapper list

public class AccountWrap {
    public Boolean flag {set;get;}
    public Account acc{set;get;}
    public AccountWrap(Account acc){
        this.acc=acc;
    }

}
----------------------------------------------
public class AccountLineItems {
    public List<Accountwrap> accounts {Set;get;}
    public AccountLineItems(){
        accounts=new List<AccountWrap>();
       soqlRecords();
    }
    public void soqlRecords(){
        List<Account> result=[select id,name,Industry,phone from Account];
        for(Account a:result){
          AccountWrap aw=new AccountWrap(a);
            accounts.add(aw);
        }
    }
    public void deleteRecords(){
        List<Account> records=new List<Account>();
        for(AccountWrap aw:accounts){
            if(aw.flag==true){
                records.add(aw.acc);
            } 
        }
        Database.delete(records,false);
        accounts.clear();
        soqlrecords();
    }

}
-----------------------------------------
<apex:page  controller="AccountLineItems">
    <apex:form>
     <apex:pageBlock title="Account List">
            <apex:pageBlockButtons>
                <apex:commandButton value="Delete" action="{!deleteRecords}" />
            </apex:pageBlockButtons>
         <apex:pageBlockTable value="{!accounts}" var="a">
             <apex:column>
                    <apex:inputCheckBox value="{!a.flag}" />
                </apex:column>
                <apex:column value="{!a.acc.name}" />
                <apex:column value="{!a.acc.industry}" />
                <apex:column value="{!a.acc.phone}" />    
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
========================================================================

No comments:

Post a Comment