+ Start a Discussion
Umamageshwari PalanisamyUmamageshwari Palanisamy 

How to update a custom field while inserting and updating the record using trigger?

 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
 
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]){
   system.debug('oppcarset.id----------------->'+oppcarset.id);
   system.debug('oppcarset.Model__r.Name----------------->'+oppcarset.Model__r.Name);
   system.debug('oppcarset.Version__r.Name----------------->'+oppcarset.Version__r.Name);
   modelOppValue = oppcarset.Model__r.Name;
   versionOppValue = oppcarset.Version__r.Name; 
   //Rfleet_DZVersionPrice_CLS.doCallout(modelOppValue,versionOppValue);
   Rfleet_DZVersionPrice_CLS.chkVersionCallOut(modelOppValue,versionOppValue);

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

}
My  apex class:
global class Rfleet_DZVersionPrice_CLS {
static String strModelName;
static String strVersionName;
static String strOppid;
@future(callout=true)
public static void chkVersionCallOut(string id,string modelName,string versionName) {
    strOppid =id;
    strModelName = modelName;
    strVersionName = versionName;
    System.debug('strModelName -------------------------------->'+strModelName);
    System.debug('strVersionName -------------------------------->'+strVersionName);
    Rfleet_DZC2GVersionPriceDetails_CLS dzVersionPrize = new Rfleet_DZC2GVersionPriceDetails_CLS(strOppid,strModelName,strVersionName);
    ProcessorControl.inFutureContext = true;
    Double VerPrice=dzVersionPrize.getVersionPrice();
    Opportunity_car_set__c opp=[SELECT Price_HT__c FROM Opportunity_car_set__c where id=:strOppid ];
    system.debug('opp>>'+ opp);
    opp.Price_HT__c=decimal.Valueof(VerPrice);
    system.debug('opp next>>'+ opp);
    Update opp;
}

}

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 string strModel{get;set;}
public string strVersion{get;set;} 
public string strid{get;set;} 
public Rfleet_DZC2GVersionPriceDetails_CLS(string strOppid,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;
    strModel=strModelName;
    strVersion=strVersionName;
    strid=strOppid;
    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 =: strModel and Version__r.name =: strVersion and id=:strid];    
    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();
        httpReq.setEndpoint(strVersionURL);
        httpReq.setHeader('Accept','application/JSON');
        httpReq.setMethod('GET');
        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') { 
                parser.nextToken();
                lstModelcalOut.add(parser.getText());
            } 
            if(fieldName == 'doc') {
                parser.nextToken();
                lstDoCcalOut.add(parser.getText());
            } 
        }
    }
    for(integer i=0;i<lstModelcalOut.size();i++) {
        mVersion.put(lstModelcalOut.get(i),lstDoCcalOut.get(i));
    }
    system.debug('mVersion===>'+mVersion);
    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') {
                    parser.nextToken();
                    strPriceListURL=parser.getText();
                    system.debug('strPriceListURL>>>>'+strPriceListURL);
                }
            }    
    }
    return strPriceListURL;         
}
public double 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);
        return strversionPrice;
  
} 

}

apex - How to update a custom field while inserting and updating the record using trigger? - Salesforce Stack Exchangeapex class to prevent recursive run:**
public class ProcessorControl {  
     public static boolean inFutureContext = false;
}
My problem is:

While inserting  and modifying the 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 !!!



.
RAM AnisettiRAM Anisetti
Hi,

Check below line of ur trigger code,you are not sending Opportunity_car_set__c id
 
Rfleet_DZVersionPrice_CLS.chkVersionCallOut(modelOppValue,versionOppValue);

​modifie ur code like this

Rfleet_DZVersionPrice_CLS.chkVersionCallOut(oppcarset.id,modelOppValue,versionOppValue);



 
Umamageshwari PalanisamyUmamageshwari Palanisamy
Sorry.I have passed the value.While updating the code i missed