You need to sign in to do that
Don't have an account?
EagerToLearn
REST callout from SF to SF
I am struggling to find a basic example using Named Credentials, Auth Provider and Connected app that will use two SF developer orgs. I want to put the https code in the debug window to update a user record's isActive field to false. I have the aforementioned setup but the code that I have doesn't seem to work as I am getting a 400 error along with the following error:
18:54:22:098 USER_DEBUG [11]|DEBUG|[{"errorCode":"URL_NOT_RESET","message":"Destination URL not reset. The URL returned from login must be set"}]
TRIED THIS IN DEBUG:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sandbox_Management/services/data/v40.0/sobjects/User/0056A000001jCnC?_HttpMethod=PATCH');
req.setMethod('POST');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody('{"isActive":false}');
//req.setBody('{"id" : "0056A000001jCnC", "isActive":false}');
HTTPResponse res = http.send(req);
System.debug('SENT');
System.debug(res.getBody());
Thanks for the help.
18:54:22:098 USER_DEBUG [11]|DEBUG|[{"errorCode":"URL_NOT_RESET","message":"Destination URL not reset. The URL returned from login must be set"}]
TRIED THIS IN DEBUG:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sandbox_Management/services/data/v40.0/sobjects/User/0056A000001jCnC?_HttpMethod=PATCH');
req.setMethod('POST');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody('{"isActive":false}');
//req.setBody('{"id" : "0056A000001jCnC", "isActive":false}');
HTTPResponse res = http.send(req);
System.debug('SENT');
System.debug(res.getBody());
Thanks for the help.
https://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/
https://www.jitendrazaa.com/blog/tag/named-credentials/
https://www.jitendrazaa.com/blog/tag/integration/
https://www.jitendrazaa.com/blog/salesforce/salesforce-rest-api-playground/
https://www.jitendrazaa.com/blog/tag/rest-api/
Thank you for the links as they were veryt helpful. I come to realize that in order for me to deactivate a user in Org B from Org A; all that I would have is the username, not the id! I tried replacing the user id with the username but I got a 404. Is there a way without having to write a custom REST service on the Org B, C, D, etc. to achieve updating the isActive field in Org B when running the following code in Org A or some similar code? Since username is unique I am hoping there is a way without a custom rest service on the Orgs, B, C, D, etc.
This code worked but I won't know the id in org B - only the username.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sandbox_Management/services/data/v40.0/sobjects/User/0056A000001jCnC?_HttpMethod=PATCH');
//req.setEndpoint('callout:Sandbox_Management/services/data/v40.0/sobjects/User/tester@myemailaddress.com?_HttpMethod=PATCH');
req.setMethod('POST');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody('{"isActive":false}');
HTTPResponse res = http.send(req);
System.debug('SENT');
System.debug(res.getBody());
https://stackoverflow.com/questions/12508026/how-to-disable-deactivate-a-salesforce-user-through-soap-api
Mark this as soleved if this helps you, you too will fetch some points if the question is marked as solved :)
I can't seem to find a complete simple example that shows Apex calling a custom apex REST service with a list as the parameter to the service.
This is the working callout as a single value...
public class SandboxManagerCallout {
@future(callout=true)
public static void DeactivateUser(String NamedCredential, String UserName) {
// The line below is only here to save time during testing by reactivating users.
// This should always be set to FALSE in PRODUCTION.
Boolean isActive = true;
String EndPoint = 'callout:' + NamedCredential + '/services/apexrest/SandboxManagerAPI/' + UserName + '/' + isActive;
HttpRequest req = new HttpRequest();
req.setEndpoint(EndPoint);
req.setMethod('POST');
Http http = new Http();
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
//req.setBody('{"UserName":UserName, "isActive":isActive}');
req.setBody('{}');
HTTPResponse res = http.send(req);
System.debug('---Response---');
System.debug(res.getBody());
}
}
AND THIS IS THE WORKING SERVICE:
@RestResource(urlMapping='/SandboxManagerAPI/*')
global with sharing class SandboxManagerAPI {
@HttpPost
global static void doDeactivate() {
Integer USER_NAME = 2;
Integer IS_ACTIVE = 3;
System.debug('---DeactivateUser.doDeactivate()---');
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
System.debug('req: ' + req.requestURI);
List<String> Parms = req.requestURI.split('/');
String UserName = parms[USER_NAME];
String isActive = parms[IS_ACTIVE];
//String userName = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
System.debug('Username Passed In: ' + UserName);
System.debug('isACtive passwed In: ' + isActive);
User user = [SELECT Id, Username, isActive
FROM User
WHERE userName = :UserName LIMIT 1];
if (user != null) {
//user.isActive = false; //make sure to uncomment this line and comment the one below after testing is complete
user.IsActive = Boolean.valueOf(isActive); //this line is only for testing
Try{
update user;
} catch (System.exception e) {
}
} else {
//Could not find the record so return a message that
//informs the caller that the record was not found
}
}
}