Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":1291,"date":"2018-03-18T06:36:20","date_gmt":"2018-03-18T06:36:20","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=1291"},"modified":"2018-03-18T06:36:20","modified_gmt":"2018-03-18T06:36:20","slug":"http-callout-to-delete-a-record-in-salesforce","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/http-callout-to-delete-a-record-in-salesforce\/","title":{"rendered":"HTTP callout to Delete a record in Salesforce"},"content":{"rendered":"

HTTP callout to Delete a record in Salesforce<\/h1>\n

 <\/p>\n

To make a HTTP Callout we need to first authorise a user and in this post I will use User-Password Flow of OAuth authentication.<\/p>\n

Before we jump in to code, there are couple of prerequisite to start this integration. In this post I am going to connect one salesforce org with another salesforce org.<\/p>\n

\"HTTP<\/p>\n

To start our Integration we have to make some point & click configurations in both target and source org.<\/p>\n

Configuration at Target Org.<\/p>\n

    \n
  1. Create a Connected Org (How to make Connected Org click here<\/a>).<\/li>\n
  2. Generate the Consumer Secret and consumer Id from the configuration setting<\/li>\n<\/ol>\n

    Configuration at Source Org.<\/p>\n

      \n
    1. Add two Remote site setting (This will whitelist the URLs at the source org).\n
        \n
      1. One for https:\/\/login.salesforce.com<\/li>\n
      2. Second for https:\/\/<-Yourdomain->.salesforce.com.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n

        After doing the above mentioned configurations we need to understand how the REST callout works in Salesforce.<\/p>\n

        To make a REST call we need to authenticate the user first, when a user is authorised salesforce returns instance URL and Access Token for the authenticated user. To authorise a user and generate an access token in return we first write a class as shown in this post.<\/p>\n

        CLick here to understand how to authenticate user before making a REST API Request<\/a><\/p>\n

        Use the class SF2SF_REST_Req created in the link shown above, it contains a method oauthLogin which returns a Static String, this string will contain the information related to the authenticated user.<\/p>\n

        In oauthLogin method, I take login URL, client Id, client secret, username and password as parameters and make an http request to the user-Password flow API i.e \u2018\/services\/oauth2\/token\u2019. If this Http request is successful it returns access token, instance url and other information of the authenticated User and I return the response body as a string from this method.<\/p>\n

        We can use this method anywhere in our org to authenticate Users.<\/p>\n

        We create another class where we will make various REST Requests from one Salesforce org to other org.<\/p>\n

        public\u00a0class\u00a0restRequestExample\u00a0{
        \npublic\u00a0Static\u00a0String\u00a0response\u00a0=\u00a0”\u00a0;
        \npublic\u00a0Static\u00a0String\u00a0oAuthResponse\u00a0;
        \npublic\u00a0Static\u00a0String\u00a0InstanceURL\u00a0=\u00a0”;
        \npublic\u00a0Static\u00a0String\u00a0accessToken\u00a0=\u00a0”;
        \npublic\u00a0static\u00a0string\u00a0endPointURL\u00a0=\u00a0”;
        \npublic\u00a0static\u00a0Map<String,\u00a0Object>\u00a0m\u00a0=\u00a0new\u00a0\u00a0Map<String,\u00a0Object>\u00a0();
        \npublic\u00a0static\u00a0void\u00a0webResponse(){
        \noAuthResponse\u00a0=\u00a0SF2SF_REST_Req.oauthLogin(‘https:\/\/login.salesforce.com’,CLIENTID’,CLIENTSecret,USerName’,PassWord);
        \nm\u00a0=\u00a0(Map<String,\u00a0Object>)JSON.deserializeUntyped(oAuthResponse);<\/p>\n

        InstanceURL\u00a0=\u00a0String.valueOf(m.get(‘instance_url’));
        \naccessToken\u00a0=\u00a0String.valueOf(m.get(‘access_token’));<\/p>\n

        system.debug(‘InstanceURL\u00a0->’\u00a0+\u00a0InstanceURL\u00a0+\u00a0‘\u00a0accessToken\u00a0->\u00a0‘\u00a0+\u00a0accessToken);
        \n}<\/p>\n

        }<\/p>\n

        In the class restRequestExample\u00a0I have created few strings variable which will hold reponse, oAuthResponse, instance Url, Access Token and End point URL. In the method Web response I make use of the oauthLogin method of SF2SF_REST_Req class. And store its response in oAuthResponse. I next deserialise this response into a map of type <string, Object>. From this map I find the value of instance URL and access token and store them in the respective string variables.<\/p>\n

        After the above step we now have the instance URL and access token, the two parameter which makes a REST authenticated. To proceed further I again make a method which is used to generate various http Request as per the given requirement. Definition of the method is given below.<\/p>\n

        public\u00a0static\u00a0HttpRequest\u00a0httpRequestGenerator(String\u00a0reqMethod,\u00a0String\u00a0endpoint,\u00a0String\u00a0accessToken,\u00a0String\u00a0reqBody){
        \nString\u00a0authorizationHeader\u00a0=\u00a0‘Bearer\u00a0‘\u00a0+accessToken;
        \nHttpRequest\u00a0httpRequest\u00a0=\u00a0new\u00a0HttpRequest();
        \nhttpRequest.setMethod(reqMethod);
        \nhttpRequest.setEndpoint(endpoint);
        \nhttpRequest.setHeader(‘Authorization’,\u00a0authorizationHeader);
        \nhttpRequest.setHeader(‘Content-Type’,\u00a0‘application\/json;charset=UTF-8’);
        \nhttpRequest.setBody(reqBody);
        \nreturn\u00a0httpRequest;
        \n}<\/p>\n

        This method takes reqest method, endpoint URL, access token and request body as String parameters and will return httpRequest as a response. Access token is used to create the value of the authorization header of the http request and request method defines whether the request will be GET, POST, PUT etc. depending upon the Request method we will either have request body or not. Finally we set the endpoint URL from the passed parameter and then returns the created Http request.<\/p>\n

        Now we will make various REST Callouts from our source org to the target as below<\/p>\n

        How to Delete a record using Http Callout?<\/p>\n

        To Delete a record we first need to make a POST request, in this POST request we will supply the value of fields of a Sobject we need to update, in the request body of the http callout, in JSON format. There is no Response from the server when a record is updated at Salesforce.<\/p>\n

        The endpoint to delete \u00a0an account is as shown below:<\/p>\n

        https:\/\/<—yourInstance—>.salesforce.com\/services\/data\/vXX.X\/sobjects\/Account\/001D000000INjVe<\/p>\n

        More generalised syntax would be<\/p>\n

        https:\/\/<—yourInstance—>.salesforce.com\/services\/data\/vXX.X\/sobjects\/sObjectName\/\/<–Record ID \u00e0<\/p>\n

        here vXX.X -> is the org version of the rest resource.<\/p>\n

        To make this Http callout I have made the below method in the restRequestExample class.<\/p>\n

         <\/p>\n

        public\u00a0class\u00a0restRequestExample\u00a0{
        \npublic\u00a0Static\u00a0String\u00a0response\u00a0=\u00a0”\u00a0;
        \npublic\u00a0Static\u00a0String\u00a0oAuthResponse\u00a0;
        \npublic\u00a0Static\u00a0String\u00a0InstanceURL\u00a0=\u00a0”;
        \npublic\u00a0Static\u00a0String\u00a0accessToken\u00a0=\u00a0”;
        \npublic\u00a0static\u00a0string\u00a0endPointURL\u00a0=\u00a0”;
        \npublic\u00a0static\u00a0Map<String,\u00a0Object>\u00a0m\u00a0=\u00a0new\u00a0\u00a0Map<String,\u00a0Object>\u00a0();<\/p>\n

         <\/p>\n

        public\u00a0static\u00a0void\u00a0webResponse(){
        \noAuthResponse\u00a0=\u00a0SF2SF_REST_Req.oauthLogin(‘https:\/\/login.salesforce.com’,’3MVG9ZL0ppGP5UrBAQBWYrUKa05u7fcH86snlLCU5mp4.DRt7PrhqrcgCoy2aPdGbFmgCTmZWgtExD__dS88o’,’1267455198314456981′,’datta.sumit.1987@gmail.com’,’pepsico@123′);
        \nm\u00a0=\u00a0(Map<String,\u00a0Object>)JSON.deserializeUntyped(oAuthResponse);<\/p>\n

        InstanceURL\u00a0=\u00a0String.valueOf(m.get(‘instance_url’));
        \naccessToken\u00a0=\u00a0String.valueOf(m.get(‘access_token’));<\/p>\n

        system.debug(‘InstanceURL\u00a0->’\u00a0+\u00a0InstanceURL\u00a0+\u00a0‘\u00a0accessToken\u00a0->\u00a0‘\u00a0+\u00a0accessToken);
        \n}<\/p>\n

        \/\/\u00a0Method\u00a0to\u00a0generate\u00a0Http\u00a0Request<\/p>\n

        public\u00a0static\u00a0HttpRequest\u00a0httpRequestGenerator(String\u00a0reqMethod,\u00a0String\u00a0endpoint,\u00a0String\u00a0accessToken,\u00a0String\u00a0reqBody){
        \nString\u00a0authorizationHeader\u00a0=\u00a0‘Bearer\u00a0‘\u00a0+accessToken;
        \nHttpRequest\u00a0httpRequest\u00a0=\u00a0new\u00a0HttpRequest();
        \nhttpRequest.setMethod(reqMethod);
        \nhttpRequest.setEndpoint(endpoint);
        \nhttpRequest.setHeader(‘Authorization’,\u00a0authorizationHeader);
        \nhttpRequest.setHeader(‘Content-Type’,\u00a0‘application\/json;charset=UTF-8’);
        \nhttpRequest.setBody(reqBody);
        \nreturn\u00a0httpRequest;
        \n}<\/p>\n

        \/\/\u00a0Delete an\u00a0Account record<\/p>\n

        public\u00a0static\u00a0void\u00a0deleteAccount(String\u00a0accessToken,\u00a0string\u00a0instanceURL\u00a0){
        \nendPointURL\u00a0=\u00a0instanceURL\u00a0+\u00a0‘\/services\/data\/v20.0\/sobjects\/Account\/0012800000nfnDXAAY’;
        \nHttp\u00a0http\u00a0=\u00a0new\u00a0Http();
        \nHttpRequest\u00a0httpRequest\u00a0=\u00a0httpRequestGenerator(\u2018POST\u2019,\u00a0endPointURL,\u00a0accessToken,\u00a0null);
        \nHTTPResponse\u00a0httpResponse\u00a0=\u00a0http.send(httpRequest);
        \nsystem.debug(‘httpResponse\u00a0->\u00a0‘+\u00a0httpResponse);
        \nsystem.debug(‘httpResponse.getHeaderKeys()\u00a0->\u00a0‘+\u00a0httpResponse.getHeaderKeys());
        \n}<\/p>\n

        Method webResponse and httpRequestGenerator has been previously explained here. To create an account record, I have used the method createAccount. It takes access token and instance url as the parameters. We create a Http request for this method using httpRequestGenerator method and provided the parameter as following, Request Method: POST, endpoint URL of creating a sObject record, access token and the requestBody which provided the required fields necessary to update the sObject.<\/p>\n

        This is how you delete a record of Sobject using REST endpoint provided by Salesforce<\/p>\n

        Also, Have a look at the below resources:<\/strong><\/h1>\n
          \n
        1. \n

          Best Salesforce Interview Questions book with Apex and Visualforce concept explained<\/a><\/h3>\n<\/li>\n<\/ol>\n

          Also, Have a look at the below learning resources:<\/strong><\/h1>\n
            \n
          1. \n

            SOQL (Salesforce Object Query Language)<\/a><\/strong><\/em><\/h3>\n<\/li>\n
          2. \n

            Apex Trigger Best Practices and the Trigger Framework<\/a><\/strong><\/em><\/h3>\n<\/li>\n
          3. \n

            Salesforce Interview Question and Answers Part 2<\/a><\/strong><\/em><\/h3>\n<\/li>\n
          4. \n

            Salesforce Interview Questions on Test Class<\/a><\/strong><\/em><\/h3>\n<\/li>\n
          5. \n

            Salesforce-lightning-interview-questions-2018<\/cite><\/span><\/a><\/strong><\/em><\/h3>\n<\/li>\n<\/ol>\n

            \u00a0 \u00a0 \u00a06.\u00a0Salesforce Interview Questions Batch Class\u00a0<\/a><\/strong><\/em><\/h3>\n

             <\/p>\n