1

I have a Custom Object called Opportunity_carset.the related list of Opportunity Carset Object is Service.

The Opportunity Carse Object has two fields.

  1. Model
  2. Version

    • The Service object has some Picklist Field called CodeLabel

    • Model and Version values from "Opportunity Carset" are sent as parameter to webservice class to retrieve the "service" Object Custom Picklist values.

My scenario:

Whenever am creating the "Service" record by clicking "New Service" from realted list of "Opportunity carset" Object,the Custom Picklist values should be displayed in Service Object.

Here,

I have created a apex class which take the Model and Version values and will give the custom Picklist values.

Apex code

public class Rfleet_DZC2GOptionDetails_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 strMarketingModelURL {get;set;}
public String strPriceListURL {get;set;}
public String strReferenceValue {get;set;}
public string  strOptionCode;
public string  strOptionLabel;
public String strConcatOptionCodeLabel; 
public List<service__c> lstServiceRecord= new List<service__c>();  //to insert records in service object
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 List<String> lstFinalContactOptionCodeLabel = new List<String>();  //List of Option codes with Label
public Map<String,String> mVersion     = new map<String,String>();  //Contains model code+doc url
public Map<String,String> mVersionVal  = new map<String,String>();  //contains specific version doc url
public Map<String, String> mapOptionCodeLabel = new Map<String,String>(); //Option code and label from Marketing URL
public Map<String, Double>  mapOptionCodePrice = new Map<String, Double>(); //Option code and Price from Price URL
Rfleet_JSON2ApexC2G_CLS obj = null;

//Constructor used to get the modelcode and version code  

public Rfleet_DZC2GOptionDetails_CLS() {
    Rfleet_CountryInfo__c cs = Rfleet_CountryInfo__c.getInstance('Algeria');
    strGeturl         =    cs.Rfleet_C2GURL__c;
}
public Rfleet_DZC2GOptionDetails_CLS(String strOppid) {
    system.debug('strOppid >>>>'+ strOppid);
    Rfleet_CountryInfo__c cs = Rfleet_CountryInfo__c.getInstance('Algeria');
    strGeturl         =    cs.Rfleet_C2GURL__c;
}

public Rfleet_DZC2GOptionDetails_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;
    system.debug('strModelCode >>>>'+ strModelCode);
    system.debug('strVersionCode >>>>'+ strVersionCode);

}
//Common method to Parse the JSON
public String init(String strVersionURL) {

    system.debug('## Inside init :: strVersionURL is..::'+ 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() {

    system.debug('## Inside  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 return the OptionCode and Label after getting the Price and Marketing URL for the Version

public Map<String, String> fetchModelSpecCodeandPricListURL( String strModelCode, String strVersionCode) {

   system.debug('## Inside fetchModelSpecCodeandPricListURL method ##');
    mVersionVal=getJSONFromREST();

    system.debug('## mVersionVal is..::'+ mVersionVal);
    system.debug('## strModelCode is..::'+ strModelCode);


    if(mVersionVal.containsKey(strModelCode.trim())) {
        system.debug('## Inside if condition ##' + strModelCode);
        strVersionDocUrl= mVersionVal.get(strModelCode.trim());
    }

    system.debug('## strVersionDocUrl is..::'+ strVersionDocUrl);
    strJSON = init(strVersionDocUrl); 

    system.debug('## fetchModelSpecCodeandPricListURL :: strJSON..::' +strJSON );

    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);
                }
                if (fieldName == 'marketingModelPresentation') {
                    parser.nextToken();
                    strMarketingModelURL = parser.getText();
                    system.debug('## strMarketingModelURL  :: >>>>'+strMarketingModelURL);
                }
            }    
    }
    if (strMarketingModelURL !=null && strMarketingModelURL.length()>0) {
        mapOptionCodeLabel = getOptionCodelabel(strMarketingModelURL);  
    }

    return  mapOptionCodeLabel;       
}


//Used to get the OptionCode and Label from MarketModel URL
public Map<String, String> getOptionCodelabel(String strMarketingModelURL) {
    strJSON = init(strMarketingModelURL); 
    JSONParser parser = JSON.createParser(strJSON);
    while (parser.nextToken() != null) {
        if (parser.getCurrentToken() == JSONToken.FIELD_NAME){
            String fieldName = parser.getText();
            if(fieldName == 'map') {
                while(parser.nextToken() != null) {                    
                    if(parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                        String mapName = parser.getText();
                        if(mapName == 'presentationItem'){
                            Integer i=0;
                            while(parser.nextToken() != null){
                                if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
                                    String Oppname = parser.getText();
                                    if(Oppname == 'null' || Oppname == 'fr'){
                                        parser.nextToken();
                                        strOptionLabel=parser.getText();
                                        i=i+1;
                                    }  
                                    if(Oppname == 'key'){
                                        parser.nextToken();
                                        strOptionCode=parser.getText();
                                        i=i+1;
                                    } 
                                    if(i==2) break;                                   
                                }
                            }
                            if(strOptionCode!=null || strOptionCode!='')
                                mapOptionCodeLabel.put(strOptionCode,strOptionLabel);
                                system.debug('mapOptionCodeLabel>>>>'+mapOptionCodeLabel);
                        }
                    }
                }      
            }
        }          
    }   
    system.debug('mapOptionCodeLabel===>'+mapOptionCodeLabel);
    return mapOptionCodeLabel;
} 


//Used to Get the final OptionCode and Label
public List<string> getPriceListbasedOnReference(String strModelCode, String strVersionCode) {

        system.debug('## Inside getPriceListbasedOnReference :: strModelCode::'+ strModelCode +'\t strVersionCode >>::'+strVersionCode);
        mapOptionCodeLabel = fetchModelSpecCodeandPricListURL(strModelCode, strVersionCode);

        // Parsing the strPriceListURL to retrieve PriceType - reference ="PVCHT" and and its map values
        String strJSON = init(strPriceListURL);

        obj = Rfleet_JSON2ApexC2G_CLS.parse(strJSON);
        // To iterate the PriceList values
        String str = obj.requestedURI;
        List<Rfleet_JSON2ApexC2G_CLS.PriceList> listPriceList  = obj.PriceList;  
        // codition to check when the PriceList is returning empty 
        if (listPriceList !=null && listPriceList.size()>0) {

            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;
                        double strversionPrice = listVersionPriceList.get(i).price; 
                        if (strversionIdSpecCode==strVersionCode) {
                            Rfleet_JSON2ApexC2G_CLS.OptionsPrices optionPrice = (Rfleet_JSON2ApexC2G_CLS.OptionsPrices )listVersionPriceList.get(i).OptionsPrices ;
                            Rfleet_JSON2ApexC2G_CLS.MapRepresentation mapRepresentaion = (Rfleet_JSON2ApexC2G_CLS.MapRepresentation)optionPrice.MapRepresentation  ;
                            Rfleet_JSON2ApexC2G_CLS.Map_Z mapValues= (Rfleet_JSON2ApexC2G_CLS.Map_Z)mapRepresentaion.Map_Z ;
                            mapOptionCodePrice = mapValues.mapZ; 
                        }    
                    }
                } 
            }  

        Set<String> setOptionID = mapOptionCodeLabel.keySet();
        for(String strIds:setOptionID){ 
            if(mapOptionCodePrice.containsKey(strIds)) {      
                String OptionCode=strIds;
                String OptionLabel=mapOptionCodeLabel.get(OptionCode);
                strConcatOptionCodeLabel= OptionCode+'-'+ OptionLabel;
                lstFinalContactOptionCodeLabel.add(strConcatOptionCodeLabel);
            }
        }
       }// end of if loop for PriceList is not empty  
     return   lstFinalContactOptionCodeLabel;//return the Picklist values
} 

}

This class will return the Picklist Options.Using MetaDataApi I can update the picklist field values in"Service" object.But this all to be happen while creating a record from Relatedlist.Give me some Idea!!!

  • Uma.. what you can do.. You can create a batch that will run before creating any records.. May be you can run this batch every sunday then this will create picklist value in every sunday.. Looks like you want this to be run only once ? correct if yes then before giving this app live just run this callout once.. – Ratan Paul Mar 2 '16 at 7:33
  • @Ratan Everytime the user creates the record from related list the Picklist Values should be displayed for User.Evert time the picklist value may change depending upon the selection of Model and version value – uma451 Mar 2 '16 at 7:36
  • Uma.. what is this Model and version.. It is fields on Parent object.. ? If yes then how many Model and version User can select. – Ratan Paul Mar 2 '16 at 7:38
  • Yes.The user select only one model and version in Opportuninty car set object.Depending upon the selection of model and version the PicklistValues may differ.model and version values sent as parameter to webservice and will give the list of PicklistValues – uma451 Mar 2 '16 at 7:42
  • Uma.. Total how many picklist value any we have.. IF few example total 20 -30 .. We can create recordType and assign the picklist values ? – Ratan Paul Mar 2 '16 at 8:45
1

So basically you have to use VF page so when you try to create service record picklist option should populated with webservice retured values.

  1. You need to override new button of service related list so onclick of that button user should redirect to VF page.
  2. Pass your Opportunity_carset id in url and get that id in VF page controller.
  3. Based on Opportunity_carset id query Model and Version fields.
  4. Now make callout with Model and Version fields and get all the picklist options.
  5. Pass all options to page picklist.
  6. Now user can save this record..

This way we can make the picklist value based on parent Model and Version fields and callout.

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.