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
Soumya sri yaravaSoumya sri yarava 

how to reuse the Rest api callout code in salesforce

 hi, guys i have around 50 to 60 callouts with same code only Endpoint will changes in every method,  so how to reuse the same set of http code in all the methods, any solution for this will be apperiacated for the code re useability.. 
 
public void method1(){

  Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}

}


public void method2(){

  Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/countries');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'countries' key as a list
    List<Object> countries= (List<Object>) results.get('countries');
    System.debug('Received the following countries:');
    for (Object country: countries) {
        System.debug(country);
    }
}

}

Note: how to reuse the same code in all the methods arount 50 methods, only the endpoint will changes here .. 

Thanks and regards
Soumya sree 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sowmya,

I think you can have a simple method that could have a parameter as the endpoint and this endpoint can be used in the code snippet and it could be something as below:

Public void CommonMethod(String Endpoint)
{
......
request.setEndpoint(Endpoint);
.......
}

Handle the response JSON, below are few examples:

>> https://opfocus.com/json-deserialization-techniques-in-salesforce/

>> https://www.ktema.org/2020/04/17/json-apex-intro/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
madhav sharma 1madhav sharma 1
Everything You Need To Know About Outbound Dialers

Dialer, by definition, is a device or software for making calls. However, today's dialers are also capable of handling other modes of communication, such as email, text, and voice mail. Some are even advanced enough to synchronize communication on social media platforms.
Plus, dialers don't just make calls. When transitioning to mixed call centers, it can also receive calls. Thus, dialers that are specifically optimized for making outgoing calls are called outgoing dialers.
In this blog, we'll list everything you need to know about outgoing dialers. First, let's do a brief definition before moving on to other details.

What is an outgoing dialer?
An outgoing dialer is an example of Computer Telephony Integration (CTI) that is used to place calls, typically to reach prospects or provide customer service. CTI is a type of technology used to integrate computers and phone systems into call centers to increase efficiency and reduce costs.
Call centers need automation to make high volumes of outgoing calls. Automating call operations minimizes employee stress while earning more revenue. Using an outgoing dialer has two benefits: improving the performance of call center agents and reducing their downtime. When integrated with CRM, outbound dialers also make lead management easier and consolidate customer data as well.
Now that we have an understanding of what an outbound dialer (https://www.callmaker.se/hur-fungerar-en-dialer/" style="color:#0563c1; text-decoration:underline) is, let's move on to how an outbound dialer works.

How does an outgoing dialer work?
Modern outgoing dialers are based on VoIP, which eliminates the need for physical equipment. They come either as software installed on a computer or as a cloud-hosted application. These hosted dialers are easy to set up and only require a few minutes to setup. They can start calling using just a computer, a headset, and an Internet connection.
The call center offers a range of services to different industries that require specific call campaigns. Call center managers create these campaigns, assign agents to them, and define rules as needed. The outgoing dialing software, then, select the numbers to call from the list according to the predefined rules.
Before connecting to live agents or reading a recorded message, the outgoing dialer filters out unproductive leads. It only connects agents to calls that a real person answers. In addition, as soon as a call ends, the dialer connects to the next contact. Advanced outgoing dialers start dialing subsequent numbers even before current calls are terminated, usually referred to as predictive dialing.
Now, based on various features and properties, outgoing dialers are classified into different types. Outgoing dialing, campaign management, contact preview, call recording, call monitoring, list management and filter-based calls are some of its core features. Let's take a look at different types of outbound dialers used for specific campaigns and business needs.