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
SNNairSNNair 

Apex code execution using visualforce page with button click

Visualforce:

<apex:page standardController="Lead" extensions="testthustd" >

</apex:page>

Apex code

---
public with sharing class testthustd
{

public testthustd(ApexPages.StandardController stdController)


{


HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();

req.setEndpoint('https://goodle.com/api/v2/job/14/launch');
req.setMethod('POST');

req.setCompressed(false);
//req.setBody('key1=value1&key2=value2');
req.setHeader('Content-type', 'application/json'); 
req.setHeader('Authorization', 'Bearer IYGIUHYwfwifuhwufhoGIYHOIVU'); 
req.setHeader('Content-type', 'application/json'); 



try {
res = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
}
System.debug(res.getBody());


}


}

This is not working.  Please help
Best Answer chosen by SNNair
Christian Schwabe (x)Christian Schwabe (x)
If you want to use a field from your current record you should use ApexPages.StandardController, like you already to.
To access those fields in apex you do something like this:
public with sharing class testthustd
{
	private final Lead lead;
	
	public testthustd(ApexPages.StandardController standardController){
		this.lead = (Lead)standardController.getRecord();	
	}
}
Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardcontroller.htm


P.S.: If I answer your question well, please mark it as "best answer". Thanks.

All Answers

Christian Schwabe (x)Christian Schwabe (x)
You missed the action-parameter in visualforce-page.

It must look like the following:
<apex:page extensions="testthustd"
    standardController="Lead"
    action="{!<Methodname>}"
>
</apex:page>

 
SNNairSNNair
Hi Chris,

Thanks for the udpate.

I am getting below error while saving it. Should I replace {!<Methodname>} this with some value or am I mising something else. I am a newbie to salesforce.

Error: testthustd line 1, column 41: Element type "apex:page" must be followed by either attribute specifications, ">" or "/>"
Error: Element type "apex:page" must be followed by either attribute specifications, ">" or "/>".

Christian Schwabe (x)Christian Schwabe (x)
Sorry for my inadequate description.
"<Methodname>" (without doublequotes) should be replaced with the name of a apex-method.

You should move your content out of your constructor and create a new apex-method i.e. public void doIt(){ ... } then the example looks like the following:
 
<apex:page extensions="testthustd"
    standardController="Lead"
    action="{!doIt}"
>
</apex:page>

 
SNNairSNNair
Hi Chris,

I have updated as you suggested. Please let me know if I am wrong.

=====================
public with sharing class testthustd
{
  public testthustd(ApexPages.StandardController stdController)


  {}

  public void doIt(){ 

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://goodle.com/api/v2/job/14/launch');
req.setMethod('POST');
req.setCompressed(false);
//req.setBody('key1=value1&key2=value2');
req.setHeader('Content-type', 'application/json');  
req.setHeader('Authorization', 'Bearer IYGIUHYwfwifuhwufhoGIYHOIVU');  
req.setHeader('Content-type', 'application/json');  

try {
    res = http.send(req);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
}
System.debug(res.getBody());
}
}

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

<apex:page extensions="testthustd" standardController="Lead" action="{!doIt}">

</apex:page>

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


 
Christian Schwabe (x)Christian Schwabe (x)
Yes, it seems correct. It's not easy to read your code because you don't use "code sample" function in the richt text editor with syntax highlightning. Now you can include your visualforce-page in a quickaction. After clicking on the button your code is executed.

Hope it helps. Happy weekend. :)
SNNairSNNair
=====================
public with sharing class testthustd
{
  public testthustd(ApexPages.StandardController stdController)


  {}

  public void doIt(){ 

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://goodle.com/api/v2/job/14/launch');
req.setMethod('POST');
req.setCompressed(false);
//req.setBody('key1=value1&key2=value2');
req.setHeader('Content-type', 'application/json');  
req.setHeader('Authorization', 'Bearer IYGIUHYwfwifuhwufhoGIYHOIVU');  
req.setHeader('Content-type', 'application/json');  

try {
    res = http.send(req);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
}
System.debug(res.getBody());
}
}

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

<apex:page extensions="testthustd" standardController="Lead" action="{!doIt}">

</apex:page>

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

Thanks for the help Chris,  I have added it but for some reason the remote api is not triggered while hitting button. I have added CORD and remote site for the url. Anything else I have missed?
Christian Schwabe (x)Christian Schwabe (x)
Hi,

I have made it myself on my on org. Everything looks good and the endpoint is tried to called, except that one I have not configured the remote site settings. That brings me to the next level of question after the implementation is proofed and correctly. Did you have configured your remote site settings? Here is the article how to do it correctly: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_remote_site_settings.htm

In shortterms: You need to allow this endpoint during the same way you do portforwarding in a router. If you did not configured this an error message in your debug log appears and looks somethine like this:

08:49:06.0 (12526612)|EXCEPTION_THROWN|[19]|System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://goodle.com/api/v2/job/14/launch

Everytime it is a good advice to active your debug logs if something does not qork in the way of your expactation.
SNNairSNNair
HI Chris,

Thanks for the help. The setup is working fine now. I was using http in the code.  I have one more question.

How to call a value for sending it as body.

csk_url_id is a field which I created and it saves some URL. I want that url to be replaced with Account specific value. Account Id i can get from same page or URL
req.setBody('{"extra_vars": {"csk_url_id":"someurl.domain.com"}}');
r

 
Christian Schwabe (x)Christian Schwabe (x)
If you want to use a field from your current record you should use ApexPages.StandardController, like you already to.
To access those fields in apex you do something like this:
public with sharing class testthustd
{
	private final Lead lead;
	
	public testthustd(ApexPages.StandardController standardController){
		this.lead = (Lead)standardController.getRecord();	
	}
}
Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardcontroller.htm


P.S.: If I answer your question well, please mark it as "best answer". Thanks.
This was selected as the best answer
SNNairSNNair
HI Chris, 

It working now. Thanks . One final question.  What if I want to use use same button for both Lead and Account. Here I have mentioned "standardController="Lead"" .  Should I have to use 2 button >> seperate visualforce page and underlying class for Lead and Contact?
<apex:page extensions="testthustd" standardController="Lead" action="{!doIt}">

</apex:page>

 
Christian Schwabe (x)Christian Schwabe (x)
For this case you should use two different visualforce-pages,two different buttins, but use the same apexclass (extension).
In the apexclass you could differ between these two buttons / records, based on the object prefix of the record Id.

Here are some examples how to do it:
https://help.salesforce.com/articleView?id=000340200&language=en_US&type=1