Monday, July 3, 2017

How To Dispaly The DropDown Values With CheckBoxes Vertically In Visual Force

In my previous post I have explained to how to display the drop down values with check boxes so we will use the same post but In this post I will explain how to display that list vertically rather than horizontally.

In <apex:SelectCheckBoxes> tag we have an attribute called 'layout' which will decides the direction of the display .If you set that to layout="pageDirection" then it will displays automatically in vertical.

Source Code:


In VisualForce Page:

<apex:selectcheckBoxes value="{!selFruits}" layout="pageDirection">
 <apex:selectOption itemLabel="Mango" itemValue="Mango" />
 <apex:selectOption itemLabel="Apple" itemValue="Apple" />
 <apex:selectOption itemLabel="Banana" itemValue="Banana" />
 <apex:selectOption itemLabel="Guava" itemValue="Guava"/>
        <apex:selectOption itemLabel="Orange" itemValue="Orange"/>

</apex:selectcheckBoxes>

In Apex Class:

Public List<String> selFruits{get;set;} // This should be list not Single String


OutPut:




Thanks for visiting..hope this helps you!

How To Display Drop down Values With Check Box in Visual Force Page

If you want to display each drop down value with the checkbox you can use the below code snippet.In below code we are using Salesforce tag called <apex:selectCheckboxes> and inside that we are adding all the SelectOptions.

Source Code:

In VisualForce Page:

<apex:selectcheckBoxes value="{!selFruits}" >
 <apex:selectOption itemLabel="Mango" itemValue="Mango" />
 <apex:selectOption itemLabel="Apple" itemValue="Apple" />
 <apex:selectOption itemLabel="Banana" itemValue="Banana" />
 <apex:selectOption itemLabel="Guava" itemValue="Guava"/>
        <apex:selectOption itemLabel="Orange" itemValue="Orange"/>

</apex:selectcheckBoxes>

In Apex Class:

Public List<String> selFruits{get;set;} // This should be list not Single String


OutPut:



Thanks for visiting..hope this helps you!

Saturday, July 1, 2017

Field expression not allowed for generic SObject in Salesforce

Scenario:


Usually when we writing any trigger we will be following some best practices like One Trigger for One Object,Business logic in Handler Class ,Don't write the whole code in Trigger etc..This kind of error will receive when we are writing whole trigger logic in Separate Handler Class(Apex class) and we are referring to that trigger context variables  in that Apex class or  referring your sobjects in Trigger handler


Root Cause : 


Trigger Code

Trigger CaseTrigger on Case (Before Insert,Before Update,After insert) 
{
   if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
    {
        CaseTriggerHandler.duplicateCheck(Trigger.new);
    }
}

Handler Class

Public class CaseTriggerHandler
{ 
public static void duplicateCheck(List<Case> TriggerNew)
 {
         for (Case cs : TriggerNew) 
        {
           if(cs.status__c!=Trigger.OldMap.get(cs.Id).status__c)
            {
               System.debug('Status has changed');
            }
        }
 }
}

When your trying save above handler code you will end up with Field expression not allowed for generic SObject .


Solution:

The reason behind is that Trigger.OldMap is a generic variable that means it will be used for all objects so when your using this variable you need to tell the compiler what type of the SObject your referring from Trigger.OldMap. I have highlighted that code change in yellow color in below code.
        

Public class CaseTriggerHandler
{ 
public static void duplicateCheck(List<Case> TriggerNew)
 {
         for (Case cs : TriggerNew) 
        {
           if(cs.status__c!=((Map<Id,Case>)Trigger.OldMap).get(cs.Id).status__c)
            {
               System.debug('Status has changed');
            }
        }
 }
}


Thanks for visiting..hope this helps you!

System.ListException: Before Insert or Upsert list must not have two identically equal elements

We will receive this error when your adding same instance of record to list twice i.e.If your adding same record twice to the list you will end up with error.

Sample Code With Error

List<Case> listCase = new List<Case>();
Case cs = new Case();
cs.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Bus').getRecordTypeId();
cs.Status ='Approved';
cs.Origin = 'Inbound Call';
listCase.add(cs);

Case cs1 = new Case();
cs1.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Bus').getRecordTypeId();
cs1.Status ='Approved';
cs1.Origin = 'Inbound Call';
listCase.add(cs);  // I should add cs1 instance but I added cs which are already in list

insert listCase;

If you run the above code you will receive an error while inserting System.ListException: Before Insert or Upsert list must not have two identically equal elements ,that is because of adding same case record twice to the list ,you can see duplicate element in yellow color where actually it should be cs1 but added cs which is already there in list.

To avoid this error you can cross check your code whether your adding some where same instance of object twice to the list.

Error Free Code


List<Case> listCase = new List<Case>();
Case cs = new Case();
cs.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Bus').getRecordTypeId();
cs.Status ='Approved';
cs.Origin = 'Inbound Call';
listCase.add(cs);

Case cs1 = new Case();
cs1.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Bus').getRecordTypeId();
cs1.Status ='Approved';
cs1.Origin = 'Inbound Call';
listCase.add(cs1);   //Corrected here adding cs1 instance to list 

insert listCase;


Thanks for visiting..hope this helps you!

How to Check Whether All Check Boxes Selected Or Not in Visual Force Page Using Jquery

In my previous post we have discussed about how to check whether at least one check box is checked or not in visual force it's very easy using Jquery .In this post we are going to know about how to check whether all check boxes has selected or not? using Jquery .Please use the below code snippet to check this functionality.

Sample Source Code:


function selectionCheck(selClass)
   {
          
     var chbxClass = '.'+selClass; // selClass is styleclass name for all your checkbox
  
 if ($(chbxClass+':checked').length == $(chbxClass).length) 
        {
        
          alert('You have selected all records for Action'));           
            
        }
  
 else
 {
  alert('You haven't selected all records'); 
 }
     
  }



Thanks for visiting..hope this helps you!