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
pkulkarnipkulkarni 

Third Party to Salesforce - Inbound Integration

I need to set inbound REST integration in one of my requirement. Whenever we hit Salesforce URL, it should update some info in Salesforce. I am using GET method and passing parameters in URL.

Here is URL I am hitting from POSTMAN - https://peeke-dev-ed.my.salesforce.com/services/apexrest/SFRen/BreakDownCounter?cust=KonarkRegency&build=A&wTank=T1&wLevel=50

But this gives me error - "errorCode": "INVALID_SESSION_ID"  

When I hit URL(/services/apexrest/SFRen/BreakDownCounter?cust=Greenland County Narhe Gaon&build=A&wTank=T1&wLevel=50) from workbench, it updates records successfully. 

My Apex Class is-
@RestResource(urlMapping='/BreakDownCounter/*')
global with sharing class RESTBreakDownController {
    @HttpGet
    global static void updateWaterLevels(){        
        String accntName=RestContext.request.params.get('cust');
        String buildName=RestContext.request.params.get('build');
        String tankName=RestContext.request.params.get('wTank');
        String waterLevel=RestContext.request.params.get('wLevel');
        
        SFRen__Water_Tank__c wTank=[select Id, SFRen__No_of_Times_Emptied_in_a_Day__c , SFRen__Building__c, SFRen__Max_Capacity__c, SFRen__Today_Water_Use__c, SFRen__Current_Water_Level__c, SFRen__Building__r.SFRen__Account__r.Name, SFRen__Building__r.Name from SFRen__Water_Tank__c where SFRen__Building__r.SFRen__Account__r.Name=:accntName and SFRen__Building__r.Name=:buildName and Name=:tankName limit 1];       
        
        if(wTank!=null){
            SFRen__Tank_Level__c level=new SFRen__Tank_Level__c(SFRen__Reading_Time__c=System.now(),SFRen__Water_Level__c=Decimal.valueOf(    waterLevel),SFRen__Water_Tank__c=wTank.Id);       
            insert level;            
        }       
    }   
}

I am not using any authentication. Am I missing anything in the process? Any help would be much appreciated. Thank you in advance.
Best Answer chosen by pkulkarni
Ankit SehgalAnkit Sehgal
In the header of the request you have to pass the session Id or it wont perform any action. The header in postman would look like 
Authorization: Bearer <session_id>

All Answers

Ankit SehgalAnkit Sehgal
In the header of the request you have to pass the session Id or it wont perform any action. The header in postman would look like 
Authorization: Bearer <session_id>
This was selected as the best answer
pkulkarnipkulkarni
Thank you Ankit for reply.

I generated session id with below code, and used that in request. It's working. But one question, will it be SAME for next request OR for every new request there would be NEW session id?

// Remove the intermediate space to get the actual Session ID.
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' +  UserInfo.getSessionId().substring(15));

If I wanna use same session id FOREVER, what should I do?
 
Ankit SehgalAnkit Sehgal
You have to generate a new session ID after the current session logs out. You can extend the time out session under "session settings" but it won't work forever.
pkulkarnipkulkarni
How can I genereate session id in third party application from where I am hitting this URL? Any other option?