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
TsvetyTsvety 

Create a visualforce page with a custom button which updates field for a custom object

Hi everyone,

I had created a detailed page button that executes Java Scprit and now I am trying to create exactly the same button, but on a Visualforce page that I will make available for Salesforce1.
Can someone help  me write the code for the Visualforce page?

Detailed Page/JavaScript/ Custom Object - "Travel Request"

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 

var tr = new sforce.SObject("Travel_Request__c"); 
tr.id = '{!Travel_Request__c.Id}'; 
tr.Approval_Status__c ='Recalled/Cancelled'; 
var result = sforce.connection.update([tr]); 

if (result[0].success == 'false') { 
alert(result[0].errors.message); 

else { 
location.reload(true); 
}
Abhi_TripathiAbhi_Tripathi
You can create a normal visualforce page, to update the record, the code above you are using its used for the buttons which of type Javascript that why it ask you call the Salesforce JS libraries, in vf page no need of these, just create a standard controller vf page and try to use apex to update the record. All that vf page for salesforce 1
TsvetyTsvety
Abhi, thanks for replying to my question. Can you help me with the standard controller vf page? I was not sure what I should use to update the specific picklist field.
Himanshu ParasharHimanshu Parashar
Hi,

You can use following code to create your vf page.
 
<apex:page standardController="Travel_Request__c">
<apex:form>
<apex:commandbutton value="Update Record" action="{!Save}"/>
<apex:inputField value="{!Travel_Request__c.Approval_Status__c}" />
</apex:form>
</apex:page>

Please don't forget to click Available for Mobile App checkbox as shown in below screenshot.

User-added image


Thanks,
Himanshu
TsvetyTsvety
Thank you, Himanshu. Is it possible to have the Approval_Status__c automatically have a value "Cancelled" when the button is clicked?
Himanshu ParasharHimanshu Parashar
Hi Tsvety,

You can use following code

Apex Class:
/**
 * An apex page controller that exposes the save functionality
 */
public with sharing class TRController {
    
    public  public Travel_Request__c TR;
    public TRController(ApexPages.StandardController stdController) {
              this.TR = (Travel_Request__c)stdController.getRecord();
      }
    
     public void SaveTR{
        TR.Approval_Status__c='Cancelled';
        update TR;
    }    
}

VF Page:
 
<apex:page standardController="Travel_Request__c" extension="TRController">
<apex:form>
<apex:commandbutton value="Update Record" action="{!SaveTR}"/>
<apex:outputField value="{!Travel_Request__c.Approval_Status__c}" rendered="false" />
</apex:form>
</apex:page>

Thanks,
Himanshu
TsvetyTsvety
I am getting the following error for the apex class:
Error: Compile Error: Option already set: PublicIdentifier at line 6 column 12
Himanshu ParasharHimanshu Parashar
aah there is typo.. 

remove one public from line 6 

public Travel_Request__c TR;
TsvetyTsvety
Error: unexpected token: 'TR.Status__c'.
Status is a picklist if this makes a difference
Himanshu ParasharHimanshu Parashar
please check field api name
TsvetyTsvety
It seems to be correct: API Name: Status__c.
Himanshu ParasharHimanshu Parashar
then you should use Status__c instead of Approval_Status__c in your code.

Makes sense ?
TsvetyTsvety
It does make perfect sense, but even if I use TR.Status__c='Cancelled'; I still get Error: unexpected token: 'TR.Status__c'.
 
Himanshu ParasharHimanshu Parashar
can you please post screenshot of your object and apex class
TsvetyTsvety
Thank you for helping me!

User-added image

User-added image
User-added image
Himanshu ParasharHimanshu Parashar
Please post Travel_Request__c fields as well. and make sure you have setup FLS for fields
Abhi_TripathiAbhi_Tripathi
Hi 

Use below code, its for account change field and object name according to your object

Go to the link to learn more
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_std_actions.htm
 
<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>

To use the same in functionality on mobile you need to create Action, one suggestion start Learnig Salesforce on trailhead

Here is the link for mobile action module on trailhead https://developer.salesforce.com/trailhead/visualforce_mobile_salesforce1/visualforce_mobile_salesforce1_actions_global


 
TsvetyTsvety
@Himanshu - Could you please clarify exactly which fields you want to see? The field is visible on the page layout.
Himanshu ParasharHimanshu Parashar
There can be two possible reason which is causing this unexpected token issue. 1. There is no field called approval_status__c or status__c 2. Fls is missing for above field Please verify that
TsvetyTsvety
So I guess it is 2. What should be the FLS? I have it set up as Visible and Editable across all profile.
I tried wsithing Approval Status with other  fields from the object and I am getting the same error.
Himanshu ParasharHimanshu Parashar
FLS should be visibile to all. Try to replace with name field So it will be travel_request__c.name
TsvetyTsvety
Didn't work either. 
I have created a new Record Type called "Open". Can you please tell me the code for a VF to change the record type by clicking on the button?
Himanshu ParasharHimanshu Parashar
HI Tsvety,

You need to get the Recordtypeid first with following query
 
SELECT Id,Name,SobjectType FROM RecordType where sobjecttype='Travel_Request__c' and Name ='Open'
so your apex class will be
/**
 * An apex page controller that exposes the save functionality
 */
public with sharing class TRController {
    
public Travel_Request__c TR;
    public TRController(ApexPages.StandardController&nbsp;stdController) {
              this.TR = (Travel_Request__c)stdController.getRecord();
&nbsp;     }
    
     public void SaveTR{
        List<Recordtype> rt = [ SELECT Id,Name,SobjectType FROM RecordType where sobjecttype='Travel_Request__c' and Name ='Open'];
if(rt.size()>0        
TR.recordtypeid=rt[0].id;
update TR;
    }    
}

Thanks,
Himanshu