function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nihon TaisaiNihon Taisai 

DELETE a record by REST API without @HttpDelete

0down votefavorite
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?
Raj VakatiRaj Vakati
You need to defind the delete action one the org one and need to invoke in second org .. 
 
@HttpDelete
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
      delete result ;
    }



 
farukh sk hdfarukh sk hd
Here integration is performed for deleting record from salesforce using rest api.
Hope this helps.

https://www.sfdc-lightning.com/2019/01/salesforce-rest-api-integration-to-delete-a-record.html