• John Issa
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
Hello,

I'm currently trying to parse an xml file that I retreive via an API call.  My dataset is relatively close to what is given in this example: 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm

When I go through my example, I can only return the values found in the first node.

If I have an xml file that has multiple nodes of the same value (i.e address and name), how would I parse the value to retreive a specific value?

For example:  If I wanted to return the city in which the addres of 555 2nd Street is found, how would I go about doing that?
 
<info>
<address>
    <name>Kirk Stevens</name>
    <street1>808 State St</street1>
    <street2>Apt. 2</street2>
    <city>Palookaville</city>
    <state>PA</state>
    <country>USA</country>
</address>
<address>
    <name>John Smith</name>
    <street1>8078 1st St</street1>
    <street2>Apt. 15</street2>
    <city>Gainsville</city>
    <state>GA</state>
    <country>USA</country>
</address>
<address>
    <name>Jim Brown</name>
    <street1>555 2nd St</street1>
    <street2></street2>
    <city>Orlando</city>
    <state>FL</state>
    <country>USA</country>
</address>
</info>
 
public class DomDocument {
 
    // Pass in the URL for the request
    // For the purposes of this sample,assume that the URL
    // returns the XML shown above in the response body
    public void parseResponseDom(String url){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        // url that returns the XML in the response body
        req.setEndpoint(url);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();
        
        //Retrieve the root element for this document.
        Dom.XMLNode address = doc.getRootElement();
        
        String name = address.getChildElement('name', null).getText();
        String state = address.getChildElement('state', null).getText();
        // print out specific elements
        System.debug('Name: ' + name);
        System.debug('State: ' + state);
        
        // Alternatively, loop through the child elements.
        // This prints out all the elements of the address
        for(Dom.XMLNode child : address.getChildElements()) {
           System.debug(child.getText());
        }
    }
}

 
String accessKey = 'ACCESSKEY';
            String secretKey = 'SECRETKEY';
            String bucketname = 'BUCKETNAME';
            String host = 's3-us-west-1.amazonaws.com';
            String filename = 'test1.json';
            String hashAlgo = 'HmacSHA256';
            String url = 'https://' + bucketname + '.' + host + '/' + filename;
            Date currentDate = date.today();
            DateTime currentDateRaw = DateTime.now();
            String currentDateOnly = DateTime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
            String regionName = 'us-west-1';
            String serviceName = 's3';
            String strBody = '';

            ////////////////////////////////////
            // 1 - CANONICAL REQUEST
            ////////////////////////////////////

            String strCanonicalRequest = '';
            strCanonicalRequest+='PUT\n';
            strCanonicalRequest+='\n';
            strCanonicalRequest+='content-type:application/x-www-form-urlencoded; charset=utf-8\n';
            strCanonicalRequest+='host:s3-us-west-1.amazonaws.com\n';
            strCanonicalRequest+='x-amz-date:' + currentDate + '\n';
            strCanonicalRequest+='\n';
            strCanonicalRequest+='content-type;host;x-amz-date';
            String strPayloadHash = EncodingUtil.convertToHex(Crypto.generateDigest('SHA-256', Blob.valueOf(strBody))); // Payload
            strCanonicalRequest+= '\n' + strPayloadHash.toLowerCase();

            ////////////////////////////////////
            // 2 - STRING TO SIGN
            ////////////////////////////////////

            String strStringToSign = '';
            strStringToSign+='AWS4-HMAC-SHA256\n';
            strStringToSign+=currentDateRaw + '\n';
            strStringToSign+=currentDateOnly + '/' + regionName + '/' + serviceName + '/aws4_request';
            String strCanonicalHash = EncodingUtil.convertToHex(Crypto.generateDigest('SHA-256', Blob.valueOf(strCanonicalRequest))); // Payload
            strStringToSign+= '\n' + strCanonicalHash.toLowerCase();

            ////////////////////////////////////
            // 3 - SIGNATURE
            ////////////////////////////////////

            String kSecret = 'AWS4' + secretKey;
            Blob kDate = Crypto.generateMac(hashAlgo, Blob.valueOf(currentDateOnly), Blob.valueOf(kSecret));
            Blob kRegion = Crypto.generateMac(hashAlgo, Blob.valueOf(regionName), kDate);
            Blob kService = Crypto.generateMac(hashAlgo, Blob.valueOf(serviceName), kRegion);
            Blob kSigning = Crypto.generateMac(hashAlgo, Blob.valueOf('aws4_request'), kService);
            String strSignature = EncodingUtil.convertToHex(Crypto.generateMac(hashAlgo, Blob.valueOf(strStringToSign), kSigning));
            strSignature = strSignature.toLowerCase();

            ////////////////////////////////////
            // 4 - AUTHORIZATION HEADER
            ////////////////////////////////////

            String strAuthorizationHeader = 'AWS4-HMAC-SHA256 ';
            strAuthorizationHeader+= 'Credential=' + accessKey + '/' + currentDateOnly + '/' + regionName + '/' + serviceName + '/aws4_request, ';
            strAuthorizationHeader+= 'SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, ';
            strAuthorizationHeader+= 'Signature=' + strSignature;


            // PUT
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndPoint(url);
			req.setMethod('PUT');
			req.setBody(body);
			req.setHeader('ACL', 'public-read-write');
            req.setHeader('Authorization', strAuthorizationHeader);
			req.setHeader('Date', currentDateOnly);
			req.setHeader('x-amz-content-sha256', 'UNSIGNED-PAYLOAD');
            HttpResponse response = http.send(req);

            if (response.getStatusCode() != 200) {
                System.debug('The status code returned was not expected: ' +
                        response.getStatusCode() + ' ' + response.getStatus());
                System.debug('Auth: ' + strAuthorizationHeader);

            else {

                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                System.debug('This is working');
                System.debug(response.getBody());
                String output =  (String)results.get('result');


            }

Hello,

I'm in the process of connecting to S3 out of APEX and I'm receiving the following error message when executing an anonymous request:

The authorization header is malformed; the authorization header requires three components: Credential, SignedHeaders, and Signature.

Example of Signature:
Auth: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/Tue, 09 Jul 2019 07:54:14 GMT/us-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=fe5f80f77d5fa3beca038a248ff027d04453421fe2855ddc963176630326f1024

When I look at the authorization signature, it looks to be setup correctly.  I"m able to add that signature to the body of my postman request and I get a successful response but when I add it as a header, I receive a 403 message.

Has any one experienced something similar?  If so, any assistance woiuld be greatly appreciated.  TY!

 
Hello,

Is it possible to trigger a pop-up when a user selects the convert button on the lead object?

When converting, we have some criteria in the lead that will dictate what record type the lead will convert to in the account object. 

My goal is to provide a pop-up, like a YesNo messagebox that will allow them to proceed to the Convert screen if Yes is selected and end the operation if No is selected.  

Any guidance would be greatly appreciated.

Thank you!