I have custom object called Opportunity car set object. In the object there are 5 fields. name, brand, model name(lookup to product),version name (lookup to product),price Depending upon the selection of model name and version name the price field should be updated. The model name and version name are sent as parameter to get the price value from web service.

My trigger:

trigger Rfleet_OpptyUpdateC2GPrice on Opportunity_car_set__c (before insert,before update) {

Public String modelOppValue;
Public String versionOppValue;

  for(Opportunity_car_set__c oppcarset:[select id,Price_HT__c,Model__r.Name,Version__r.Name from Opportunity_car_set__c  where id in:trigger.new]){
   modelOppValue = oppcarset.Model__r.Name;
   versionOppValue = oppcarset.Version__r.Name; 

      system.debug('modelOppValue--------------------->'+ modelOppValue);
      system.debug('versionOppValue --------------------->'+ versionOppValue);


Apex class:

global class Rfleet_DZVersionPrice_CLS {
static String strModelName;
static String strVersionName;
static String strOppid;
public static void chkVersionCallOut(string modelName,string versionName) {
    strModelName = modelName;
    strVersionName = versionName;
    System.debug('strModelName -------------------------------->'+strModelName);
    System.debug('strVersionName -------------------------------->'+strVersionName);
    Rfleet_DZC2GVersionPriceDetails_CLS dzVersionPrize = new Rfleet_DZC2GVersionPriceDetails_CLS(strModelName,strVersionName);
    ProcessorControl.inFutureContext = true;


Webservice class:

public class Rfleet_DZC2GVersionPriceDetails_CLS {
public String strJSON {get;set;}
public String strGeturl {get;set;}
public String strGetcountrycode {get;set;}
public String strGetCurrency {get;set;}
public String strModelCode {get;set;}
public String strVersionCode {get;set;}
public String strVersionDocUrl {get;set;}
public String strPriceListURL {get;set;}
public String strOppid {get;set;}
public double strversionPrice{get;set;}
public List<String> lstModelcalOut = new List<String>(); //to get list of model code
public List<String> lstDoCcalOut = new List<String>(); //to get list of docurls
public Map<String,String> mVersion     = new map<String,String>();  //Contains model code+doc url
Rfleet_JSON2ApexC2G_CLS obj = null;
public Rfleet_DZC2GVersionPriceDetails_CLS(String strModelName, String strVersionName) {

    Rfleet_CountryInfo__c cs = Rfleet_CountryInfo__c.getInstance('Algeria');
    strGeturl         =    cs.Rfleet_C2GURL__c;
    strGetcountrycode =    cs.Rfleet_CountryCode__c;
    strGetCurrency    =    cs.Rfleet_Currency__c;
    Opportunity_car_set__c opp=[SELECT Model__r.ProductCode,Version__r.Rfleet_C2G_version_code__c FROM Opportunity_car_set__c where Model__r.name =: strModelName and Version__r.name =:      strVersionName];    
    strModelCode    = opp.Model__r.ProductCode;
    strVersionCode  = opp.Version__r.Rfleet_C2G_version_code__c;
    strOppid        = opp.id;
    system.debug('<<strOppid'+ strOppid);


//Common method to Parse the JSON
public String init(String strVersionURL) {
    try {
        Http http = new Http();
        HttpRequest httpReq = new HttpRequest();
        HttpResponse response = http.send(httpReq);
        strJSON= response.getBody();

    } catch (Exception ex) {
        system.debug('<<Method: init Exception ::'+ ex);
    return  strJSON;     

//Used for Getting ModelCode and Doc URL in Map
public Map<String, String> getJSONFromREST() {
    strJSON = init(strGeturl); 
    JSONParser parser = JSON.createParser(strJSON);
    while(parser.nextToken() !=null) {
        if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
            String fieldName = parser.getText();
            if(fieldName == 'modelSpecCode') { 
            if(fieldName == 'doc') {
    for(integer i=0;i<lstModelcalOut.size();i++) {
    return mVersion;       

//Used to get Version Price URL
public String getVersionPriceURL() {
     Map<string,String> mVersionVal=getJSONFromREST();
    if(mVersionVal.containsKey(strModelCode)) {
        strVersionDocUrl= mVersionVal.get(strModelCode);
    strJSON = init(strVersionDocUrl); 
    JSONParser parser = JSON.createParser(strJSON);
    while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String fieldName = parser.getText();
                if(fieldName == 'pricesList') {
    return strPriceListURL;         
public void getVersionPrice() {
       String strVersionPriceURL=getVersionPriceURL();
       String strJSON = init(strVersionPriceURL);
       obj = Rfleet_JSON2ApexC2G_CLS.parse(strJSON);
       List<Rfleet_JSON2ApexC2G_CLS.PriceList> listPriceList  = obj.PriceList;  
       for(Rfleet_JSON2ApexC2G_CLS.PriceList priceList : listPriceList) {
            String strPriceTypeReference = (String)priceList.priceType.reference;
            List<Rfleet_JSON2ApexC2G_CLS.VersionPriceList>  listVersionPriceList;
            if(strPriceTypeReference =='PVCHT') {
                listVersionPriceList = new List<Rfleet_JSON2ApexC2G_CLS.VersionPriceList>();
                listVersionPriceList = priceList.versionPriceList;
                for(integer i=0; i< listVersionPriceList.size(); i++) {
                    String strversionIdSpecCode = listVersionPriceList.get(i).versionIdSpecCode;
                    if(strversionIdSpecCode==strVersionCode) {
                      strversionPrice = listVersionPriceList.get(i).price;
                      system.debug('<<<<<<<<<VersionId and Price'+ strversionPrice);  

        system.debug('<<<<<<<<<VersionId outside Price'+ strversionPrice);
        Opportunity_car_set__c opp=[SELECT Price_HT__c FROM Opportunity_car_set__c where Model__r.name =:strModelName and Version__r.name =:strVersionName];
        system.debug('opp>>'+ opp);
        system.debug('opp next>>'+ opp);
        Update opp;



*apex class to prevent recursive run:**

public class ProcessorControl {  
     public static boolean inFutureContext = false;

My problem is:

While inserting new record price value is not updated.While modifying the record the value is updated after refreshing the page.please help me out!!!thanks in advance !!!


The webservice call tries to query the record even before its inserted I beleive.

Try changing operation to after insert and after update in the trigger instead of before insert and before update respectively

trigger Rfleet_OpptyUpdateC2GPrice on Opportunity_car_set__c (after insert,after update) {
    if(system.isFuture()) return;


Added check in trigger to avoid recursion

if(system.isFuture()) return;
| improve this answer | |
  • 2
    And after update, too. – sfdcfox Feb 29 '16 at 7:46
  • when I try after insert the price value is retrieved from web service. but it is not updated.It throws error "System.AsyncException: Future method cannot be called from a future or batch method: Rfleet_DZVersionPrice_CLS.chkVersionCallOut" – uma451 Feb 29 '16 at 8:02
  • sfdcfox thank you, I updated the answer to reflect the same. @uma see my update, you need to add if(system.isFuture()) return; to avoid recursion on update from future class – karthikselva Feb 29 '16 at 8:08
  • Hi@karthikselva.Thanks for the update.The value was retrived from weebservice while inserting and updating a record.But after refreshing the page only the value is updated.have to refresh the Page ?Please help me !! – uma451 Feb 29 '16 at 8:35
  • @uma Yeah since its asynchronous you can't expect the page to reflect immediately, – karthikselva Feb 29 '16 at 8:41

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.