Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How To Resolve The Below Callout Error?

Asked by Camellia Kleiber Feb 8, 2023 3.7K views 2 answers
Share

About this question

 I have written an inline Vf page to update the record value. The values are coming from a web service. While updating the record the detail page redirected to the VF page. But the record was committed in the database with the error in debug log

"You have uncommitted work pending. Please commit or rollback before calling out"

I want to save the value in the record and the page remains in the same detail page. How could I achieve this?

controller:

public with sharing class  ServiceOptionsChange_CLS{ 
public string strId;
public String strOppid{get;set;}
public String strModelCode {get;set;}
public String strVersionCode {get;set;}
public List lstOptions {get; private set;}
List lstC2Goptions= new List();
public String strop{get;set;}
public Service__c objService{get;set;}
public boolean refreshPage{get;set;}
public String strCode {get;set;}
public String strLabel {get;set;}
public Map mapOptionCodePrice = new Map();
public ServiceOptionsChange_CLS(ApexPages.StandardController controller) {
    objService = new Service__c();
    strId=ApexPages.currentPage().getParameters().get('id');
    Service__c objService=[select Opportunity_car_set__c from Service__c where id=:strId]; 
    strOppid=objService.Opportunity_car_set__c; 
    Opportunity_car_set__c opp=[SELECT Model__r.ProductCode,Version__r.version_code__c,Country_Code__c FROM Opportunity_car_set__c where id=:strOppid];    
    strModelCode    = opp.Model__r.ProductCode;
    strVersionCode  = opp.Version__r.version_code__c;
    system.debug('<<<<
  } 


public List getOptions(){
   OptionDetails_CLS optionValue=new OptionDetails_CLS();
    if(String.isNotBlank(strModelCode) && String.isNotBlank(strVersionCode)){
        lstC2Goptions=optionValue.getPriceListbasedOnReference(strModelCode,strVersionCode);
   }
    if(lstC2Goptions.size()>0){
        lstOptions = new List();
        lstOptions.add(new SelectOption('','--Select--'));
        for(String strC2Goptions:lstC2Goptions) {
            lstOptions .add(new SelectOption(strC2Goptions,strC2Goptions));
        }
    }
  return lstOptions ;
}
public Pagereference customSave(){
    strop=objService.CodeC2G__c;
    String strQuantity=String.valueof(objService.Quantity__c);
    system.debug('<<<<    strCode=strop.substringbefore('-').trim();
    strLabel =strop.substringafter('-').trim();
    system.debug('<<<<    objService.id=strId;
    objService.Code__c=strCode;
    objService.Label__c=strLabel;
    Double dbPriceHT;
    OptionDetails_CLS optionValues =new OptionDetails_CLS();     
    mapOptionCodePrice = optionValues.getOptionPrice(strModelCode,strVersionCode);
    if (mapOptionCodePrice.containsKey(strCode)) {
        dbPriceHT = mapOptionCodePrice.get(strCode);
    }
    if (dbPriceHT!=null) {
        objService.Price_HT__c=dbPriceHT;
    }
    try{
        update objService;
        refreshPage=true;
        system.debug('<<<<        return null;
    }
    catch(Exception ex){
        System.debug('Error in SOQL Fetching'+ex);
        ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, ex.getMessage() );
        ApexPages.addMessage(msg);
       return null;
        }
}
 }
vf:


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 8, 2024

Without the specific details of the error message, it's difficult to provide a tailored solution. However, generally speaking, resolving a callout error in programming involves understanding the context of the error and addressing the root cause. Here are some general steps you can take to resolve a callout error:


Understand the Error Message: Read the error message carefully to understand what the problem is. It might provide clues about what went wrong.

Check Documentation: Consult the documentation or resources related to the technology or library you're using. Often, errors are documented with explanations and possible solutions.

Review Code: Look at the code that's causing the error. Check for any obvious mistakes or issues that might be causing the callout error.

Debugging: If possible, use debugging tools or techniques to step through the code and identify where the error occurs. This can help pinpoint the problem more precisely.

Validate Inputs: Ensure that the inputs to the function or method causing the error are valid and properly formatted. Sometimes, errors occur due to incorrect inputs.

Handle Exceptions: If the error is due to exceptional conditions, make sure that your code properly handles exceptions and errors. This might involve adding try-catch blocks or other error-handling mechanisms.

Check Connectivity: If the callout involves network communication (e.g., making HTTP requests), verify that your network connection is working properly and that you have the necessary permissions and access rights.

Review Permissions and Settings: Make sure that your application has the necessary permissions and settings to perform the callout. For example, if you're accessing an external API, check that you have the correct API key or authentication credentials.

Consult Forums or Community: If you're still unable to resolve the error, consider asking for help on forums, community websites, or Q&A platforms. Other developers might have encountered similar issues and can provide insights or solutions.

Contact Support: If you're using a third-party service or library and unable to resolve the error on your own, consider contacting their support team for assistance. They might be able to offer guidance or troubleshoot the issue with you.

By following these steps and investigating the specific details of the error message, you can often identify and resolve callout errors in your code.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.