You need to sign in to do that
Don't have an account?
SNNair
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
<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
To access those fields in apex you do something like this: 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
It must look like the following:
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 "/>".
"<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:
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>
======================
Hope it helps. Happy weekend. :)
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?
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.
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
To access those fields in apex you do something like this: 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.
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?
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