+ Start a Discussion
Ben MalesBen Males 

Apex Class POST Method help

Hi All,

I have the below Apex code running via a button on the Contact page
public with sharing class auto_sendSMS 
{
    public String phone {get;set;}

    public auto_sendSMS(ApexPages.StandardController stdController) {
    Contact contact = (Contact)stdController.getRecord();
    contact = [SELECT Id, phone from contact where Id = :contact.Id];
    
    String username = 'apiUser';
    String password = 'z448rcqAQ7w55ySK';
    String fromID = 'Cirrus';
    String dest = contact.phone;
    String body = 'API test';
    
    String requestEndpoint = 'https://auto.cirrusresponse.com/auto_sendSMS.asp';
    requestEndpoint += '?u=' + username;
    requestEndpoint += '&p=' + password;
    requestEndpoint += '&fromID=' + fromID;
    requestEndpoint += '&dest=' + dest;
    requestEndpoint += '&body=' + body;
    
    Http http = new http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(requestEndpoint);
    request.setMethod('POST');
    HttpResponse response = http.send(request);
    
    }

}
I have set the remote site settings for the endpoint, but the request is never received.

I'd greatly appreciate if anyone can advise me where I've gone wrong in the above as the non-Apex version of the code is running sucessfully through Postman.

Kind regards
Jakub Kołodziej 7Jakub Kołodziej 7
Could you try below code or just check resposne via Developer Console?
public with sharing class auto_sendSMS 
{
    public String phone {get;set;}

    public auto_sendSMS(ApexPages.StandardController stdController) {
    Contact contact = (Contact)stdController.getRecord();
    contact = [SELECT Id, phone from contact where Id = :contact.Id];
    
    String username = 'apiUser';
    String password = 'z448rcqAQ7w55ySK';
    String fromID = 'Cirrus';
    String dest = contact.phone;
    String body = 'API test';
    
    String endpoint = 'https://auto.cirrusresponse.com/auto_sendSMS.asp?';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setbody('u=' + username + '&p=' + password + '&fromId=' +  fromId + '&dest=' + dest + '&body =' + body);
    Http http = new http();
    HTTPResponse response = http.send(req);

                 if(response.getStatusCode() != 201) {
                 System.debug('Unexpected response from web service, expecte response status status 201 but got ' +                 response.getStatusCode());
                 System.debug('Response from Server in Salesforce: '+ response.getBodyAsBlob().toString());
                 }
    }

}

 
Ben MalesBen Males
Thanks Jakub,

I have tried with the amended code and the result is the same.

If I take the code and execute via anonymous window there is nothing shown under the Debug only option either.

In the Visualforce page I have tried to add the variables to see if they are returned as expected and I only see the variable name, not the value - not sure if this would have an impact on the Apex code executing however?

User-added image

Kind regards
Jakub Kołodziej 7Jakub Kołodziej 7
Below code worked for me:
public class auto_sendSMS
{
    public auto_sendSMS(ApexPages.StandardController stdController) {
    mycallout();
}

 @future(callout=true)
   public static void myCallout(){
    for(Contact c: [Select Phone from Contact where Id =:ApexPages.currentPage().getParameters().get('id')])
    {  
    String username = 'apiUser';
    String password = 'z448rcqAQ7w55ySK';
    String fromID = 'Cirrus';
    String dest= c.phone;
    String body = 'API test';
    
    String endpoint = 'https://auto.cirrusresponse.com/auto_sendSMS.asp?';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setbody('u=' + username + '&p=' + password + '&fromId=' +  fromId + '&dest=' + dest + '&body =' + body);
    Http http = new http();
    HTTPResponse response = http.send(req);

                 if(response.getStatusCode() != 201) {
                 System.debug('Unexpected response from web service, expecte response status status 201 but got ' +                 response.getStatusCode());
                 System.debug('Response from Server in Salesforce: '+ response.getBodyAsBlob().toString());
                 }
    }
}
}

Make sure your button looks like that  /apex/vfpname?id={!Contact.Id}
Ben MalesBen Males
Thanks Jakub,

Make sure your button looks like that  /apex/vfpname?id={!Contact.Id}

Is this a setting I need add to the custom button itself? I looked at the button, but couldn't see where to add this code.

The button sits on the Contact record as below

User-added image

Kind regards
Jakub Kołodziej 7Jakub Kołodziej 7
Your custom button "SMS" how does it look like?
That's custom contact deatil page button with link to visualforce page in code yes?
Ben MalesBen Males
Hi Jakub,

Correct, this is a custom button with a link to the Visualforce page as per the below:

User-added image

Kind regards
Jakub Kołodziej 7Jakub Kołodziej 7
Change content source to URL and paste in formula /apex/auto_sendSMS?id={!Contact.Id}
Ben MalesBen Males
Thanks Jakub,

Visualforce Error
System.LimitException: @future call currently not allowed
Class.auto_sendSMS.<init>: line 4, column 1

Currently receiving the above error with the revised code and modified page.

Kind regards
Ben MalesBen Males
The enable profile on the Apex class is the same as that of the User trying to execute the code so not sure if I need to make any further changes?
Ben MalesBen Males
If anyone can advise on the below Visualforc error that'd be appreciated.

Visualforce Error
System.LimitException: @future call currently not allowed
Class.auto_sendSMS.<init>: line 4, column 1

Thanks