0

I need to make integration between Org1 and Org2. We created a callout POST. When we create an object in Org1, we also create an object in Org2. POST work. All is Ok. Now I need also delete an object in Org2, if External Id in this obj == Id in obj from Org1.

Something like that:

If(org2Object.ExternalId == org1Object.Id){
   Delete org2Object;
}

I try to delete a record from another org, but I don't know, how to write it right. My Delete callout:

private static Position__c pos = [SELECT Id, Name, Location__c, Job_Description__c, Salary_Range__c, Skills_Required__c, Education__c,
                       Hiring_Manager__r.Name, Email__c, Phone__c, Status__c
                       FROM Position__c WHERE Name = 'Title22'];

public static String getRequestBody(){
    Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
                            FROM Settings__c
                            WHERE Name = 'OurSettings'];  
    String consumerKey = settings.ConsumerKey__c;
    String consumerSecret = settings.ClientSecret__c;
    String username = settings.Username__c;
    String password = settings.Password__c + settings.SecurityToken__c;
    String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
                     '&username=' + username + '&password='+password;
    return request;
}


public static void deleteCalloutResponseContents(Id ourId){
    Http ourHttp = new Http();
    HttpRequest request = httpRequest('DELETE');
    HttpResponse response = ourHttp.send(request);
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);

    if(objAuthenticationInfo.ACCESS_TOKEN != null){
        pos.Id = ourId;

        HttpRequest finalRequest = new HttpRequest();
        String jsonstr='{"Position_ID__c":"'+ ourId +'"}';

        finalRequest.setHeader('Authorization','Bearer '+ objAuthenticationInfo.ACCESS_TOKEN);
        finalRequest.setHeader('Content-Type','application/json');
        finalRequest.setHeader('accept','application/json');
        finalRequest.setBody(jsonstr);
        finalRequest.setMethod('DELETE');

        request.setEndpoint(System.Label.Job_Advertisement_URL + '/services/data/v43.0/sobjects/Job_Advertisement__c/ourId');
        response = ourHttp.send(finalRequest);
        System.debug('RESPONSE BODY: ' + response.getBody());
    }        
}

public class OAuth2{
        public String ACCESS_TOKEN{get;set;}    
    }

How can I fix it?

1

NB: this answer is partially invalidated because the poster changed the code in the question.

HttpRequest does not have a constructor that takes a string. Are you shadowing the object's name with a method httpRequest? That seems likely to produce confusion.

Regardless, the sObject Query resource does not accept the DELETE method. To delete a record, you need to make a DELETE-method request to the sObject Rows endpoint,

/vXX.X/sobjects/SObjectName/id/

with your sObject's name (Job__c) and Id in the relevant positions.

Note that the @HttpDelete annotation is used for building inbound REST services, not making outbound callouts.

  • But how can I compare Id from record from Salesforce org 1 with a Id from record from Salesforce org 2? And delete only right record. – MichaelLev19 Oct 4 '18 at 12:07
  • I don't understand what you're asking. – David Reed Oct 4 '18 at 12:16
  • I updated it... – MichaelLev19 Oct 4 '18 at 12:48
0

To delete via External ID, use the External ID resource:

request.setEndpoint(
  System.Label.Job_Advertisement_URL + 
  '/services/data/v43.0/sobjects/Job_Advertisement__c/Position_ID__c/'+ourId);

There is no need for a JSON body or the associated Content-Type header. If everything goes well, you will get a return status of 204.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.