You need to sign in to do that
Don't have an account?
Sandesh Vishwakarma 9
Hii Guys Please help me correct if I am doing anything wrong here. As per my requirement code is working fine but I want it to get checked once by you experienced people
TASK : I want to send the recordId of the updated Opportunity record whenever it is updated. I want to send it to external web service , my code is working and fulfilling my requirement but have a look at it once.
I first got the updated Opportunity recirdId from trigger then I called my first callout where I got the 'Token' , and then I called the secod method where I send the recordId.
Please examine this code. Thanks in advance
public class OpportunityToBeSent {
@future (callout=true)
public static void makePostCallout(String recId) {
String token;
// System.debug('recorddIdd' + recorddIdd);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://XXXXXXXXXXXXXXXXXXXX/authenticate');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"email": "XXXXX@XXXX.com","password": "XXXXXX"}');
System.debug('request'+ request);
HttpResponse response = http.send(request);
System.debug('response'+ response);
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
Map<String, Object> m2 = (Map<String, Object>) results.get('data');
System.debug('results'+ m2.get('jwtToken'));
token = (string)m2.get('jwtToken');
System.debug('token '+ token);
}
makePostCalloutt(recId , token);
}
public static void makePostCalloutt(String recId , String tokenn) {
System.debug('Token >>>'+ tokenn);
String body = recId;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://XXXXXXXXXXXXXXXX?id='+ body);
request.setMethod('POST');
request.setHeader('Content-Type', 'text');
request.setHeader( 'token', 'Bearer ' + tokenn );
// Set the body as a JSON object
request.setBody(body);
System.debug('request'+ request);
HttpResponse response = http.send(request);
System.debug('response'+ response);
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
I first got the updated Opportunity recirdId from trigger then I called my first callout where I got the 'Token' , and then I called the secod method where I send the recordId.
Please examine this code. Thanks in advance
public class OpportunityToBeSent {
@future (callout=true)
public static void makePostCallout(String recId) {
String token;
// System.debug('recorddIdd' + recorddIdd);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://XXXXXXXXXXXXXXXXXXXX/authenticate');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"email": "XXXXX@XXXX.com","password": "XXXXXX"}');
System.debug('request'+ request);
HttpResponse response = http.send(request);
System.debug('response'+ response);
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
Map<String, Object> m2 = (Map<String, Object>) results.get('data');
System.debug('results'+ m2.get('jwtToken'));
token = (string)m2.get('jwtToken');
System.debug('token '+ token);
}
makePostCalloutt(recId , token);
}
public static void makePostCalloutt(String recId , String tokenn) {
System.debug('Token >>>'+ tokenn);
String body = recId;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://XXXXXXXXXXXXXXXX?id='+ body);
request.setMethod('POST');
request.setHeader('Content-Type', 'text');
request.setHeader( 'token', 'Bearer ' + tokenn );
// Set the body as a JSON object
request.setBody(body);
System.debug('request'+ request);
HttpResponse response = http.send(request);
System.debug('response'+ response);
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
I dont see any issue with the code. Let the community know if you are facing any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
I dont see any issue with the code. Let the community know if you are facing any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Can you help me in writting test class for the same code