I am trying to implement below example shown in screenshot :-

Account Wrapper DML Example

I want to check that for records where checkbox is selected, name field should not be null, then only insertion should happen, but name validation is not happening in my create method.

If am using InputField then standard name validation is being obeyed but I am using Inputtext and want to validate name before inserting.

Below are the code spinets and my wrapper class which I have tried :-

acc is Account reference variable in AccountWrapper and wrapper is list reference variable for AccountWrapper in my class

Wrapper Class:

public class AccountWrapper {

    public Account acc {set;get;}
    public Boolean flag {set;get;}


public class DML_Example_3_sum {

    public list<AccountWrapper> wrapper {set;get;}
    public list<Account> accounts {set;get;}

    public DML_Example_3_sum(){

        wrapper = new list<AccountWrapper>();
        accounts= new list<Account>();
    public void add(){

        AccountWrapper aw = new AccountWrapper();
        Account a = new Account();


1st code Snippet:

public PageReference create(){

        for(AccountWrapper a : wrapper){

            if(a.flag == true && a.acc.name != Null){

                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Select Accounts to be inserted');
                insert accounts;
                PageReference p = new PageReference('/001/o');
                return p;
            }catch(Exception e){              
                return null;

2nd Code Snippet:

public PageReference create(){

        for(AccountWrapper a : wrapper){

            if(a.flag == true){

                     if(a.acc.Name == Null){

                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Name Cannot be blank');


VF Page:

<apex:page controller="DML_Example_3_sum" >

    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Accounts">
            <apex:sectionHeader title="Add accounts" subtitle=" Accounts" />
            <apex:pageBlockButtons location="top" >
                <apex:commandButton value="Create" action="{!create}" />
            <apex:pageBlockTable value="{!wrapper}" var="a" >
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox />
                    <apex:inputCheckbox value="{!a.flag}" />
                <apex:column headerValue="Name" >
                    <apex:inputText value="{!a.acc.Name}" />
                <apex:column headerValue="Phone" >
                    <apex:inputField value="{!a.acc.Phone}" />
                <apex:column headerValue="Rating" >
                    <apex:inputField value="{!a.acc.Rating}" />
                <apex:column headerValue="Industry" >
                    <apex:inputField value="{!a.acc.Industry}" />
                <apex:column >
                    <apex:commandButton value="Add" action="{!add}" />

  • 1
    You can add required attribute in your inputText field like this: <apex:column headerValue="Name" > <apex:inputText value="{!a.acc.Name}" required="true"/> <apex:messages /> </apex:column> – salesforce Developer Sep 8 '18 at 15:13

Instead of a null value it is likely that the Account name is being set to an empty string. The Apex String class has methods that deal with both:

if (a.flag && String.isNotBlank(a.acc.name)) {
    // OK
} else {
    // Not OK
  • I tried the above , but still it is taking me to Account home page. Actually I have just started to learn salesforce, would appreciate your inputs. – Sumit Anand Sep 8 '18 at 15:02
  • @SumitAnand Best you edit the question to just have the code that you think is closest to working and describe exactly what is going wrong. Then others can answer based on that. If you are just starting, take the time to work through the relevant Trailheads so you've got a better understanding of what is happening when something doesn't work. – Keith C Sep 8 '18 at 15:22
  • @SumitAnand Your code is working. It inserted the record, then took you to the account home page /001/o. It did exactly what you told it to do. – sfdcfox Sep 8 '18 at 16:30
  • @KeithC Got it...from next time will post only relevant code...yes, I am studying and practicing from Trailheads and challenges... thanks for the help :) – Sumit Anand Sep 9 '18 at 16:28
  • @sfdcfox yes...It clicked to me ....before taking to home page I added a condition to check size of Accounts wrapper ..the it worked fine... thanks for the help :) – Sumit Anand Sep 9 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.