-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
public class BatchApexCalloutNPS implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful{ | ||
|
||
public Set<Id> orderIdSet = new Set<Id>(); | ||
public string responseErrorMessage = ''; | ||
|
||
public BatchApexCalloutNPS(Set<Id> orderIdSet) { | ||
this.orderIdSet = orderIdSet; | ||
} | ||
|
||
public Database.QueryLocator start(Database.BatchableContext bc) { | ||
System.debug('size from start method '+ orderIdSet.size()); | ||
return Database.getQueryLocator([ | ||
SELECT Id, OrderNumber, CustomerAuthorizedBy.Email | ||
FROM Order | ||
WHERE ID in :orderIdSet | ||
]); | ||
} | ||
|
||
public void execute(Database.BatchableContext bc, List<sObject> scope) { | ||
System.debug('Size from execute '+ scope.size()); | ||
//for (Order ord: (List<Order>) scope) { | ||
responseMessage = makeCallout(scope); | ||
//} | ||
} | ||
|
||
public void finish(Database.BatchableContext BC){ | ||
if(responseErrorMessage != ''){ | ||
|
||
} | ||
} | ||
|
||
private string makeCallout(List<Order> ordList) { | ||
if(responseErrorMessage == ''){ | ||
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); | ||
/*String jsonBody = JSON.serialize( | ||
new Map<String, Object> { | ||
'sfId' => ord.Id, | ||
'orderNumber' => ord.OrderNumber, | ||
'customerEmail' => ord.CustomerAuthorizedBy.Email | ||
});*/ | ||
request.setBody(jsonBody); | ||
HttpResponse response = http.send(request); | ||
Integer httpResponseCode = response.getStatusCode(); | ||
if(httpResponseCode != 200){ | ||
responseErrorMessage = response.getStatus(); | ||
} | ||
}catch (Exception e) { | ||
System.debug('Error:' + e.getMessage() + 'Line no:' + e.getLineNumber() ); | ||
} | ||
} | ||
return responseErrorMessage; | ||
} | ||
|
||
private String makeJsonString(List<Order> ordList){ | ||
Map<String, Object> resultMap = new Map<String, Object>(); | ||
for(Order ord : ordList){ | ||
resultMap.put('sfId', ord.Id); | ||
resultMap.put('orderNumber', ord.OrderNumber); | ||
resultMap.put('customerEmail', ord.CustomerAuthorizedBy.Email); | ||
} | ||
String jsonBody = JSON.serialize(resultMap); | ||
return jsonBody; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/BatchApexCalloutNPS.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |