1

Im trying to create a visualforce page and do some validations on it. If my first name and last name are same then it should throw me an error ..or else it should insert the record.When i tried running the code,its giving me this error even though I'm entering "Last Name". Could someone figure this out?

public class perinfo {
    public contact Cont {get;set;}
    public String fname {get;set;}
    public String lname {get;set;}
    public String email {get;set;}
    public String phone {get;set;}
    public perinfo() {
        fname = '';
        lname = '';
        email = '';
        Cont = new Contact();
    }
    public void Submitbutton() {
        if (fname == lname)
            apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.error, 'FirstName and  LastName cannot be same'));
        else
            insert cont;
    }
}
  • Can you post your Visualforce page? – Alex Tennant Aug 1 '14 at 10:25
  • 1
    Purely out of interest, is there any reason you aren't using a validation rule for this? – Simon Lawrence Aug 1 '14 at 11:43
  • Agree with @simon, seems like a validation rule would save a lot of unnecessary time and effort. Unless I am missing something. – Chris Duncombe Aug 1 '14 at 12:23
3

Ravi,

believe you are binding the Last Name field in your visualforce page to the lname string variable instead of the Contact Last Name field (infact i believe you are binding all the ui fieds to the variables you declared in your controller).

so when your submitButton method is called, its inserting an empty contact and you are getting this error since Salesforce expects LastName field for the contact.

if you remove the variables and directly bind your visualforce page inputFields to Cont fields, it should work

public class perinfo {
    public contact Cont {get;set;}
    public perinfo() {
        Cont = new Contact();
    }
    public void Submitbutton() {
        if (Cont.FirstName == Cont.LastName)
            apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.error, 'FirstName and  LastName cannot be same'));
        else
            insert Cont;
    }
}

or before inserting the new Contact you have to assign the variables to your Contact fields

public void Submitbutton() {
        if (fname == lname)
            apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.error, 'FirstName and  LastName cannot be same'));
        else{
            Cont.FirstName = fname;
            Cont.LastName = lname;
            insert Cont;
        }
    }
  • 1
    This sounds like a very likely solution. – Simon Lawrence Aug 1 '14 at 11:43
0
public Contact con{get; set;}

public your_constructor()
{
  con= new Contact();
}

public Pagereference save() //this should be your action method which you will link with command button in you VF page
{
  //here you will do validation
   if(con.Name == con.LastName)
         apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.error, 'FirstName and  LastName cannot be same'));

    else
    {
        Contact newContact = new Contact();
        newContact.Name = con.Name;
        newContact.LastName = con.LastName;
        insert newContact;
     }
}

In your VF page

Name : <apex:inputField value="{!con.Name}"/>
Last Name: <apex:inputField value="{!con.LastName}"/>

Hope this helps...

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.