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
Ravi kumar 292Ravi kumar 292 

How to hit the URL

Hi all,

I want to hit the URL(http://test.com/callback.ashx?lead_id=ld121212&contact_id=234234&application_id=IL2342343&status=Approved&amount=100000) when i click on a custom button. How can i do this. Please help.

Thanks 
Shaijan ThomasShaijan Thomas
Hi

can you try this https://success.salesforce.com/answers?id=90630000000hBg6AAE
Ravi kumar 292Ravi kumar 292
Hi Thomas,

Thanks for your reply.

But I have to send a post request to some other server from apex controller and get the response back. 

Help on this..
MUJJAMMIL KHANMUJJAMMIL KHAN
HI Ravi,
You can refer to following link to execute Apex methode from javascript which will execute when user will click on Custom button.
You have to declare your class as global and your methode as Webservice.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm
Ravi kumar 292Ravi kumar 292
Hi,

This is my APex class code:

public with sharing class LDController {
    String LeadId;
    String Status;
    Decimal Amount;
    String ApplicationId;
    
    public LoanDecisionController(ApexPages.StandardController controller) {
        LeadId=ApexPages.currentPage().getParameters().get('id'); 
        Lead l =[select id,LastName,Amount_in_Rs__c,Status,Application_Id__c from Lead where id=:LeadId];
        system.debug('Lead id--'+l.id); 
        List<Contact> c = [select id,FirstName,LastName from Contact where LastName=:l.LastName];
        system.debug('contact id --'+c[0].id);
        
        Status = l.Status;
        Amount =l.Amount_in_Rs__c;
        ApplicationId =l.Application_Id__c;
        
    }
    public Void CallBack(){
         String body ='LeadId='+LeadId+'&Status='+Status+'&Amount='+Amount+'&ApplicationID='+ApplicationId+'';
         system.debug('------'+body);
        // Get the XML document from the external server
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        
        req.setEndpoint('http://test/callback.ashx');
        req.setMethod('POST');
        req.setbody(body);
          
        try{
             res = http.send(req);
             String response=res.getBody();
             System.debug('Response '+response);
           }catch(System.CalloutException e) {
               System.debug('Callout error: '+ e);
               }
         }
    }

When i hit the Endpoint URL with Leadid,Status,Amount and ApplicationID then i will get a mail from client side that i am hitting their URL. But now am not getting any mail from their end. Please check the code and tel me if i have change the code.

Thanks..  
MUJJAMMIL KHANMUJJAMMIL KHAN
I think you should use following code for endpoint url and Method as GET as you are hitting URL with Query parameters.

req.setEndpoint('http://test/callback.ashx' + body );
req.setMethod('GET');
Ravi kumar 292Ravi kumar 292
Hi Khan,

I did the same but am not getting any mail from their end. 
I have one doubt please clarify it. In order to hit the client URL do we need to pass any username and password??

 
MUJJAMMIL KHANMUJJAMMIL KHAN
Yes you need some Authentication mechanism from which the client will recognize the request that are coming are coming from legtimate user.
There are different Authentication mechanisms like oAuth 2.0 or UserName Password based authentication etc.
Ravi kumar 292Ravi kumar 292
Thanks.. If they give me the Username and Password then where should i write them in my code. Could you please tel me.