0

As per the requirement we have a quick action on lightning and on the load of the lightning quick action we are calling external service and after that updating the object record in future method.

On load of the page when I first click the action its giving me the exception and the after that everything is working fine.

The issue could be salesforce related but I am trying to debug the code.

this method is used for call out

// This method generates the token and returns it

public static IAMResponse generateToken(WebserviceConfiguration__c oConfiguration){
    IAMResponse response;
    HttpRequest oRequest = new HttpRequest();
    oRequest.setEndpoint('callout:'+oConfiguration.NamedCredentials__c);
    oRequest.setMethod('POST');
    oRequest.setTimeout(Integer.valueOf(oConfiguration.CalloutTimeOut__c));
    oRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    Http http = new Http();
    // send the request
    HttpResponse oResponse = http.send(oRequest);

    if(oResponse.getStatusCode() == 200){
        system.debug('statusCode'+oResponse.getStatusCode());
        Map<String,Object> result = (Map<String,Object>)JSON.deserializeUntyped(oResponse.getBody());
        updateCurrentConfiguration(String.ValueOf(result.get('access_token')),
                                   Integer.valueOf(result.get('expires_in')),
                                   oConfiguration.Id);
 }else{
        Map<String,Object> result = (Map<String,Object>)JSON.deserializeUntyped(oResponse.getBody());
        response = new IAMResponse(NULL,
                                   System.Label.IAM_APIErrorPrefix+' '+result.get('error_description')+' '+System.Label.IAM_APIErrorSuffix);
        Error_Log__c oError = new Error_Log__c(Method_Name__c='generateToken',
                                               Class__c='IAM_Authorization',
                                               Error_Message__c=String.valueOf(result.get('error')),
                                               Error_Type__c=String.valueOf(result.get('error_description')));
        insert oError;
    }

    return response;
}

// method call to update the object

 @future
public static void updateCurrentConfiguration(String access_token,Integer expires_in,String oConfigurationId){
    WebserviceConfiguration__c oConfiguration = [select id,
                                                 AccessToken__c,
                                                 TokenExpirationInterval__c,
                                                 TokenValidTill__c,
                                                 LastRun__c from WebserviceConfiguration__c where Id=: oConfigurationId];
    oConfiguration.AccessToken__c = encryptToken(access_token);
    oConfiguration.TokenExpirationInterval__c = expires_in;
    oConfiguration.TokenValidTill__c = 
    System.Now().addSeconds(expires_in);
    oConfiguration.LastRun__c = System.Now();

    Database.SaveResult oDSR = Database.Update(oConfiguration);
    List<Database.SaveResult> dsrList = new List<Database.SaveResult>();
    dsrList.add(oDSR);
    system.debug('dsrList'+dsrList);
    if(!ExceptionHelper.processSaveResult(dsrList,'IAM_Authorization','generateToken','WebserviceConfiguration__c')){
        system.debug('#error'+System.Label.IAM_DMLErrorMessage);
    }

}
  • provide apex code, so that we can help you. and line, where this exception is throwing – Oleksandr Berehovskyi Jul 17 '19 at 18:42
  • Depending on the order of operations, this can happen. We'd need to see your component's JavaScript to help debug the issue. – sfdcfox Jul 17 '19 at 18:47
  • This is not enough code context to troubleshoot the problem. What happens before you make the callout? – David Reed Jul 17 '19 at 21:41
0

According to your description, you need to redirect after that update is done before you make the callout to complete the transaction flow. There is not really enough code in your example, however, this is typically the error that you would get if you perform a DML operation before a callout is made and you have not redirected which would commit the changes. This one article HERE has an interesting approach as well.

Knowledge Article: https://help.salesforce.com/articleView?id=000326129&type=1&mode=1

Documentation (Testing): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing_dml.htm?search_text=Callout

|improve this answer|||||
  • The update method I am calling it's a future method. – Edward Jul 18 '19 at 5:59
  • I think i got the error , after the callout object updation is done in Future method than again we have a callout for other service , may be there is something failing because of DML. – Edward Jul 18 '19 at 15:50
  • have you tried to make a call to refreshView in your component upon your response from your callout? – Ronnie Jul 18 '19 at 17:58
  • That would not help as when getting callout exception , no status will return on the component side but blank. – Edward Jul 18 '19 at 20:39
  • Have you considered a catch for your callout exception so that you know what step to take next? – Ronnie Jul 23 '19 at 19:09
0

Issue is fixed now ,according to document when you call future method it create a job that implicitly . Thats why it happend as a solution i change the flow and execeuted the DML at last of all the callout.

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.