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
Prashant Ranjan 12Prashant Ranjan 12 

Custom field on standard object

I want to create the custom field on a standard object using apex from the user interface i.e. Visualforce.
My requirement is this custom field(city,country,state and zipcode) on standard object contact.
Karthik jKarthik j
Hey Prashant,
Greetings!
I written code for your requirement. for this you need to create 4 custom objects city__c,country__C,state__c,zipcode__c for contact object and one more thing you need to take LastName field because which is required field in contact object.

APEX CODE:
public class Contactapexcontroller {
public contact con{get;set;}
    public ApexPages.StandardController controller;
    public Contactapexcontroller(ApexPages.StandardController controller) 
    { 
        this.controller = controller;
        String Idacc = ApexPages.CurrentPage().getparameters().get('id');
        if(Idacc != null){
            con = [select LastName,city__c,country__c,state__c,zipcode__c from contact where id =: Idacc];
        }
        else{
            con = new contact();
        }
    }
    public PageReference saveMethod(){ 
        insert con;
        return new PageReference('/' +con.Id);
    }
}

VF PAGE:
<apex:page standardController="contact" extensions="Contactapexcontroller" >
    <apex:form >
        <apex:pageBlock title="My Content" mode="save">
            <apex:pageBlockSection title="My Content Section" columns="1">
                <apex:inputField value="{!con.LastName}"/>
                <apex:inputField value="{!con.city__c}"/>
                <apex:inputField value="{!con.state__c}"/>
                <apex:inputField value="{!con.country__C}"/>
                <apex:inputField value="{!con.zipcode__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons > 
                <apex:commandButton value="Save" action="{!saveMethod}" id="saveButton" />
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" />
            </apex:pageBlockButtons> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark it as best answer if it helps,Thank you.
Prashant Ranjan 12Prashant Ranjan 12
Hey Karthik it works for me.
Thanks
Prashant Ranjan 12Prashant Ranjan 12
where id = Idacc; This is for a particular id of contact