+ Start a Discussion
Amita TatarAmita Tatar 

remove elements from wrapper list in salesforce

Hi all,

I have a Map in which i have Sobject fields and labels and i am adding these labels to my wraper list and showing it on VF page.
I want to show only the custom field labels here. So i am trying to add condition while adding the labels in wrapper list...but cannot do it.
Can you help? Here is my code:
APEX class::



public class getLabel{

Public Proposal_Form__c pfc = new Proposal_Form__c();
public List<WrapperClass> listWrapper {get;set;}
public Map<String,String> labelMap;
public String resultString {get;set;} 
public ApexPages.StandardController controller; 

public getLabel(ApexPages.StandardController Controller) {
    pfc = (Proposal_Form__c)Controller.getRecord(); 
    this.controller = Controller;  
    listWrapper = new List<WrapperClass>();
    labelMap = new Map<String,String>();
    labelMap = retLabelMap('ProposalFields__c');
    system.debug('******LabelMap***'+labelMap);
        for(String s : labelMap.values()){
            listWrapper.add(new WrapperClass(s,false));
        }
       
   }

public class WrapperClass {  
    public Boolean checkBool {get;set;}
    public String fieldNme{get;set;}  
    public WrapperClass(String prop,Boolean checkBool ){  
        this.fieldNme = prop;  
        this.checkBool = checkBool ;  
    }  
}

public Static Map<String,String> retLabelMap(String type){
    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
    Schema.SObjectType s= m.get(type);
    Map<String, Schema.SObjectField> fieldMap = s.getDescribe().fields.getMap();
    Map<String,String> aMap = new Map<String,String>();
    for (String fieldName: fieldMap.keySet()) {
        aMap.put(fieldName,fieldMap.get(fieldName).getDescribe().getLabel());
    }
    return aMap;
}

public PageReference saveCheckboxValue(){
   if(listWrapper!=null && listWrapper.size()>0){
        for(WrapperClass w : listWrapper){
            if(w.checkBool == true){
              
                   if(resultString!=null){
                        resultString = resultString + '\n'+ w.fieldNme;
                   }
                   else{
                        resultString = w.fieldNme;
                   }
            }
          }
        }
     
     pfc.Fields_Associated__c = resultString; 
     try
     {
         insert pfc;
         PageReference pg = new PageReference('/'+pfc.id);
         return pg;
     }
     catch(dmlexception e)
     {
           apexpages.addmessages(e);
           return null;
     }

}
}
I am trying to add some if condition in the bold part of my code.

Thanks,
Amita
 
Best Answer chosen by Amita Tatar
VivekShindeVivekShinde
Hi Amita,

You have mentioned in your query that you want to show only the custom fields on the visualforce page. It is possible to know whether a field is a custom field by making use of isCustom() method of DescribeFieldResult class. You need to modify your retLabelMap method as follows:
public Static Map<String,String> retLabelMap(String type){
    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();

    Schema.SObjectType s= m.get(type);

    Map<String, Schema.SObjectField> fieldMap = s.getDescribe().fields.getMap();

    Map<String,String> aMap = new Map<String,String>();

    for (String fieldName: fieldMap.keySet()) {
        if(fieldMap.get(fieldName).getDescribe().isCustom()) {
            aMap.put(fieldName,fieldMap.get(fieldName).getDescribe().getLabel());
        }
    }

    return aMap;

}
Let me know if this works.

Thanks,
Vivek Shinde

All Answers

VivekShindeVivekShinde
Hi Amita,

You have mentioned in your query that you want to show only the custom fields on the visualforce page. It is possible to know whether a field is a custom field by making use of isCustom() method of DescribeFieldResult class. You need to modify your retLabelMap method as follows:
public Static Map<String,String> retLabelMap(String type){
    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();

    Schema.SObjectType s= m.get(type);

    Map<String, Schema.SObjectField> fieldMap = s.getDescribe().fields.getMap();

    Map<String,String> aMap = new Map<String,String>();

    for (String fieldName: fieldMap.keySet()) {
        if(fieldMap.get(fieldName).getDescribe().isCustom()) {
            aMap.put(fieldName,fieldMap.get(fieldName).getDescribe().getLabel());
        }
    }

    return aMap;

}
Let me know if this works.

Thanks,
Vivek Shinde
This was selected as the best answer
Amita TatarAmita Tatar
Thanks a lot Vivek. That helped me out.

-Amita