You need to sign in to do that
Don't have an account?
Andrew 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:
Screenshot of 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
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:
Screenshot of 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
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.
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
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
If i send thru to /services/oauth2/token a body format like:
I receive a response 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