64 lines
2.7 KiB
OpenEdge ABL
64 lines
2.7 KiB
OpenEdge ABL
public class BrevoHandler {
|
|
|
|
static Map<String, Object> results = new Map<String, Object>();
|
|
public static void getcontacts(){
|
|
HttpRequest req = new HttpRequest();
|
|
Http http = new Http();
|
|
|
|
// GET request to check if the contact exists
|
|
|
|
req.setEndpoint('https://api.brevo.com/v3/contacts/' );
|
|
req.setMethod('GET');
|
|
req.setHeader('Content-Type', 'application/json');
|
|
req.setHeader('api-key', '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');
|
|
|
|
try {
|
|
HttpResponse res = http.send(req);
|
|
if (res.getStatusCode() == 200) {
|
|
System.debug('Contact Already Exist in Brevo');
|
|
results.putAll((Map<String, Object>) JSON.deserializeUntyped(res.getBody()));
|
|
|
|
}
|
|
} catch(Exception e) {
|
|
System.debug('API call failed with message: ' + e.getMessage());
|
|
}
|
|
|
|
}
|
|
public static void print(){
|
|
BrevoHandler.getcontacts();
|
|
System.debug('@@@@@@@'+results.Size());
|
|
}
|
|
|
|
public static void createcontact(string email,string firstname,string lastname,string phone){
|
|
|
|
Brevohandler.getcontacts();
|
|
HttpRequest req = new HttpRequest();
|
|
Http http = new Http();
|
|
|
|
req.setEndpoint('https://api.brevo.com/v3/contacts');
|
|
req.setMethod('POST');
|
|
req.setHeader('Content-Type', 'application/json');
|
|
req.setHeader('api-key', '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');
|
|
Map<String, Object> brevoContact = new Map<String, Object>();
|
|
brevoContact.put('email', email);
|
|
Map<String, Object> brevoAttributes = new Map<String, Object>();
|
|
brevoAttributes.put('FIRSTNAME', firstName);
|
|
brevoAttributes.put('LASTNAME', lastName);
|
|
brevoAttributes.put('SMS', phone);
|
|
brevoContact.put('attributes', brevoAttributes);
|
|
String brevoBody = JSON.serialize(brevoContact);
|
|
req.setBody(brevoBody);
|
|
|
|
try {
|
|
HttpResponse res = http.send(req);
|
|
if (res.getStatusCode() != 201) {
|
|
System.debug('Failed to create contact in Brevo with status code: ' + res.getStatusCode());
|
|
}else{
|
|
System.debug('Created Secussfully:'+brevobody);
|
|
}
|
|
} catch(Exception e) {
|
|
System.debug('API call failed with message: ' + e.getMessage());
|
|
}
|
|
}
|
|
|
|
} |