1

I want to concatenate 'input' variable in the Endpoint URL.
If i am passing hard-coded value like 'London', its working properly.(I am getting weather in JSON format back).
I want to do it dynamically.(Value entered through Bot should be set in URL)
Currently this code isnt working? Can you please help me out with this?


public class BotAction {

     public class ChatBotTempInput{
        @InvocableVariable(required= true)
        public String City ;
    }

    @InvocableMethod(Label ='Do Callout' description='does a callout')
    public static List<String> DoCallout(List<ChatBotTempInput> input){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather?q='+input+'&APPID=######################');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            return new List<String> {response.getBody()};
                }
        return new List<String> {'Test'};
            }
}
  • Have you debug and check whether input variable is getting value you passed. – Reshma Dec 10 '18 at 7:54
  • @Reshma I found the solution. I did not use Invocable variable, but instead used normal List<String> input. And it worked. But when i try to handle the response the Bot fails.? Do you know how to handle response in Chatbot class? – user62549 Dec 10 '18 at 9:22
1

I found the solution:

public class BotAction {
    @InvocableMethod(Label ='Do Callout' description='does a callout')
    public static List<String> DoCallout(List<String> input){
        String city = input.get(0);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather?q='+city+'&APPID=#####################');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            return new List<String> {response.getBody()};
        }
        return new List<String> {'Test'};
    }
}

I did following changes -

1.Removed Invocable variable, instead used normal List as input parameter. 2.Stored 1st element of List in String var. 3.Concatenated this String var in endPoint URL.

  • 1
    As is, this answer is what we call a "code dump". A code dump is where you provide a block of code, but do not provide any explanation as to what it does or how it works. Code dumps tend to be low-quality answers. When you aren't answering your own question, such answers tend to generate a lot of back-and-forth in the comments like "that didn't work" and "try this instead". I like to keep in mind that other people with similar problems will be visiting this answer to try to help solve their own issues. – Derek F Dec 11 '18 at 12:25
  • 1
    This answer would be greatly improved if you were to explain what this code does and, perhaps most importantly, how it's different from the code that you first tried. – Derek F Dec 11 '18 at 12:27
  • @DerekF Sure, i will edit the answer with explanation!! – user62549 Dec 12 '18 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy