0
List<Result__c> rs=[SELECT FieldName__c,Id,Name,Type__c  FROM Result__c where Type__c='Checkbox'];
Opportunity CurrentOpportunity=[Select Id,Checkbox1__c,CheckBox2__c,Checkbox3__c from Opportunity where Id=:currentId];
boolean isUpdate=false;
Map<boolean,Boolean>mp=new Map<boolean,Boolean>();
for(Result__c vf:rs)
{
if(currentOpportunity.get(vf.FieldName__c)== true)
{
isUpdate=true;
mp.put(false,vf.FieldName__c);
}

}
if(isUpdate)
    {
     update CurrentOpportunity;   
     system.debug('Update Successfully');
    } 

In this code I quering on custom setting that gives me list of fields whose type is checkbox and If the checkbox value is then i have to set the value to false. I am trying to use Map<> where i am updating its value to false but failed to update it. Can somebody tell me what i am missing in syntax.

  • 1
    What is the error you are getting? – Samir Sep 27 '16 at 11:51
  • 1
    A Boolean key does not make any sense. Your Map can never reach a size greater than 3... – Adrian Larson Sep 27 '16 at 11:52
  • @Samir Incompatible value type String for Map&lt;Boolean,Boolean&gt; – Dhananjay Patil Sep 27 '16 at 11:54
  • @AdrianLarson if this is the case then how can i set the checkbox value to false.because i have to update all the checkbox value false at a time. – Dhananjay Patil Sep 27 '16 at 11:54
1

A code similar to below will work for your requirement:

List<Result__c> rs=[SELECT FieldName__c,Id,Name,Type__c  FROM Result__c where Type__c='Checkbox'];
Opportunity CurrentOpportunity=[Select Id,Checkbox1__c,CheckBox2__c,Checkbox3__c from Opportunity where Id=:currentId];
boolean isUpdate=false;

for(Result__c vf:rs)
{
if(currentOpportunity.get(vf.FieldName__c)== true)
{
isUpdate=true;
currentOpportunity.put(vf.FieldName__c,false);

}

}
if(isUpdate)
    {
     update CurrentOpportunity;   
     system.debug('Update Successfully');
    } 

Note that, using map the way you are using is not useful.

The addition is below line of code:

currentOpportunity.put(vf.FieldName__c,false);

put is sObject method that is used to set the value on a object. So this line of code will set current iteration field to false as per your requirement. Later update statetment will commit data into database.

| improve this answer | |
  • 2
    Please don't just post a code dump. Explain what you are doing and why. – Adrian Larson Sep 27 '16 at 11:56
  • @MukeshVerma It is giving me an error 'Incompatible value type String for Map&lt;Boolean,Boolean&gt; ' – Dhananjay Patil Sep 27 '16 at 11:59
  • I feel you don't need any map in code so you can remove all references of map. – Mukesh Verma Sep 27 '16 at 12:01
  • @AdrianLarson The requirement is we have some checkboxes present on VF page and once i set the checkbox value to true then it is replced by 'green' icon.Now the problem is if someone accidently check the checkbox and what if he wants to revert back? Currently in our application there is no way where we can uncheck the checkbox value.So i have implemented Onclick JS button where if u click on it then it should uncheck all the checkbox values.So the actual logic i am written in my apex class and in Onclick JS i am just calling the class and its method – Dhananjay Patil Sep 27 '16 at 12:03
  • @MukeshVerma 'FieldName__c' gives me the exact Checkbox field name.How can i uncheck all the checkbox field without using Map – Dhananjay Patil Sep 27 '16 at 12:06
1

Consider simply using a Set<String> to store the field names. You probably should also describe them and determine if they are updatable...

Map<String, SObjectField> schemaFields = SObjectType.Opportunity.fields.getMap();

Set<String> fields = new Set<String>();
for (Result__c result : [SELECT FieldName__c FROM Result__c WHERE Type__c='Checkbox'])
{
    SObjectField field = schemaFields.get(result.FieldName__c);
    if (field != null && field.getDescribe().isUpdateable())
        fields.add(field);
}

Opportunity someRecord = [SELECT Id FROM Opportunity WHERE ...];
for (String field : fields) someRecord.put(field, false);
update someRecord;
| improve this answer | |
  • 1
    I was writing the same piece of code.This should work.There is no need of map. – Samir Sep 27 '16 at 12:10

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.