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
Anji P 9Anji P 9 

i'm trying to know the list is empty for that i used 1)list.size()>0 2) list.isempty(); 3)list!=null even though its not taking empty values are taking how to solve this can any one help in that

when i insert contact child record of account it will take that one only but its asking remaining child records i mean opportunity,case records also how to solve this problem. 


vf page:

<apex:page controller="Account_insert_con">
    <apex:form >
        <apex:pageBlock id="pb" title="Account list" >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="save" action="{!save}"/>
            </apex:pageBlockButtons>
            <apex:pageBlocksection title="account data" id="pbs">
                <apex:inputField value="{!wraps.acc.name}"/>
                <apex:inputfield value="{!wraps.acc.type}"/>
                <apex:inputfield value="{!wraps.acc.phone}"/>
               <apex:inputField value="{!wraps.acc.industry}"/>
            </apex:pageBlocksection>
            <apex:pageBlockSection columns="1"  title="contact list" >
                <apex:pageBlockTable value="{!contacts}" var="c" id="pbt">
                    <apex:column headerValue="firstname">
                        <apex:inputtext value="{!c.firstname}"/>
                    </apex:column>
                    <apex:column headerValue="lastname">
                    <apex:inputText value="{!c.lastname}"/>
                    </apex:column>
                    <apex:column headerValue="phone">
                        <apex:inputText value="{!c.phone}"/>
                    </apex:column>
                    <apex:column headerValue="Leadsource">
                        <apex:inputText value="{!c.leadsource}"/>
                    </apex:column>
                    <apex:column headerValue="AddRow" >
                        <apex:commandButton value="add" action="{!multichild}" id="pbs"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageblocksection columns="1">
                <apex:pageblocktable value="{!opp}" var="o">
                    <apex:column headerValue="name">
                        <apex:inputText value="{!o.name}"/>
                       </apex:column>
                  
                     <apex:column headerValue="stagename">
                        <apex:inputText value="{!o.stagename}"/>
                    </apex:column>
                      <apex:column headerValue="ClosedDate">
                          <apex:inputText value="{!o.CloseDate}"/>
                    </apex:column>
                    <apex:column headerValue="AddRow">
                        <apex:commandbutton value="add" action="{!optyadd}" id="pbs"/>
                    </apex:column>
                </apex:pageblocktable>
            </apex:pageblocksection>
            <apex:pageblocksection columns="1">
                <apex:pageBlockTable value="{!cases}" var="c">
                    <apex:column headerValue="Origin">
                        <apex:inputText value="{!c.origin}"/>
                    </apex:column>
                    <apex:column headerValue="status">
                        <apex:inputText value="{!c.status}"/>
                    </apex:column>
                    <apex:column headerValue="AddRow">
                        <apex:commandButton value="caseadd" action="{!caseadd}" id="pbs"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageblocksection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

apex code:

public class Account_insert_con {
    public Account_insert_wrapper wraps{set;get;}
    public List<contact> contacts{set;get;}
    public List<opportunity> opp{set;get;}
    public List<case> cases{set;get;}
    public Account_insert_con(){
        wraps=new  Account_insert_wrapper();
        wraps.acc=new Account();
        wraps.con=new contact();
        opp=new List<opportunity>();
        cases=new list<case>();
        wraps.opps=new opportunity();
        contacts=new List<contact>();
        contacts.add(wraps.con);
        opp.add(wraps.opps);
        wraps.cs=new case();
        cases.add(wraps.cs);
        multichild();
        optyadd();
        caseadd();
    }
    public void multichild(){
        wraps.con=new contact();
        contacts.add(wraps.con);
        }
    public void optyadd(){
        wraps.opps=new opportunity();
        opp.add(wraps.opps);
    }
    
    public void caseadd(){
        wraps.cs=new case();
        cases.add(wraps.cs);
    }
    
    public PageReference save (){
        insert wraps.acc;
        System.debug('====>'+contacts);
        System.debug('====>'+opp);
        System.debug('====>'+cases);
        List<contact> cons=new List<contact>();
        if(contacts.size() != 0){
            System.debug('in contacts');
        for(contact c:contacts){
            c.Accountid=wraps.acc.id;
            cons.add(c);
        }
        insert cons;
        }

            List<opportunity> opportunities=new List<opportunity>();
        if(opp != null && opp.size() > 0)
           {    
            System.debug('in opportunities');
            for(opportunity o:opp){
                o.AccountId=wraps.acc.id;
                opportunities.add(o);
            }
            insert opportunities;
        }
                List<case> cc =new List<case>();
        if(cases != null && cases.size() > 0) {
            System.debug('in cases');
                for(case co: cases){
                    co.AccountId=wraps.acc.id;
                    cc.add(co);
                }
            insert cc;
        }
        PageReference p=new PageReference('/'+wraps.acc.id);
        return p;
    }

        
}

wrapper class:--

public class Account_insert_wrapper {
    public Account acc{set;get;}
    public contact con{set;get;}
    public opportunity opps{set;get;}
    public case cs{set;get;}
}