+ Start a Discussion
Michael Hedrick 2Michael Hedrick 2 

Calling web service is this the correct approach

Hello All,
I need to call a web service from Salesforce. So I have been informed that the end point url will need to look like this with parameter.
GET /abc/def.asmx/ghi?emailTo=string&firstName=string&lastName=string&leadType=string&activityType=string&agree=string&source=string&status=string&streetAddress=string&streetAddress2=string&city=string&state=string&zip=string&country=string&sendsEmail=string&emailFrom=string&activitySubject
My cass looks like this:
public class CallTrexWebServiceInsideSales
{
    Lead leads;
    string message;
        
     {
        message = '';
        leads = [SELECT Email, FirstName , LastName , RecordTypeId, city,  FROM Lead WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
            }
        }


    public String myresponse{get;set;}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        
        String ActivityType = 'Sales';
        string Subject = 'test;
        
        
        String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}

I knowall of the required fields are not in the string url but I was not sure if I approaching this correctly.  I am trying to send data to the webservice and I am not returning anything.  Any help is appreciated.

Cheers,
M

 
Best Answer chosen by Michael Hedrick 2
Amit Singh 1Amit Singh 1
yes, you are on the right track. Before making the callout add the Endpoint into the Remote Site Setting. Also, I have modified the class which is given below
public class CallTrexWebServiceInsideSales
{
    public Lead leads {get;set;};
    public string message {get; set;}
    public String myresponse{get;set;}
	
    public CallTrexWebServiceInsideSales() {
        message = '';
        leads = [SELECT Email, FirstName , LastName , RecordTypeId, city,  FROM Lead WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
            }
	}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        
        String ActivityType = 'Sales';
        string Subject = 'test;
        
        
        String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}

Regards,
Amit 

All Answers

Amit Singh 1Amit Singh 1
yes, you are on the right track. Before making the callout add the Endpoint into the Remote Site Setting. Also, I have modified the class which is given below
public class CallTrexWebServiceInsideSales
{
    public Lead leads {get;set;};
    public string message {get; set;}
    public String myresponse{get;set;}
	
    public CallTrexWebServiceInsideSales() {
        message = '';
        leads = [SELECT Email, FirstName , LastName , RecordTypeId, city,  FROM Lead WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
            }
	}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        
        String ActivityType = 'Sales';
        string Subject = 'test;
        
        
        String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}

Regards,
Amit 
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Thanks Amit.  I will keep building the url string and hopefully it will work when I click the button to call the Cpex Class on the lead record.
Cheers,
M
  
Michael Hedrick 2Michael Hedrick 2
Heu Amit,
Do I have to use Webservice static void testMethod() to be able to call this class from a Detailed record button?
Thanks,
M
Amit Singh 1Amit Singh 1
If you want to call the method from the JavaScript then you need to declare Webservice static with the returnType and the method Name.
Michael Hedrick 2Michael Hedrick 2
Amit. I said JS button becasue that is what I have read.  Would using a VF page be better.  I had to modify the code to get it to save without errors.  It did not see the eads or message variable.  even with this change I keep getting an error on button click.
Global class CallTrexWebServiceInsideSales
{
    public Lead leads {get;set;}
    public string message {get; set;}
    public String myresponse{get;set;}
    
    webservice static void CallTrexWebServiceInsideSales() {
        string message = '';
        Lead leads = [SELECT Email, FirstName , LastName , RecordTypeId, city  FROM Lead WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) 
           {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
            }
    }
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        
        String ActivityType = 'Sales';
        string Subject = 'Trexpro and Dealer Locations sent to Consumer ';
              
        String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}
Michael Hedrick 2Michael Hedrick 2
Amit,
I am getting the following errro:
11:30:53:001 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object
11:30:53:000 FATAL_ERROR Class.CallTrexWebServiceInsideSales.TrexWebServiceInsideSales: line 9, column 1
11:30:53:001 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I can see that I am getting the Id of the Lead so I ma not sure what the error is referencing

Class:
Global class CallTrexWebServiceInsideSales
{
    public Lead leads {get;set;}
  //  public string message {get; set;}
    public String myresponse{get;set;}
    
    webservice static void TrexWebServiceInsideSales(String id) {
        string message = '';
        Lead leads = [SELECT Email, FirstName , LastName , RecordTypeId, city  FROM Lead WHERE id = :ApexPages.currentPage().getParameters().get('Lead.Id')];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) 
           {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
            }
    }
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        
        String ActivityType = 'Sales';
        string Subject = 'Trexpro and Dealer Locations sent to Consumer ';
              
        String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}

Button:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

{
   var retStr; 
   retStr = sforce.apex.execute("CallTrexWebServiceInsideSales", "TrexWebServiceInsideSales", {id:'{!Lead.Id}'}); 

}

 
Amit Singh 1Amit Singh 1
You need to pass the Parameters in the method which you are calling from the javascript.
Refer below link for more information
http://blog.shivanathd.com/2014/07/call-apex-class-from-custom-button-salesforce.html
Michael Hedrick 2Michael Hedrick 2
ok so the JS should look like this?
retStr = sforce.apex.execute("CallTrexWebServiceInsideSales", "TrexWebServiceInsideSales", {emailTo:lead.email,leadType:lead.RecordTypeId.....});

Basically I need to follow the string values required by the web service:
GET /abc/def.asmx/ghi?emailTo=string&firstName=string&lastName=string&leadType=string&activityType=string&agree=string&source=string&status=string&streetAddress=string&streetAddress2=string&city=string&state=string&zip=string&country=string&sendsEmail=string&emailFrom=string&activitySubject

 
Amit Singh 1Amit Singh 1
No, Just send the Id From the button and then Query in the Controller and then in the controller build the URL. Use below class
Global class CallTrexWebServiceInsideSales{
    public Lead leads {get;set;}
  //  public string message {get; set;}
    public String myresponse{get;set;}
    
    webservice static void TrexWebServiceInsideSales(String id) {
        string message = '';
        Lead leads = [SELECT Email, FirstName , LastName , RecordTypeId, city  FROM Lead WHERE id =:id];
        if(String.isEmpty(leads.email) || String.isEmpty(leads.Account_Type__c)) {
            message = 'This lead is either missing an email address or the Account Type field is NULL.' ; 
        }else{
			HttpRequest req = new HttpRequest();
			Http http = new Http();
			req.setMethod('GET');
			
			String ActivityType = 'Sales';
			string Subject = 'Trexpro and Dealer Locations sent to Consumer ';
				  
			String url = 'https://abc/def.asmx/ghi?'+leads+ActivityType +Subject;
			req.setEndpoint(url);
			HTTPResponse resp = http.send(req);
			myresponse=resp.getBody();
		}
    }
}

Call TrexWebServiceInsideSales method from the javascript and pass the Lead Id
 
Michael Hedrick 2Michael Hedrick 2
Thnaks Amit.  Do I need to move ' public String myresponse{get;set;}'  into webservice static void TrexWebServiceInsideSales(String id) method?  I am getting an error  of:
Error: Compile Error: Variable does not exist: myresponse at line 22 column 13
Amit Singh 1Amit Singh 1
Change 
public String myresponse{get;set;}

TO 

public static String myresponse{get;set;}

 
Michael Hedrick 2Michael Hedrick 2
Awesome Amit.  Not sure if its working but it did not blow anything up and I do not see any errors in developer console.
Now I will see if I cna put a Test class togther...
Thanks for your help and have a good weekend.
M
 
Amit Singh 1Amit Singh 1
Cheers :)