function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Subhasini Bhosal 6Subhasini Bhosal 6 

How to display two tables and compare

Any idea on how to display two tables on a vf page and compare each row of the two tables and display the output as matched or not matched on click of a button ?In the second table there should be a flag column that should be updated with true or false if any of the rows from two table match..
Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Subhasini,

I think we can easily achieve it by using Custom Controller along with Wrapper Class. I tried below example, it works well on page load (not on button click).

Here I am using two objects, Employee and Student. I am matching Student table Name with Employee Table and displaying TRUE id Name exists in Employee table.

I have two questions here:
  1. If you want to match these rows on a button click, then what is the use of this column on initial page load?
  2. Do you want to load a new VF page after click or you want this updated flag field on the same page?
Wrapper

Beow is the sample code:
VF Page
<apex:page controller="myTableController" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection >
            
                <apex:pageBlock title="Employee Table">
                    <apex:pageblockTable value="{!EmployeeName}" var="e">
                        <apex:column value="{!e.Employee_Name__c}"/>
                    </apex:pageblockTable>
                </apex:pageBlock>
            
                <apex:pageblock title="Student Table">
                
                  <apex:pageblockButtons >
                      <apex:commandButton value="Check for Match" action="{!ProcessRows}" reRender="Stutable" />
                  </apex:pageblockButtons>
                  
                    <apex:pageblockTable value="{!StudentName}" var="s" id="Stutable">
                        <apex:column headerValue="Student Name" value="{!s.stu.Name}" />
                        <apex:column headerValue="Match Flag" value="{!s.matchInd}"/>
                    </apex:pageblockTable>
                </apex:pageblock>
            
            </apex:pageblockSection>
        </apex:pageblock>
        
    </apex:form>
</apex:page>

Controller
public class myTableController {

        public List<Empolyee__c> EmpList = [Select Employee_Name__c from Empolyee__c limit 50];

        public List<Empolyee__c> getEmployeeName() {
            return EmpList;
        }

        public List<wStudent> StuList {get; set;}

        public List<wStudent> getStudentName() {
        if(StuList == null) {
            StuList = new List<wStudent>();
            for(Student__C s: [select Name from Student__C limit 50]) {
                
                StuList.add(new wStudent(s));
            }
        }
        return StuList;
    }


    public PageReference ProcessRows() {
        return null;
    }

    // This is wrapper/container class for Student. 

    public class wStudent {
        public Student__C stu {get; set;}
        public String matchInd {get; set;}
        
        set<string> myset = new set<string>();
        List<Empolyee__c> EmpList1 = [Select Employee_Name__c from Empolyee__c limit 50];
        
        //This is the contructor method. 
        public wStudent(Student__C s1) {
        
            For(Empolyee__c m : EmpList1){ 
                myset.add(m.Employee_Name__c); 
            }
        
            stu = s1;
            if(myset.contains(s1.Name) == true){
                matchInd = 'TRUE';
            }
            else {
                matchInd = 'FALSE';
            }      
        }
    }

}
Please let me know if this make sense or you still want to match rows on button click then we can try something else.

Thanks,
Rajneesh Ranjan
 
Subhasini Bhosal 6Subhasini Bhosal 6
Hi Rajneesh!!

Thanks for your help. This is very close to the requirement.
However I still have a doubt. Suppose Employee object has two instance: First Name and Last Name. Similarly Student object has two instances: First Name and Last Name.
Now if for a student record both first name and last name match with Employee record then it should display as True.
Suppose if Employee name is Subhasini Bhosal. Student names are Subhasini Bhosal, Subhasini Bhosal1, Subhasini Bhosal2, then Only for Student record Subhasini Bhosal it should show as true.

Hope you got my point.
Rajneesh Ranjan 23Rajneesh Ranjan 23
I would suggest.. you can concatenate two columns say First Name and Last Name for both objects and then compare it in the same way as I did with single column in the controller itself. I will give a try and will post the code later.. ;)