+ Start a Discussion
Nurav1982Nurav1982 

Calling apex code via button click

Hello All,

I have this following requirement


a) A custom button to be exposed

b) Upon clicking the button, I need to verify whether the current record is already present in a custom object

c) If the match is found I need to call TIBCO

d) If the match is not found, then I need to popup a message

Can someone point me in a direction as to how to proceed ?.

Thanks for your time !!!
Best Answer chosen by Nurav1982
suyog dongaonkarsuyog dongaonkar
Nurav1982,
You can use Ajax Toolkit provided by Salesforce for your purpose. The AJAX Toolkit is a JavaScript wrapper around the API and can be used instead of VF and Apex.
1. Create a custom button which executes JavaScript on its click.
2. Use Ajax Toolkit to perform the operations. You can perform SOQL and DML operation in Ajax Toolkit.

All Answers

RoyGiladRoyGilad
Follow these steps and review the links for more details...
  1. Create a Visualforce page, using the 'standardController (http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std_associate.htm)' and 'extensions (http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm)' attributes on apex:page (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm)
  2. Create a Custom Button (http://na11.salesforce.com/help/doc/en/defining_custom_links.htm) using the Visualforce page as its Content Source (http://na11.salesforce.com/help/doc/en/defining_custom_links_fields.htm)
  3. Add the Custom Button to the appropriate Layout of the object
  4. Use either the 'action (http://www.salesforce.com/us/developer/docs/pages/)' attribute or apex:commandButton (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_commandButton.htm)'s on your page to invoke Apex logic.
Use of action attribte on apex:page. If you use the 'action' attribute as per step 4 your Apex code will execute as soon as the Custom Button is pressed. However if your Apex code performs database updates this is considered unsecured as its possible that your code will be open to a CSRF attack. See this excellent topic from Salesforce for more information. If this is your case its better to use the apex:commandButton option and provide a confirmation button to your user before invoking your Apex code.
Nurav1982Nurav1982
Hi,

Thanks for your reply.
The tricky part is that I need to do this(expose button/functionality) on a non-VF page.

Another interesting feature is, in this existing non-VF page we do have custom buttons available.

A good example is that of a button that takes the ownership of record.

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}
try
{
var vOb = new sforce.SObject("Case");
vOb.Id = '{!Case.Id}';
vOb.OwnerId='{!$User.Id}';
var vresult = sforce.connection.update([vOb]);
if (vresult[0].getBoolean("success"))
window.location.href=window.location.href;
else{
var err = vresult[0].errors;
alert ('Cannot be updated as : '+err.message);
}
}
catch(e)
{
alert (e.toString());
window.location.href=window.location.href
}

Cannot I do something on this vein instead of creating a brand new VF page ?
suyog dongaonkarsuyog dongaonkar
Nurav1982,
You can use Ajax Toolkit provided by Salesforce for your purpose. The AJAX Toolkit is a JavaScript wrapper around the API and can be used instead of VF and Apex.
1. Create a custom button which executes JavaScript on its click.
2. Use Ajax Toolkit to perform the operations. You can perform SOQL and DML operation in Ajax Toolkit.
This was selected as the best answer