+ Start a Discussion
Andrew GAndrew G 



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 :
The code:
public with sharing class SvcApptTriggerController {
	public static void addPDFAttach(string sessionId, list<id> svcApptIdList){
		System.debug('@@@@@ in addPDFAttach of SvcApptTriggerController');

		HttpRequest req = new HttpRequest();
		req.setHeader('Authorization', 'Bearer '+ sessionId);
		req.setHeader('Content-Type', 'application/json');
		Http http = new Http();
		System.debug('@@@@@ before TEST if');
			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.

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


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 : 

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

@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Andrew GAndrew G
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:

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.

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

If i send thru to /services/oauth2/token a body format like:

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