I was testing out a new APEX callout where whenever an opportunity was won, it would call out to our application.
I was able to get the job to complete, however I am not seeing the update take place in the external system, and I do have system.debug in my class where I'm not seeing anything logged in the debug logger. Any help would be appreciated if you notice anything wrong in my trigger and class.
Here is my APEX Class:
global class OpportunityUpdateURL {
public class Message {
public Prospect prospect;
public String status;
}
public class Prospect{
public String email;
}
private static String toJsonString(Message m) {
return JSON.serializePretty(m);
}
@future (callout=true)
public static void hitTheServer(String email, String status) {
// Instantiate a new HTTP request, specify the method (PATCH) as well as the endpoint
String url='https://XXX.XXXqa.com/api/referrals?_method=PATCH';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setHeader('Authorization', 'Token token="apitokenhere"');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/vnd.infl-v3+json');
Prospect p = new Prospect();
p.email = email;
Message m = new Message();
m.prospect = p;
m.status = status;
String body = toJsonString(m);
req.setMethod('POST');
req.setBody(body);
System.debug('this is the body');
System.debug(body);
HttpResponse res = h.send(req);
System.debug('Response: '+ res.getBody());
}
}
Here is my APEX trigger:
trigger updateOpptyCustomerWons on Opportunity (after update) {
//make a set to hold opportunity ids that we need
Set<Id> opptyIds = new Set<Id>();
for(Opportunity o : trigger.new)
{
//check to see if our field has been changed
if(o.StageName != trigger.oldMap.get(o.Id).StageName){
opptyIds.add(o.Id);
}
}
if(!opptyIds.isEmpty()){
//Opportunity opp
List<Opportunity> opptyList =
[SELECT Id,StageName,Name, Account.Name
FROM Opportunity
WHERE StageName = 'Closed Won' AND Id IN :opptyIds ];
for(Opportunity oppty: opptyList){
String email = '[email protected]';
String status = 'won';
OpportunityUpdateURL.hitTheServer(email, status);
}
}
}
Thanks! Steve