Skip to content

Commit

Permalink
Moving CalloutNPS Apex class
Browse files Browse the repository at this point in the history
  • Loading branch information
Haiiii76 committed Sep 10, 2023
1 parent 8212c58 commit 62c5019
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
88 changes: 88 additions & 0 deletions force-app/main/default/classes/CalloutNPS.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
public class CalloutNPS{
public static String responseMessage;
public static Integer httpResponseCode;
public static string returnMessage;

@InvocableMethod(label='Send Order Data' description='Send E-Mail Address and Order Number to NPS API' callout = 'true')
//The Record- and Schedule-Triggered flow support bulkification, which means we can build the flow as if it is for only one record,
//and it will automatically loop through all records.
public static List<Results> getData(List<Request> requests){
Results res = new Results();
List<Id> orderIds = new List<Id>();
for(Request req : requests){
orderIds.add(req.RecordId);
}
Set<Id> orderIdsSet = new Set<Id>(orderIds);
String batchSize = System.Label.BatchSizeNPS;
Integer maxRecordSize = Integer.valueOf(batchSize);
List<Order> orderList = [
SELECT Id, OrderNumber, CustomerAuthorizedBy.Email
FROM Order
WHERE ID in :orderIdsSet
];
List<Order> lstTemp = new List<Order>();
List<List<Order>> lstWrapper = new List<List<Order>>();
for(Integer i = 0 ; i < (orderList.size() / maxRecordSize)+1 ; i++){
lstTemp = new List<Order>();
for(Integer j=(i*maxRecordSize);(j<(i*maxRecordSize)+maxRecordSize) && j<orderList.size() ; j++){
lstTemp.add(orderList.get(j));
}
lstWrapper.add(lstTemp);
}
for(List<Order> indvList : lstWrapper){
system.debug('indvList size'+ indvList.size());
res.returnMessage = makeCallout(indvList);
}
List<Results> resList = new List<Results>();
resList.add(res);
return resList;
}

private static String makeCallout(List<Order> ordList) {
try{
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:NPS');
request.setHeader('Content-Type', 'application/json');
request.setMethod('POST');
String jsonBody = makeJsonString(ordList);
request.setBody(jsonBody);
HttpResponse response = http.send(request);
httpResponseCode = response.getStatusCode();
responseMessage = response.getBody();
returnMessage = httpResponseCode + ' : ' + responseMessage ;
System.debug('returnMessage ' + returnMessage);
}catch (Exception e) {
System.debug('Error:' + e.getMessage() + ' Line no:' + e.getLineNumber() );
}
return returnMessage;
}

private static String makeJsonString(List<Order> ordList){
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartArray();
for(Order ord : ordList){
gen.writeStartObject();
gen.writeStringField('sfId', ord.Id);
gen.writeStringField('orderNumber', ord.OrderNumber);
gen.writeStringField('customerEmail', ord.CustomerAuthorizedBy.Email);
gen.writeEndObject();
}
gen.writeEndArray();
String jsonData = gen.getAsString();
System.debug('jsonData-' + jsonData);
return jsonData;
}

public class Request{
@InvocableVariable(label='Record Id' required=true)
public Id RecordId;
}
public class Results{
@InvocableVariable(label='Output Response')
public String returnMessage;

}


}
5 changes: 5 additions & 0 deletions force-app/main/default/classes/CalloutNPS.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>

2 comments on commit 62c5019

@sfgithub1984
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout class

@sfgithub1984
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout main class

Please sign in to comment.