0

I am trying to test my Apex class code exposed as rest api. For this, I am making a call to this API using postman. Below is the class I created

@RestResource(urlMapping='/SurboChat/SaveChatTranscript')
global with sharing class SurboChatIntegration {
    @HttpPost
    global static ResponseClass doPost(){
        SurboChatTranscript chatRequest;
        try{
        RestRequest req = RestContext.request;
        String reqBody = (req.requestBody.toString());
        System.debug(reqBody);
        chatRequest = (SurboChatTranscript)json.deserialize(reqBody, SurboChatTranscript.class);
        Account accountObj = new Account(id = chatRequest.AccountId);
        //List<Account> accountObj = [select fields[ALL] from Account where id =:chatRequest.accountId];
        Case caseObj = new Case(id = chatRequest.CaseId);
        //List <Case> caseObj = [select fields[ALL] from Case where id=:chatRequest.caseId];
        SurboChat__c surboChat = new SurboChat__c (ChatBody__c=chatRequest.body, CaseId__c = caseObj.Id, AccountId__c=accountObj.Id, StartTime__c = chatRequest.startTime, EndTime__c=chatRequest.endTime);
    
        insert surboChat;
        
        }
        
        catch(Exception e){
            Integration_Log__c log = new Integration_Log__c();
            log.Component_Name__c='SurboChat Transcript';
            log.Error_Message__c=e.getMessage();
            log.IsIntegration__c=true; 
            insert log; 
            if(log.Error_Message__c!=null)
            {
                ResponseClass sc = new ResponseClass();
                sc.status ='ERROR!';
                sc.message=log.Error_Message__c;
                return sc;
            }
            
        }
        ResponseClass sc = new ResponseClass();
        sc.status ='Success';
       // String query=[Select id from SurboChat__c where StartTime__c = :chatRequest.startTime AND EndTime__c = :chatRequest.endTime];
        sc.message = chatRequest.surboId;
        return sc;
       
    }


   public class SurboChatTranscript{
        String surboId;
        String body;
        String accountId ;
        String caseId;
        DateTime startTime;
        DateTime endTime;
        String status;
        String error;

    }
    
   global class ResponseClass{
        string status{get;set;}
        string message{get;set;}
    }
}

Below is the request I am making to call the above api

curl --location --request POST 'https://<instance_name>/services/data/v53/SurboChat/SaveChatTranscript' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token_id>' \
--header 'Cookie: BrowserId=MG4uOQDPEey3pZcptr2XgA; CookieConsentPolicy=0:1' \
--data-raw '{
    "surboId": "2333",
    "body": "This is the surbo chat transcript test we are doing to save it in Salesforce",
    "accountId": "null",
    "caseId": "null",
    "startTime": "2020-08-11:10:10:10",
    "endTime": "2020-08-11:11:10:10",
    "status": "null",
    "error": "null"
}'

With this call, I am getting errors:

[ { "errorCode": "NOT_FOUND", "message": "The requested resource does not exist" } ]

1 Answer 1

4

Your endpoint https://<instance_name>/services/data/v53/SurboChat/SaveChatTranscript in the postman request is incorrect for custom rest resources. That should be:

https://<instance_name>/services/apexrest/SurboChat/SaveChatTranscript

This is well described in the documentation of the RestResource Annotation

The URL mapping is relative to https://instance.salesforce.com/services/apexrest/

1
  • thank you.. this works :)
    – bhavi
    Sep 3, 2021 at 7:32

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.