Wrapper Class & Junction Object

If you work upon salesforce then you are most familiar with these two things: Wrapper Class & Junction Object.

First of all, starting with the Junction object,

Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object “Job Application” that links the 2.

So you’d create a lookup field for both Position and Candidate object on the “Job Application” object. This will establish many to many relationships between Position and Candidate via the “Job Application” object known as the junction object.

The second thing here is Wrapper class,

A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.

 

Example for this:

Objects: Student, Course, Registration, Fee their fields are showing in the images below;

 

Student:

Student- Coderinme

Course :

Course- coderinme

Registration:

registration- coderinme

 

Fee:

fee- Coderinme

Code to find out the Fees of students for different courses who register for it.

public class RegisteredStudentList {
  // wrapper list
   public list<stdWrapper> stdListWrapper{get;set;}
    
    //constructor
    public RegisteredStudentList(){
    
    // query for student ( inner query for registration(course)  and Fee
        list<student__c> stdList=[select id,name,City__c,Date_Of_Birth__c,(select id,course__r.Fees__c,course__r.Name from Registrations__r),(select id,Amount__c from Fees__r) from student__c where Total_Course__c>0];
        
        // wrapper all value
        stdListWrapper= new List<stdWrapper>();
        double pd=0, payb=0, rem=0;
        
        // courses string
        string courseDet='[ ';
        for(student__c std:stdList){
            stdWrapper stw= new stdWrapper();
            for(Registration__c reg: std.Registrations__r){
                //payable amount
                payb +=reg.course__r.Fees__c;
                // courses string
               courseDet+=reg.course__r.Name +'--> Rs.'+reg.course__r.Fees__c+', ';
                }
                courseDet= courseDet.removeEnd(', ');
                courseDet+=' ]';
            
             //paid fees   
            for(Fees__c fe: std.Fees__r)
                pd += fe.Amount__c;
            
            // remaining
            rem=payb-pd;
            stw.stud=std;
            stw.payable=payb;
            stw.paid=pd;
            stw.remain=rem;
            stw.crsDet=courseDet;
            // adding this wrapper into list
            stdListWrapper.add(stw);
        }
    }
    // wrapper class
    public class stdWrapper{
        public student__c stud{get;set;}
        public string crsDet{get;set;}
        public double payable{get;set;}
        public double paid{get;set;}
        public double remain{get;set;}
    }
    // code s'f 
}

 

Explanation :

In this code, two nested queries for registration and fee is applied with the query of Student in which there is a “where” condition for the total course is greater than 0.

Then a “for” loop for the student in which wrapper class reference is created, then again a for loop of registration to calculate the payable amount. In the next line, the calculation for the course is done, then in the ” // Remaining ” comment, we fetch out the data from wrapper all the object which needs to be visible in VF page.

In the last, the wrapper class is created for few field to be visible on the VF page like Paid, Payable and Remaining mainly.

There is also the alternate way to code this with the aggregate query:

 

public class RegStuList {
    
  // wrapper list
   public list<stdWrapper> stdListWrapper{get;set;}
    
    //constructor
    public RegStuList(){
    
    // query for student ( inner query for registration(course)  and Fee
        list<student__c> stdList=[select id,name,City__c,Date_Of_Birth__c,(select id,course__r.Fees__c,course__r.Name from Registrations__r) from student__c where Total_Course__c>0];
        set<id> stdSet= new set<id>();
        Map<id, double> stdPaidMap= new Map<id, double>();
        for(student__c std:stdList)
            stdSet.add(std.id);

         for(AggregateResult agR: [SELECT student__c, sum(Amount__c) totalfee FROM Fees__c where student__c IN:stdSet GROUP BY student__c])
                stdPaidMap.put(string.valueOf(agR.get('student__c')), Double.valueOf(agR.get('totalfee')));
          
        // wrapper all value
        stdListWrapper= new List<stdWrapper>();
        
        for(student__c std:stdList){
        double pd=0, payb=0, rem=0;
       
            stdWrapper stw= new stdWrapper();
             // courses string
        string courseDet='[ ';
        
            for(Registration__c reg: std.Registrations__r){
                //payable amount
                payb +=reg.course__r.Fees__c;
                // courses string
                courseDet+=reg.course__r.Name+' --> '+reg.course__r.Fees__c+', ';
                }
            courseDet= courseDet.removeEnd(', ');
            courseDet+=' ]';
                
            if(stdPaidMap.containsKey(std.id))
                pd=stdPaidMap.get(std.id);

            // remaining
            rem=payb-pd;
            
            stw.stud=std;
            stw.payable=payb;
            stw.paid=pd;
            stw.remain=rem;
            stw.crsdt=courseDet;
            
            // adding this wrapper into list
            stdListWrapper.add(stw);
                
        }  
    }
    // wrapper class
    public class stdWrapper{
        public student__c stud{get;set;}
       // public string crsDet{get;set;}
       public string crsdt {get;set;} 
        public double payable{get;set;}
        public double paid{get;set;}
        public double remain{get;set;}
       
    }
}

 

For both of the above controller, we have a Visualforce page which can apply to both:

<apex:page controller="RegisteredStudentList" sidebar="false">
   <apex:pageBlock title="Student Panel">
    <apex:pageBlockTable value="{!stdListWrapper}" var="stw">
        <apex:column headerValue="Id"><apex:outputField value="{!stw.stud.id}"/>  
        </apex:column>
        
        <apex:column headerValue="Name"><apex:outputField value="{!stw.stud.Name}"/>
        </apex:column>
        <apex:column headerValue="City"><apex:outputField value="{!stw.stud.City__c}"/>
        </apex:column>
        
        <apex:column headerValue="Date of Birth"><apex:outputField value="{!stw.stud.Date_Of_Birth__c}"/>
        </apex:column>
        <apex:column headerValue="Courses"><apex:outputText value="{!stw.crsDet}"/>
        </apex:column>
        <apex:column headerValue="Payable amount"><apex:outputText value="{!stw.payable}"/>
        </apex:column>
        <apex:column headerValue="Paid Amount"><apex:outputText value="{!stw.paid}"/>
        </apex:column>
        <apex:column headerValue="Remainimg Balance"><apex:outputText value="{!stw.remain}"/>
        </apex:column>
        
    </apex:pageBlockTable>
       </apex:pageBlock> 
</apex:page>

 

VF Page look like

VfPage-Junction object wrapper class - Coderinme

 

This question is asked by Varsha and Coded By Saif(@Hasectic) & Varsha, Thank you both.

 

The video tutorial is also available.

Note: All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

 

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.