+ Start a Discussion
Andrew GAndrew G 

System.HttpResponse[Status=Unauthorized

Hello

Having an issue implementing some code which is to create a PDF document from a trigger.  I have used this as a reference/start point :
https://jungleeforce.wordpress.com/2014/06/11/generate-a-pdf-and-attach-it-to-record-from-a-trigger-in-salesforce-restful/comment-page-1/
The code:
public with sharing class SvcApptTriggerController {
	@Future(callout=true)
	public static void addPDFAttach(string sessionId, list<id> svcApptIdList){
		System.debug('@@@@@ in addPDFAttach of SvcApptTriggerController');

		HttpRequest req = new HttpRequest();
		req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/AddPDFtoSvcAppt/');
		req.setMethod('POST');
		req.setBody('{"svcApptIdList":'+JSON.serialize(svcApptIdList)+'}');
		req.setHeader('Authorization', 'Bearer '+ sessionId);
		req.setHeader('Content-Type', 'application/json');
		Http http = new Http();
		System.debug('@@@@@ before TEST if');
		if(!test.isRunningTest()){
			System.debug('@@@@@ in TEST if');
			HTTPResponse res = http.send(req);
		}
	}
}
The debug log snippet:
Error message from debug log - Unauthorised
Screenshot of remote Site settings:
Remote Site Settings

So the endpoint reported in the debug logs is in the remote site settings.
The PDF creation code works from the workbench - which obviously uses a different authentication model (name/password).

Feeling that I have missed something obvious.

Regards
Andrew
 
Raj VakatiRaj Vakati
You  need to add the remote site settings  with the below URL
 
WITH <YOUR_SALESOFCE_URL>/services/apexrest/AddPD



        req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/AddPDFtoSvcAppt/');

in the above line you have an URL that need to be added to remote site settings

Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the callout fails. Salesforce prevents calls to unauthorized network addresses.

To add a remote site setting:
From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
Click New Remote Site.
Enter a descriptive term for the Remote Site Name.
Enter the URL for the remote site.
Optionally, enter a description of the site.
Click Save.


 
v varaprasadv varaprasad
Hi Andrew,

Please check once sessionId is generated correctly or not.

Otherwise test web service in postman first.
More info : 
https://www.youtube.com/watch?v=7PaDtgPLH90

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Andrew GAndrew G
@Raj
If you note the included screen shots in the original post, you will see that the Remote Site settings match the debug log endpoint:

from my debug log the Endpoint:
System.HttpRequest[Endpoint=https://coldshield--Dev2.cs58.my.salesforce.com/services/apexrest/AddPDFtoSvcAppt/, Method=POST]
From my remote Site settings:
https://coldshield--Dev2.cs58.my.salesforce.com

@V
Session ID is now suppressed in debug logs, but I have tested that a Session ID is generated via other means.

I have not used Postman before, but will go and see what I can learn.

Regards
Andrew
Andrew GAndrew G
Ok, so I have played with Postman.

If i send thru to /services/oauth2/token a body format like:
 
grant_type=password&client_id=<myclientID>&client_secret=<myclientSecret>&username=<me>@<mydevdomain>&password=<mypassword>

I receive a response
{
    "access_token": "<randomtokenstring>",
    "instance_url": "https://coldshield--Dev2.cs58.my.salesforce.com",
    "id": "https://test.salesforce.com/id/00DXXXXXXXXXXX/005XXXXXXXXX",
    "token_type": "Bearer",
    "issued_at": "1540421809325",
    "signature": "<randomstring>="
}
I then stick the access token in the Authorization header Key as Bearer <randomtokenstring> with a formatted JSON body, the code executes and a record is created and PDF attached.

However, it does not function still from the Trigger.

Feedback / thoughts / input appreciated.

Andrew G