Sunday, 13 August 2017

                   Remove duplicate items from a List:

Today I'am going to demonstrate how to remove duplicate values in List. Please add below code in your anonymous window and check how it works.

List<integer> orginalresult=new List<integer>();
orginalresult.add(1); // 0th position
orginalresult.add(2);//1st position
orginalresult.add(2);
orginalresult.add(10);
orginalresult.add(11);
orginalresult.add(3);
orginalresult.add(4);
orginalresult.add(4);
orginalresult.add(1);
orginalresult.add(5);
system.debug('@@@ originalList@@@'+orginalresult);
set<integer> setresult=new set<integer>();
List<integer> Listresult=new List<integer>();
setresult.addAll(orginalresult);
system.debug(setresult);
Listresult.addAll(setresult);
system.debug(Listresult);

Problem: when converted to a Set and then back to a List using addAll(), the order doesn't remain.
Solution:
//To maintain order, you need to iterate over the elements:
for(integer s:orginalresult){
    if(setresult.add(s)){
        Listresult.add(s);
    }
    }
system.debug('@@@ListResult @@@@'+Listresult);   

Wednesday, 2 August 2017

Using Apex:Variable in Visualforce to control rendering of HTML Markup


We mostly use apex:variable tag for declaring complex expressions or variables in visual force page. But the same can be used very easily to control the rendering too.



<apex:page controller="ApexVarController1">
<div>
        <!-- apex:variable tag to control the rendering for Markup --> 
<apex:variable
value="anything will go here" var="flag"
rendered="{!flag}">
<h1> Flag value is true </h1><br />
</apex:variable> 
<!-- apex:variable tag used to not render for a Markup --> 
<apex:variable value="anything will go here" var="flag1" rendered="{!flag1}">
<h1>Flag value is false</h1><br />
</apex:variable>
</div>
</apex:page>
--------------------------------------------------------------------------------------------------
public class ApexVarController1 {
    public boolean flag { get;set;}
    public boolean flag1{get;set;}
    
    public ApexVarController1 (){
       flag=true;
       flag1=false;
    }

}
--------------------------------------------------------------------------------------------------------