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
mritzmritz 

How to save record in Account Object Using VisualForce Page

I want to create a VisualFroce page having input fields in it. I want this page to enable Guest User to enter new record in Account Object( or any other Object).

I want headStart on how to write code for Save/Search/Edit/Delete Functions on a visulaForce page.
(Assume that i have created required input fields on visualFroce Page)
 
I can do this using Flow but i want to acheive this using VisualForce Page.

Even a link to similar solved question would do.

PS: Salesforce is new to me.

Best Answer chosen by mritz
Tejpal KumawatTejpal Kumawat
Hello Friend,

You can do it without sharing class/permission set with Account read, create, edit permission

try this  without sharing class:
 
public without sharing class AccountController {
	public Account account{get; set;}
	public AccountController(){
		account = new Account();
	}
	
	public void save(){
		upsert account;
	}
}
 
<apex:page controller="AccountController">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
If this answers your question then hit Like and mark it as solution!

 

All Answers

Tejpal KumawatTejpal Kumawat
Hello Friend,

Try this :
<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
If this answers your question then hit Like and mark it as solution!
 
mritzmritz
Tejpal,

Will it work without user account, i mean can a guest user save records using this code embedded in "site". or do i need to tweak user permissions somewhere.
fundooMentalfundooMental
Did you give correct permisssions in the Public Access setting? If yes then yes the code above would work
Tejpal KumawatTejpal Kumawat
Hello Friend,

You can do it without sharing class/permission set with Account read, create, edit permission

try this  without sharing class:
 
public without sharing class AccountController {
	public Account account{get; set;}
	public AccountController(){
		account = new Account();
	}
	
	public void save(){
		upsert account;
	}
}
 
<apex:page controller="AccountController">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
If this answers your question then hit Like and mark it as solution!

 
This was selected as the best answer
fundooMentalfundooMental
No. I am not talking about sharing rules or such. I am talking about Public Access settings for a site. Public Access Setting let us modify the profile for guest user for the site and the guest user should have correct permissions here for an object to modify (create, edit etc) the records on that object
fundooMentalfundooMental
My answers are based upon the assumption that you are using this visualforce page in the site created through Force.com and through this public site you want to save records on the account object
Tejpal KumawatTejpal Kumawat
Hello fundooMental,

Yes we can provide the Public Access settings to profile for guest user for the site, Account (Read & Create only).

If this answers your question then hit Like and mark it as solution!
fundooMentalfundooMental

Yes. From the question I though m ritzi 9 is talking about Force.com site. If not for site then the code you have written would perfectly work without bothering about sharing rules and user permissions using the custom controller as they operate in system mode and ignores the user permission and sharing rules etc.

m ritzi 9 - Please clarify if you are looking to update/insert/delete from Force.com public site or in the salesforce only?

mritzmritz
correct fondooMental
I am talking about Force.com Sites.

I want that a person without having login access to salesforce can create new records in Account Object.
fundooMentalfundooMental

OK. Thanks for clarifying. As I said YES this is possible that "a person without having login access to salesforce can create new records in Account Object." . First write your visualforce page and if required controller for that and then in the Public Access Setting of the Site just give the Read and Create permission for the guest user license.

mritzmritz
One final Clarification before i close this question.

How to i enable users to edit and delete there details from Account Object again without "loggin in".
fundooMentalfundooMental
There details from Account Object means?
mritzmritz
Say a user/Org has saved his/its info into Account Object, and now its required to make some changes or delete that particular record.
How can i acheive this using visual force page.
fundooMentalfundooMental

See you can save, edit, create records through salesforce site but like you want a user to see only some specific  Account records like of his/her own then you have to write the Visualforce accordingly. Like in the Controller class you can use Userinfo.getUserId() to get the Id of the currently logged in user and based upon that you can expose records. So that logic you have to figure out how do you want.

The baseline is, for Guest Profile first you need to give the proper Object level permissions to let the user see/edit etc the records and then in the VF page and Controller that you create there you have to figure out which records to show and how etc.

Let us know if you need more help.

Hema Rama Krishna KadamatiHema Rama Krishna Kadamati

I tried this code and it is working for Standard Objects but it is not working for Custom Objects. In this code the record is getting inserted but it is inserting with some random ID as name such as a0T28000000TsyE,etc. Can anyone provide me the solution for Custom Objects.
Visualforce code:
<apex:page standardController="Player__c" extensions="all_player">
  <apex:form >
  <apex:pageBlock title="Player Information">
  <apex:pageBlockSection >
  <apex:inputField value="{!Player__c.Name}"/>
    <apex:inputField value="{!Player__c.Club_Captain__c}"/>
      <apex:inputField value="{!Player__c.Jersey_Number__c}"/>
        <apex:inputField value="{!Player__c.team1__c}"/>
  </apex:pageBlockSection>
  <apex:pageBlockButtons location="top">
  <apex:commandButton value="Save" action="{!save}"/>
    <apex:commandButton value="Cancel" action="{!cancel}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Class code:
public class all_player 
{
    public Player__c t1{get;set;}
    public all_player(ApexPages.StandardController controller) 
    {
        t1= new Player__c();
    }
    public PageReference save()
    {
        insert t1;
        PageReference p=new PageReference('/a0T/o');
        return p;
    }
}