1

I'm running into a strange problem. The code is completing fine and sending the request/getting response back, handling the response correctly. I am just unable to show the "Success" toast message to the user. The callback never completes despite everything working. I've tried using try/catch statements, no exceptions get returned. I've looked at the json, its formatted the same going into the component as it is going out. Only thing is if i comment out the deserialization then the callback returns a message. Is this a bug?

handleSend: function(component, event, helper) 
    {
        
        var exigerData = component.get("v.data");
        var requestType = component.get("v.tabId"); 
        if(helper.validate(requestType, component) == true)
        {
            if (requestType == 'Company') 
            {
                var action = component.get("c.sendExigerCompanyData");
                action.setParams({
                    jdata: JSON.stringify(exigerData)
                });
                
                action.setCallback(this, function(response) {
                    alert('callback');
                    var state = response.getState();
                    
                    if (state === "SUCCESS") {  
                        
                            alert('Successful response');
                            var storeResponse = response.getReturnValue();
                            if(storeResponse == 'Success')     
                            {
                                alert('Successful status');
                                var toastEvent = $A.get("e.force:showToast");
                                toastEvent.setParams({
                                    title: "Success!",
                                    message: "Information was sent to Exiger!",
                                    type: "Success"});
                                toastEvent.fire();    
                            }
                        
                    }
                });
                $A.enqueueAction(action);
            }  
        }  
    }

First problem occurs in this method when deserializing the jdata i never get an exception but the aura callback never gets a response(but everything executes fine). If i comment this out i get the debug saying the callback finished.

@auraEnabled
    public static string sendExigerCompanyData(String jdata)
    {
        String status = 'Success';
        wrapperClass data = new wrapperClass();
        try
        {
            //if i comment this out the callback completes, but it never throws an exception
            data = (wrapperClass)JSON.deserialize(jdata, wrapperClass.class);
            system.debug(data);
        }
        catch(Exception e)
        {
            status = 'Error';
            system.debug(e.getMessage());
        }
        
        ExigerDataPayload payload = new ExigerDataPayload();
        
        //set data for request        

        
        String bearerToken = ExigerAPIClient.getAccessToken();
          
        if(bearerToken != 'Token Error') status = ExigerAPIClient.sendCompanyInfoToExiger(bearerToken, payload, data.accountId);
        else status = 'Token Error'; 
  
        System.debug(status);     
        return status;
    }

Second issue (similar to above), if the first issue doesn't occur then the same thing happens when trying to deserialize this response. No exception, code completes, but no response is returned to the aura callback

@auraEnabled
    public static String getAccessToken() //ExigerDataPayload payload
    {
        Exiger_Credentials__c exiger = [Select password__c, username__c from Exiger_Credentials__c limit 1];
        
        string accessToken = '';
        // Define the endpoint URL
        String endpoint = 'https://insightauth.exiger.com/oauth2/token';
        
        //credentials
        String username = exiger.username__c;  //metadata setting
        String password = exiger.password__c;  //metadata setting
        String requestBody = 'grant_type=password&username='+username+'&password='+password;
        
        // Create the HTTP request
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);  
        request.setHeader('Accept', 'application/json');
        request.setMethod('POST');
        request.setBody(requestBody);
        
        // Instantiate a new http object
        Http h = new Http();
        HttpResponse response = h.send(request);
        
        if (response.getStatusCode() == 200) {
            try {
                Map<String, Object> responseBody = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                accessToken = (String) responseBody.get('access_token');
                System.debug('Access Token = ' + accessToken);
            } catch (Exception e) {
                System.debug('Error occurred during deserialization: ' + e.getMessage());
                accessToken = 'Token Error';
            }
        } else {
            System.debug('API call failed with status code: ' + response.getStatusCode());
            accessToken = 'Token Error';
        }
        
        return accessToken;
    }


 @auraEnabled
    public static string sendCompanyInfoToExiger(String accessToken, ExigerDataPayload payload, String accId) 
    {
        if(accessToken != 'Token Error' && accessToken != null)
        {
            String jsonPayload = JSON.serialize(payload);
            // Instantiate a new http object
            Http h = new Http();
            
            // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://insightapi.exiger.com/api/v1/thirdparty/company');
            req.setMethod('POST');
            req.setHeader('Content-Type', 'application/json');
            req.setHeader('Authorization', 'Bearer ' + accessToken);
            req.setBody(jsonPayload);
            
            // Send the request, and return a response
            HttpResponse res = h.send(req);
            String responseBody = res.getBody();
            if(res.getStatusCode() == 200)
            {
                Owner_Health_Check__c ohc = new Owner_Health_Check__c();
                ohc.account__c = accId;
                ohc.Exiger_Id__c = responseBody;
                insert ohc;    
                return 'Success';
            }
            else
            {
                try{
                    Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
                    return 'Error: ' + responseMap.get('message');
                }
                catch(exception e)
                {
                    return 'Error: ' + e.getMessage();            
                }
            }
        }   
        else
        {
            System.debug('Access Token was null');
            return 'Token Error';
        }
        
        
    }

v.data = (wrapperclass)

public class wrapperClass
    {
        @auraEnabled public String oppId {get; set;}
        @auraenabled public String email {get; set;}
        @auraEnabled public Integer clientId {get; set;}
        @auraEnabled public Map<Id, String> PotentialBuyerAccounts{get; set;}
        @auraenabled public String accountId {get; set;} //create Owner Health Check record related to this account
        
        //Pass country info to multi-select picklist
        @auraEnabled public List<String> allCountries {get; set;}
        
        //High Risk and Prohibited Countries
        @auraEnabled public List<String> highRiskCountries {get; set;}
        @auraEnabled public List<String> ProhibitedCountries {get; set;}
        
        //heading
        @auraEnabled public String OpportunityName {get; set;}
        @auraEnabled public Date RequestDate {get; set;}
        @auraEnabled public String Developer {get; set;} //read only
        //company
        @auraEnabled public String UmbrellaOwner {get; set;}
        @auraEnabled public String EntityType {get; set;}  
        @auraEnabled public String CountryOfFormation {get; set;}  
        @auraEnabled public String StateOfFormation {get; set;}  
        @auraEnabled public String website {get; set;}
        @auraEnabled public String alternateName {get; set;}
        @auraEnabled public List<String> countriesOfDoingBusiness {get; set;}
        
        //individual
        @auraEnabled public String firstName {get; set;}
        @auraEnabled public String lastName {get; set;}  
        
        //Affiliate Info
        @auraEnabled public List<IndAffiliateWrapper> IndividualAffiliateList {get; set;}  
        @auraEnabled public List<CoAffiliateWrapper> CompanyAffiliateList {get; set;}  
    }    
    
    public class CoAffiliateWrapper
    {
        public String name {get; set;}   
        public String country {get; set;}
        public String state {get; set;}
        public List<String> doingBusiness {get; set;}
    }
    
    public class IndAffiliateWrapper
    {
        public String firstName {get; set;}
        public String lastName {get; set;}
        public String country {get; set;}
        public List<String> doingBusiness {get; set;}
    }

JSON

getAccesstoken json:
{
    "access_token": "asd0987fehwu09if87w0h0s8dfhv",
    "token_type": "bearer",
    "expires_in": 900
}

sendCompanyToExiger json:

123456

v.data json between aura and apex:


{
"allCountries":["Afghanistan","Aland Islands"], 
"alternateName":"test", 
"clientId":0000,
"CompanyAffiliateList":[],
"countriesOfDoingBusiness":["United States"],
"CountryOfFormation":"United States",
"Developer":"John Doe",
"email":"[email protected]",
"EntityType":"Corporation",
"firstName":"",
"highRiskCountries":["Afghanistan"],
"IndividualAffiliateList":[],
"lastName":"",
"oppId":"006Ki000002HqUlIAK",
"OpportunityName":"Amazon",
"PotentialBuyerAccounts":{"a07O400000G8nskIAB":"Amazon"},
"ProhibitedCountries":["Cuba"],
"RequestDate":"2024-04-25",
"StateOfFormation":"Alaska",
"UmbrellaOwner":"Amazon",
"website":"test",
"accountId":"a07O400000GA6kXIAT"
}

Exactly what is returned from console.log. I notice there is no closing bracket. And also two clientId fields. Maybe potential problem?

6
  • When you leave the deserialize in place, what debug output do you get?
    – Phil W
    Commented Apr 23 at 19:02
  • Debug outputs "Success" and everything works as it should. Just no response back to the aura component.
    – ThatGuy
    Commented Apr 23 at 19:43
  • We'd have to see the content of the data attribute from the component. You may be double-serializing or something that is throwing the entire thing off. Also, consider just not using a try-catch--if something is going wrong, you'll get the error you're looking for.
    – sfdcfox
    Commented Apr 23 at 19:57
  • updated code with the wrapperclass which is referenced in the v.data. If i don't use a try/catch then it's the same behavior, no response but everything works fine. Initially the try/catch is what fixed the first issue, then the second issue came up. I did the try/catch thinking it would fix it but now neither of them work.
    – ThatGuy
    Commented Apr 23 at 20:15
  • Can you provide an example of the JSON received?
    – Phil W
    Commented Apr 23 at 22:05

0

You must log in to answer this question.

Browse other questions tagged .