+ Start a Discussion
madhavi21.3966038613070422E12madhavi21.3966038613070422E12 

I want to assign the string str as a expression value to checkbox.

public Component.Apex.InputCheckbox createchkbox()
{
Component.Apex.InputCheckbox chk=new Component.Apex.InputCheckbox();

Component.Apex.InputCheckbox test=new Component.Apex.InputCheckbox();
        
            String str=NULL;
      String type= 'FieldObject__c';  // Say,this is my object
      Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
      Schema.SObjectType leadSchema = schemaMap.get(type);
      Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
          
      


  
        for (String fieldName: fieldMap.keySet())
       
             { 
                  String mylabel;
                      //It provides to get the object fields label.
                  mylabel = fieldMap.get(fieldName).getDescribe().getLabel();
                  
                 Schema.DisplayType myType=fieldMap.get(fieldName).getDescribe().getType();
                  
                   
       
       
                             if (myType == Schema.DisplayType.BOOLEAN)
                                
                             {
                           
                                
                                             if(fieldName.endsWith('__c'))
                                             {
                                              
                                          str='\'{!'+type+'.'+fieldName+'}\'';
                                           
                                             }
                            }
  
  
                    }
   
       
       
        chk.expressions.value=str;
        return chk;
       
       
      

}

Above is my code in which I am trying to the string str as a expression value to checkbox. As I am trying to do this, I am getting the error 
"System.VisualforceException: Value assigned to expression field is not valid for <apex:inputCheckbox value>"

Kindly help me out.
Vinita_SFDCVinita_SFDC
Hi,

Only Boolean values(true/false) can be assigned to fields with ckeckbox type. You need to decide when to set true/false value or you can chane the field type to text.
wankarmadhavi11.3968559972086655E12wankarmadhavi11.3968559972086655E12
Thanks for your reply vinita.
I want to assign the string as an expression value to checkbox like
chk.expressions.value =str '{!FieldObject__c.Chkbox__c}'; where str contains the value of  '{!FieldObject__c.Chkbox__c}'.And for this, I am  trying to assign by writing below code:-chk.expressions.value=str; ,but its giving me an error.
how should I assign?
VijoshVijosh
Hi Vinita,

In the code we had assign some value to a string like   str='\'{!'+type+'.'+fieldName+'}\'';
Now in "str" we would have the following value: '{!FieldObject__c.Chkbox__c}'. As this is to dynamically set a field name that would be displayed in a VF page. So till here its fine, but when I assign it to 
Component.Apex.InputCheckbox  chk.expression.value= str;
This is where we experience an error.

Regards,
Vijosh P


Vinita_SFDCVinita_SFDC
Hi,

You are getting error because of field type mismatch. I can suggest you two things:

1) Convert str to boolean if it is true or false.
2) If str is not true or false the by using IF condition set the value of input checkbox:

if(str="abc")
{
Component.Apex.InputCheckbox  chk.expression.value= true;
}
else
Component.Apex.InputCheckbox  chk.expression.value= false;