0

I'm posting this question previously but i'm not achieving my goal for that i posting again

I have a custom object called Incident__c. and it has some fields

  1. Imtpact__c
  2. Incident_state__c
  3. Short_description__c
  4. Contact_type__c

Note : all fields are check box fields

from visualforce page i'm saving Incident__c record

when i'm saving the record i checked only two fields and the remaining two fields are unchecked and i saved that record.

now my requiremt is i want two query only those checked two fields from the saved record

how can i achieve this

this is my apex class for saving the Incident records

public class DataInsert {

     public Boolean impact {get;set;} 
     public Boolean incident_state {get;set;} 
     public Boolean short_description {get;set;} 
     public Boolean sys_id {get;set;} 
     public Boolean contact_type {get;set;} 
     public Boolean u_sftype {get;set;} 
     public Boolean sys_updated_onONLast {get;set;}


     List<Incident__c> lst = new List<Incident__c>();

     public PageReference doInsert() {

        Incident__c objdlt = new Incident__c();   

       if(impact){ 
          objdlt.impact__c=impact ;
        }       

        if(incident_state){
           objdlt.incident_state__c=incident_state;
        }

        if(short_description){
           objdlt.short_description__c=short_description;
        }

        if(contact_type){
           objdlt.contact_type__c=contact_type;
        }


        insert objdlt; 
        lst.add(objdlt);

     }      
}

Here suggest anyway to get the only checked fields from the saved Incident__c record.

Thanks In Advance

  • which ones are checkbox fields? – Santanu Boral Apr 26 '17 at 5:05
  • @santanu Boral thanks for replying all fields are checkbox fields among all i checked only some fields and the remaining are un-checked and saved that record.now i want to read only checked fields from that record (this will be working dynamically for furture changing those checked fields) – Srikar Apr 26 '17 at 5:18
1

Hi here is an example which you can try it out,

Account acc = new Account(name='acc', myCheckbox__c = true);
insert acc;
Schema.DescribeSObjectResult objSchema = Account.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getMap();
Set<String> fields = new Set<String>(); 
for (String fieldName : fieldMap.keySet()){
    SObjectField sfield = fieldMap.get(fieldName);
    schema.describefieldresult dfield = sfield.getDescribe();
    if(acc.get(fieldName) == true) {
        if(dfield.isCustom() && String.valueOf(dfield.getSoapType()) == 'Boolean') {
            system.debug(acc.get(fieldName));
            fields.add(fieldName);
        }  
    }
}

in this example you in fieleds set you will get the set of fields which is checked(true). In this example I'm considering only custom fields. Hope it helps you.

0

if you want all checked fields through query you can get by querying your object --

[Select your fields reqiued FROM Custom Object Name WHERE Checkbox1=True || Checkbox2=True || Checkbox3=True || Checkbox4=True]

like this you can get only the checked fields.please let me know if it works !!

  • this query isn't satisfy my requirement.because where condition isn't suitable here because it retrieving fields any one checkbox field is true.but here i need only checked fields among all checked and un-checked fields – Srikar Apr 26 '17 at 5:52
  • @Aarya this query isn't working for my requirement.please see the below avijit and my conversation below you w'll get my requirement . i need some help – Srikar Apr 26 '17 at 6:13
0

I have modified the code and comment on the changes : -

public class DataInsert {

     public Boolean impact {get;set;} 
     public Boolean incident_state {get;set;} 
     public Boolean short_description {get;set;} 
     public Boolean sys_id {get;set;} 
     public Boolean contact_type {get;set;} I have modified the code and comment on the changes : -

    public class DataInsert {

         public Boolean impact {get;set;} 
         public Boolean incident_state {get;set;} 
         public Boolean short_description {get;set;} 
         public Boolean sys_id {get;set;} 
         public Boolean contact_type {get;set;} 
         public Boolean u_sftype {get;set;} 
         public Boolean sys_updated_onONLast {get;set;}


         List<Incident__c> lst = new List<Incident__c>();
         **List<Incident__c> lst_selected_check = new List<Incident__c>();//only stores those records for which check box is checked.** 

         public PageReference doInsert() {

            Incident__c objdlt = new Incident__c();   

           if(impact){ 
              objdlt.impact__c=impact ;
            }       

            if(incident_state){
               objdlt.incident_state__c=incident_state;
            }

            if(short_description){
               objdlt.short_description__c=short_description;
            }

            if(contact_type){
               objdlt.contact_type__c=contact_type;
            }

            insert objdlt; 
            **//check either one of your checkbox is checked then add to the list
            Schema.DescribeSObjectResult objSchema = Incident__c.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getMap();
            Set<String> fields = new Set<String>(); 
            for (String fieldName : fieldMap.keySet()){
                SObjectField sfield = fieldMap.get(fieldName);
                schema.describefieldresult dfield = sfield.getDescribe();
                if(objdlt.get(fieldName) == true) {
                    if(dfield.isCustom() && String.valueOf(dfield.getSoapType()) == 'Boolean') {
                        lst_selected_check.add(objdlt);     
                    }  
                }
            }**         
            lst.add(objdlt);

         }      
    }

i think this will solve your purpose.

  • thanks for replying if i have 100 fields this approach isn't good i think so please suggest it for working dynamically – Srikar Apr 26 '17 at 5:55
  • 1
    yes this can be done as well, Use "Schema.DescribeFieldResult" for tour custom object to know how many checkbox you have for your custom object and then you can dynamically iterate the "objdlt" for your 100 checkbox field. – Avijit Das Apr 26 '17 at 6:04
  • Thanks for replying again exactly that is my requirement what you said could you please help me some basic code for that i already tried but their is no luck,suggest any example – Srikar Apr 26 '17 at 6:09
  • @venkat, have updated the code it will now as you expected. – Avijit Das Apr 26 '17 at 6:36
  • now i'm modifying the code as per blackperl sample code in the above – Srikar Apr 26 '17 at 6: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.