This is my first time attempting a JSON POST and I trust your collective judgement. I would appreciate if someone could tell me if I am approaching this correctly or not. I didn't receive any compile errors in my classes, but I am receiving an error in the logs when attempting the callout.


sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Googling of this error points towards a bad certificate or certificate issue on the receiving end, but the company whos API I am calling to says that the certificate on the box is valid. They even directed me to a Google App called "Advanced REST Client" for testing and I was able to successfully POST using this tool... SO, I am doing something wrong apparently, I just don't know what.

Objective: Send Serialized JSON Opportunity Object data to an external third party REST API upon successful update of an opportunity.

Stack (starting from Trigger):

OpportunityTrigger calls OpportunityTriggerHandler.VantivCalloutExec(Trigger.new) > VantivCalloutExec turns the Trigger.new list into Opportunity <Set>Id's and Passes to @future VantivCallout.VantivPDCallout(Set<Id>)

VantivCallout.VantivPDCalloutthen builds a new list of Opps that match the Set of Ids, and then instantiates a new HttpRequest, sets the endpoint, the method (POST), the Base64 authorization combo using username/pass, sets a few headers, and then loops through the opp list building a new serialized string and sending it to the REST API for each opp in the loop.

I'm not having an error specifically related to the code - all code saved/compiled correctly. But, the callout still fails. Since I have never done this before, I'm having trouble understanding if I'm implementing this incorrectly or if there is another underlying problem blocking the secure connection. I've provided all of my @future code below for your convenience.

VantivCallout (@future) Code:

public class VantivCallout {

    public static void VantivPDCallout(Set<Id> pdOppIds) {

        List<Opportunity> oppsList = [SELECT Id FROM Opportunity WHERE Id IN :pdOppIds];

        HttpRequest req = new HttpRequest();

        String username = 'NOT-A-REAL-USERNAME';
        String password = 'NotOurPassword123';

        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headervalue);

        req.setHeader('Authorization', authorizationHeader);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept', 'application/json');

        Http http = new Http();

        JSONGenerator generator = JSON.createGenerator(true);

        for(Opportunity opp : oppsList){

            generator.writeStringField('DBAName','Morgan\'s Test Account');

            String jsonString = generator.getAsString();

            HTTPResponse res = http.send(req);

Thanks for your time!

  • You may want to try posting this at codereview.stackexchange.com, as it might be losed here for a few reasons (You can look at the dont ask list as well : salesforce.stackexchange.com/help/dont-ask) – battery.cord May 2 '16 at 20:21
  • Hi @battery.cord, thanks for the info. I'll consider CodeReview, but my concern/reason for posting it on Salesforce Stack is that it relates specifically to how to do JSON calls correctly via Apex/Future Call using the System.JSON class and related methods. I read the dont-ask page and I will admit that it does meet the criteria of 'no actual problem to be solved' but that is only because the error I am receiving seemed unrelated. I will work to modify my question to be more appropriate. – Morgan Marchese May 2 '16 at 20:29
  • 1
    @battery.cord I've modified my question into a more appropriate one while still serving my interest of vetting my code for correctness. I hope this more adheres to the standards. – Morgan Marchese May 2 '16 at 20:36
  • 3
    The "unable to find valid certification path to requested target" error can be caused by the site you are connecting to having a self-signed certificate (or an otherwise invalid certificate chain) rather than one correctly created through a Certificate Authority. – Keith C May 2 '16 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.