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
christwin123christwin123 

Call Apex Class From A Custom Button.

Could any one please let me know if we can call a FIELD UPDATE  as a function from an Apex Class

souvik9086souvik9086

VF Page

 

<apex:commandButton action="{!save}" value="Save"/>

 

Controller

 

public void save(){

//Update fields

Upsert objVar;

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

christwin123christwin123

Thank you for your reply

 

My Field Update function name is "Status Update" and its Unique Name is "Status_Update". Could you please let me know the syntax with which I can Call this

 

Will That Be something like the below .I tried it but getting errors like "Compile Error: Method does not exist or incorrect signature: Status_Update() at line 64 column 12"

 

public void save(){

//Calling The Field Update Function

Status_Update();

}

 

Please Advice

 

souvik9086souvik9086

If your method name in action is like the following

 

<apex:commandButton action="{!Status_Update}" value="Save"/>

 

Then the apex will be

 

public void Status_Update(){

UPSERT objVar;

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

christwin123christwin123

Sorry About the Confusion

 

Status_Update is the Field Update Function I create by going through SetUp--> App SetUp--> Create -->WorkFlow and Approvals --> Field Updates--> New Field Update.

 

Can we call this function inside any Apex Method for a Custom Button Or Link or any.

 

Thanks,

Christwin

 

 

 

 

 

souvik9086souvik9086

Oh man. You are using Worflow?

 

Than nothing needs to be done using apex. When you save the record it will be automatically fire the worflow and do the field update.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Prafull G.Prafull G.
Hi Chris,

You can not call the Field Updates, defined under customization within apex.
However as Souvik mentioned you can write the logic to update the same field in apex.

Thanks,
Prafull
christwin123christwin123

Oh..Ok. We just has a requirement of getting the Field Updated through a Custom Button. Thought of doing it this way.

 

Thank you Souvik and Prafull.

 

Is there a way of updating a field through a Custom Button Like

 

Object API Name.Field API Name = 'New Value';

 

 

 

 

souvik9086souvik9086

If you want simple field update then you can write the workflow rule as you did and simply edit fields values in the standard salesforce edit page and click the standard salesforce save button. Thats all you need.

 

Thanks

Prafull G.Prafull G.
You can achieve it using the ajax as well

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

var c = new sforce.SObject("<OBJECT API NAME HERE>");
c.id = "{!<OBJECT API NAME>.Id}";// put id here
c.<CUSTOM FIELD API NAME> = <VALUE HERE>;
result = sforce.connection.update([c]);

window.location.reload();
christwin123christwin123

Oh..Here we are uploading a Document  .The Document is not getting Stored in the Standard Record Edit Page instead it is getting stored in a location like "/p/attach/NoteAttach?pid="Record id" EAQ&&parentname=Name&retURL=/apex/Page&isdtp"

 

Hence after we get this Document uploaded..... through a Custom Button We want the status of a Field in the record corresponding to that Record id to be Changed to Uploaded or Something

 

Hence Thought of using the code like

 

Object API.Field API.Record id = 'Uploaded'

 

Can We do this anyway

 

Thanks,

Christwin

christwin123christwin123

Ill Try The code now Prafull

Prafull G.Prafull G.
Sorry Chris but I think I dint understand your last comments properly. However what I understood is-
The record with record id (e.g. 001xxxxxxxxxxxx) has a field "Status" (lets say). When you click the custom button the custom field "Status" should be 'Uploaded'. If the above is true then yes you can do that using the code I posted earlier.
christwin123christwin123

Sorry for the late Reply..I tried the code to update the field automatically but I get this error when I run that

"Error: FieldUpdat Compile Error: line 22:16 no viable alternative at character '"' at line 22 column 16"

 

were I have the following lines in line 22 and 23.

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

 

If I delete the line 22 I get the same error for line 23.

 

Could you please advice if anything should be added to this code or anywere.

 

Thanks,

Christwin

Prafull G.Prafull G.
Can you post your whole code here?
christwin123christwin123

This is my complete code 

 

<apex:page sidebar="false" showHeader="false" controller="Testing">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:commandButton value="Upload" action="{!FieldUpdate}"/>
<apex:outputLabel value="{!Fiel.Name__c}"></apex:outputLabel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

=============

 

public with sharing class Testing {

public COwner__c Fiel {get;set;}
public PageReference FieldUpdate() {
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var c = new sforce.SObject("<COwner__c>");
c.id = "{!<COwner__c>.a0190000006dIYd}";// put id here
c.<Name__c> = <Updated>;
result = sforce.connection.update([c]);
window.location.reload();
Fiel = [select Name__c,Id from COwner__c where Id = 'a0190000006dIYd'];
return null;
}

 

}

 

Thank you,

Christwin

 

 

 

Prafull G.Prafull G.
Hi Chris,

Actually you mixed two ways to update field-

First-
This is pure javascript. You need to create a custom button and add the following code to
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var c = new sforce.SObject("<COwner__c>");
c.id = "{!<COwner__c>.a0190000006dIYd}";// put id here
c.<Name__c> = <Updated>;
result = sforce.connection.update([c]);
window.location.reload();

Let me know if you want me to explain the other way i.e. call apex class and do the update logic there.
christwin123christwin123

Hi Prafull,

 

I tried by giving the code in the Apex page itself but I get some error like  " Syntax error. Found '<' ".

 

Could you kindly let me know how to use that in Apec Class. I will also try that the other way.

 

Thank you so much for you support.

 

 

Christwin

Prafull G.Prafull G.
The above code should be on custom button content. NOT on visualforce page.