0

Controller

public class createNewContactcontroller {
    public String firstName{set;}
    public String lastName{set;}
    public String phone{set;}
    list <contact> conList;

    public pageReference Save(){
        Contact con = new Contact(firstname, lastname, phone);
        conList.add(con);
        insert conList;
        return null;
    }
}

Visualforce page

<apex:page controller="createNewContactController" >
<apex:pageBlock>
    <apex:form>
        <apex:pageBlockSection>       
            First Name <apex:inputText value="{!firstName}" />
            Last Name <apex:inputText value="{!lastName}" />
            Phone <apex:inputText value="{!phone}" />
            <apex:commandButton value="save" action="{!save}" />
        </apex:pageBlockSection> 
    </apex:form>    
</apex:pageBlock>

I am getting a compile time error:

"Type requires name=value pair construction: Contact"

Final solution

public class createNewContactcontroller {

    public String firstName{get;set;}
    public String lastName{get;set;}
    public String phone{get;set;}
    list <contact> conList;

    public createNewContactcontroller(){
       conlist=new List<contact>();
    }

    public pageReference Save(){
        Contact con = new Contact(firstname=firstname, lastname=lastname, phone=phone);
        conList.add(con);
        insert conList;
        return null; 
   }
}
1

SObject constructors use a fieldName = fieldValue syntax so an arbitrary set of fields can be set. (More conventional syntax where the position matters is very hard to use when there are tens or hundreds of possible values to set.)

So for your code:

 Contact con = new Contact(firstname = firstname, lastname = lastname, phone = phone);

should work. You will also have to have {get; set;} on each property.

But the extra fields are not really needed; you could have used a standard controller or a custom controller where you make the Contact object a public property. Then in your Visualforce:

<apex:pageBlockSection>       
    <apex:inputField value="{!Contact.firstName}"/>
    ...
  • thanks alot keith. i am a new to this , thanks for the suggestions, appreciate it – Mohit Joshi Jun 22 '17 at 18:04
  • i am getting this runtime error Attempt to de-reference a null object Error is in expression '{!save}' in component <apex:commandButton> in page createnewcontact: Class.createNewContactcontroller.Save: line 9, column 1 – Mohit Joshi Jun 22 '17 at 18:11
  • @MohitJoshi Your list isn't initialised. In this case you don't need a list so you can just do insert con;. – Keith C Jun 22 '17 at 18:20
0
        Contact con = new Contact(FirsName=firstname, LastName=lastname, Phone=phone);

here is the link to all of the Contact Api Names: Contact Api Names

0
  1. Constructors syntax: field name= value

Contact con = new Contact(firstname = firstname, lastname = lastname, phone = phone);

  1. You need to set getter & setter methods in your apex class. A getter method is used to pass variable value from controller to VF page and setter method to pass value from VF page to controller

    public String firstName{get;set;}

    public String lastName{get;set;}

    public String phone{get;set;}

Also,you don't need a conList really as you are really inserting only one contact at a time.

Code:

public class createNewContactcontroller {
public String firstName{get;set;}
public String lastName{get;set;}
public String phone{get;set;}
//list <contact> conList;

public pageReference Save(){
    Contact con = new Contact(firstname = firstname, lastname = lastname, phone = phone);
    insert con;
    return null;
}

}

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.