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
Priyesh Misquith 12Priyesh Misquith 12 

Save the multiple selected contact in account using wrapper class

I have created a page to where user can select the multiple contact from the search
but not not able to impliment save funtionality
Apex code: 
public class AccountContactProject {
    public Account accData{set;get;}{accData = new Account();}
    public String Name{get;set;}
    public Boolean MessageDetails{get;set;}{MessageDetails= false;}
    public Boolean ShowDetails{get;set;}{ShowDetails = false;}
    public List<Contact> conList{set;get;}
    Public List<MyWrapper> lisWrapper {get;set;}
    
    public void AccounteditPage(){
        Id IdValue = apexpages.currentpage().getparameters().get('id');
		accData = [select id,name,phone,Email__c,BillingStreet, BillingCity, BillingState, BillingPostalCode,
                   BillingCountry,account.Contact__c from Account where id=:IdValue];
    }
    
    public PageReference save(){
        if(accData.ID!=NULL){
            update accData;
        }else{
            insert accData;
        }

        return null;
    }
    
     public AccountContactProject(){
        lisWrapper = new List<MyWrapper>();
    }    
    
    public pageReference Search(){
        string tempInput = '%' + Name + '%';
        conList =[select id, name from Contact where name Like :tempInput ];
        if(conList!=null && !conList.isEmpty()){
           ShowDetails = true;
            MessageDetails = false;
            for(Contact con : conList){
      	lisWrapper.add(new MyWrapper(con,false));
          }
        }
         else{
        ShowDetails =false;
        MessageDetails = true;
        }
        return null;
    }
    
    public class MyWrapper{
        public contact conWrp{get;set;}
        public boolean checkBoxVal{get;set;}
        public MyWrapper(Contact conWrp, Boolean checkBoxVal){
            this.checkBoxVal = checkBoxVal;
            this.conWrp = conWrp;   
        }
    }
}

Vf Page:
 
<apex:page controller="AccountContactProject" showHeader="true">
    <apex:sectionHeader title="Account Edit" subtitle="New Account"/>
    <apex:form >
        <apex:pageBlock title="Account Edit">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Account Information" columns="2">
                <apex:inputField value="{!accData.name}" />
                <apex:inputField value="{!accData.phone}"/>
                <apex:inputfield value="{!accData.Email__c}"/>
                
                <!-- <apex:inputField value="{!accData.Country}"/> -->
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Account Address" >
                <apex:inputField value="{!accData.BillingStreet}"/>
                <apex:inputField value="{!accData.BillingCity}"/>
                <apex:inputField value="{!accData.BillingState}"/>
                <apex:inputField value="{!accData.BillingPostalCode}"/>
                <apex:inputField value="{!accData.BillingCountry}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Select Contact" >
                <apex:inputText label="Contact Search" value="{!Name}"/>  
                <apex:commandButton id="search" action="{!search}" value="Search"/>
                <apex:outputPanel id="NoData" rendered="{!MessageDetails}"><div>Data is not present in database</div> </apex:outputPanel>
                <apex:outputPanel id="WithData">
                    <apex:pageblock title="Contact Deatils" rendered="{!ShowDetails}" >
                        <apex:pageBlockTable value="{!lisWrapper}" var="wrpData">
                            <apex:column ><apex:inputCheckbox value="{!wrpData.checkBoxVal}"/></apex:column>
                            <apex:column value="{!wrpData.conWrp.name}"/>
                        </apex:pageBlockTable>
                    </apex:pageblock>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks you.
 
Best Answer chosen by Priyesh Misquith 12
Khan AnasKhan Anas (Salesforce Developers) 
Hi Priyesh,

Greetings to you!

You need to update the contacts in save method. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
public class AccountContactProject {
    
    public Account accData{set;get;}{accData = new Account();}
    public String Name{get;set;}
    public Boolean MessageDetails{get;set;}{MessageDetails= false;}
    public Boolean ShowDetails{get;set;}{ShowDetails = false;}
    public List<Contact> conList{set;get;}
    Public List<MyWrapper> lisWrapper {get;set;}
    public List<Contact> del {get;set;}
    
    public void AccounteditPage(){
        Id IdValue = apexpages.currentpage().getparameters().get('id');
        accData = [select id,name,phone,Email__c,BillingStreet, BillingCity, BillingState, BillingPostalCode,
                   BillingCountry from Account where id=:IdValue];
    }
    
    public PageReference save(){
        del=new list<Contact>();
        for(MyWrapper cc: lisWrapper){
            if(cc.checkBoxVal){
                del.add(cc.conWrp);
            }
        }
        System.debug('del-> ' + del);
        List<Contact> conts = new List<Contact>();
        if(accData.ID!=NULL){            
            update accData;
            for(Contact c : del){
            	c.AccountId = accData.Id;
                conts.add(c);
            }            
            UPDATE conts;
        }else{
            insert accData;
            for(Contact c : del){
            	c.AccountId = accData.Id;
                conts.add(c);
            }
            UPDATE conts;
        }
        
        return null;
    }
    
    public AccountContactProject(){
        lisWrapper = new List<MyWrapper>();
    }    
    
    public pageReference Search(){
        string tempInput = '%' + Name + '%';
        conList =[select id, name from Contact where name Like :tempInput ];
        if(conList!=null && !conList.isEmpty()){
            ShowDetails = true;
            MessageDetails = false;
            for(Contact con : conList){
                lisWrapper.add(new MyWrapper(con,false));
            }
        }
        else{
            ShowDetails =false;
            MessageDetails = true;
        }
        System.debug('lisWrapper-> ' + lisWrapper);
        return null;
    }
    
    public class MyWrapper{
        public contact conWrp{get;set;}
        public boolean checkBoxVal{get;set;}
        public MyWrapper(Contact conWrp, Boolean checkBoxVal){
            this.checkBoxVal = checkBoxVal;
            this.conWrp = conWrp;   
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas