0

Attached visualforce code displays names of people who are under a specific user. The names of the users are displayed as first name and last name but we could like to display them as last name, first name.

I am attaching the code.

Table

<select id="filterColumnList">
    <option value="1">{!$Label.PD_DownlineReport_SortName}</option>

String currDistribId = ''; // FLP 360 users (Can be primary or associated fbo records) if(this.distributor.Id == null && Userinfo.getUserType().contains('Partner')){ // Get the distributor based on the currently logged in user User usr = [Select Contact.AccountId, Associated_Id__c, Associated_Location__c From User Where Id =:UserInfo.getUserId()]; currDistribId = (usr.Associated_Id__c != null ? usr.Associated_Id__c : usr.Contact.AccountId); selectedCountry = usr.Associated_Location__c != null && usr.Associated_Location__c != 'ALL' ? usr.Associated_Location__c : null; this.distributor = [Select Id, Name, Level__c, Location_Country__c,Location_Operating_Company__c, ParentId From Account Where Id = :currDistribId Limit 1]; // Set selected country to Fbo's operating company, (only when its set to null. When switching to associated Ids, the value should not be overwritten) selectedCountry = (selectedCountry == null ? distributor.Location_Operating_Company__c : selectedCountry); findoutAssociatedFbos(); // Can't pass in the method: Also used in visual force page requiring an w/o parameter method signature }else if(this.distributor.Id == null){ currDistribId = ApexPages.currentPage().getParameters().get('id'); selectedCountry = ApexPages.currentPage().getParameters().get('op'); this.distributor = [select Id, Name, Level__c, Location_Operating_Company__c, Location_Country__c, ParentId from Account where Id = :currDistribId]; selectedCountry = (selectedCountry == null ? distributor.Location_Operating_Company__c : selectedCountry); findoutAssociatedFbos(); // Can't pass in the method: Also used in visual force page requiring an w/o parameter method signature } if(this.distributor != null && this.distributor.Id != null){ // Get list of Countries (Pass the priamry FBO detail only - to get ALL the countries) countriesList = DownlineUtil.getCountryList((this.distributor.ParentId != null ? this.distributor.ParentId : this.distributor.Id)); } }

// Check if any associated Id exists for the selected op country
public pageReference findoutAssociatedFbos() {
    // Set the dropdown value to null first and recalculate
    availableAssociatedFBOs = null;        
    // Find the Parent record for the FBO:
    Set<Id> relatedRecordIds = new Set<Id>();
    for(Account fbo: [Select Id,ParentId From Account Where Id =: this.distributor.Id]) {
        relatedRecordIds.add(fbo.Id);
        if(fbo.ParentId != null) {
            relatedRecordIds.add(fbo.ParentId);
        }
    }

    if(relatedRecordIds.size() > 0 ) {
        // Get the Associated FBOs (if any existing) from the primary record belonging to given opearating company
        // Query the Sponsors instead of Accounts to get the correct scope of master. For associate Ids in SGP & BEN, 
        // depending on the selected operating company, the primary record's correct scope will show up.
        List<Sponsor__c> lstSponsors = [Select Id,Scope__c,Location_Operating_Company__c ,Distributor__c, Distributor__r.Name, Distributor__r.ParentId
                                        From Sponsor__c Where Location_Operating_Company__c = :selectedCountry 
                                        AND (Distributor__c IN :relatedRecordIds OR Distributor__r.ParentId IN :relatedRecordIds)];

        // If any Associated Ids found, build the dropdown (show country, not operating country; For CIV,BEN,TGO there could be 1 Primary & 2 associated Ids)
        // Primary record will always be found, so lstSponsors.size() should be more than 1
        availableAssociatedFBOs = new List<SelectOption>();
        if(lstSponsors != null && lstSponsors.size() > 1) {
            selectedFboId = this.distributor.Id;  // Set the Parent fbo as pre-selected value
            for(Sponsor__c spon: lstSponsors) {
                // Replace Pipes (if any) with commas
                String value = '';
                if(spon.Distributor__r.ParentId != null){
                    value += Label.PD_Associated + ' - ' + spon.Location_Operating_Company__c;
                }else{
                    // Include the text 'Primary' for primary records only: 
                    value += Label.PD_Primary + ' - ' + spon.Location_Operating_Company__c;
                }
                // Build dropdown list : Id vs Name + Sponsored Country
                if(value != ''){
                    availableAssociatedFBOs.add(new SelectOption(spon.Distributor__c, value));
                }
            }
        }
        // If only one record found, set back to primary record (When an associated Id is selected and changed to some country which isn't merged)
        else if(lstSponsors != null && lstSponsors.size() == 1) {
            // Refetch the primary distributor details from the sponsor
            this.distributor = [Select Id, Name, Level__c, Location_Operating_Company__c, Distributor_ID__c, ParentId, Parent.Distributor_ID__c, Description From Account Where Id=:lstSponsors[0].Distributor__c];
        }
    }
    return null;
}

// Update User record (Move to a Without Sharing class)
public pageReference changeFbo(){
    // Take the selectedFboId and stamp it on User record (to automatically select on other dashboard elements/ next login)
    if(selectedFboId != null && selectedFboId != '' && selectedCountry != null && selectedCountry != ''){
        User usr = new User(Id = UserInfo.getUserId());
        usr.Associated_Id__c = selectedFboId;
        usr.Associated_Location__c = selectedCountry;
        Update usr;
        // Refresh the FBO details
        initFbo();
    }
    return null;
}

public static List<DownlineRecordWrapper> getDownlineData(Account acc, String country, Integer level, Set<String> includeLevels, String firstDistrib, String currUplineList, Integer offset){               
    Map<String, DownlineRecordWrapper> dataSet = new Map<String, DownlineRecordWrapper>();
    Map<String, String> parentToAssociatedMap = new Map<String, String>();
    operatingLocations = getLocations(country);
    List<Sponsor__c> dl = new List<Sponsor__c>();

    Boolean hasMore = false;
    String newCurrUplineList = '';

    // Root Node or Logged in FBO //
    // For level 0 - request will always come from Parent FBO and never from the Associated FBO // - Not Necessarily: Associate Id can be toggled...So request can come from the Associated FBO (handled in next if condition)
    if (level == 0){
  currUplineList = '';

        for(Sponsor__c spon :[SELECT Distributor__c, Distributor__r.Primary_Last_Name__c, Distributor__r.Primary_First_Name__c, Sponsor_Date__c, Distributor__r.ParentId,
              Distributor__r.Parent.Distributor_Id__c, Distributor__r.Distributor_ID_Format__c, Level__c, Distributor__r.Phone_1__c, Distributor__r.Email__c, Location_Operating_Company__c, 
              Location_Country__c , Distributor_ID__c,Sponsor_Distributor__r.Name,Distributor__r.Name FROM Sponsor__c  WHERE Distributor__c = :acc.Id and Location__c IN :operatingLocations 
              ORDER BY Distributor__r.Primary_Last_Name__c]){

          dl.add(spon);

          if(!newCurrUplineList.contains(spon.Distributor__c)){
            newCurrUplineList += spon.Distributor__c;
            if(spon.Distributor__r.ParentId != null && !newCurrUplineList.contains(spon.Distributor__r.ParentId)){
              newCurrUplineList += ',' + spon.Distributor__r.ParentId;
            }
            newCurrUplineList += ';';  
          }
        }

        // If nothing found on Current ID check FBOs on Parent ID
        if (dl.size() == 0){
            Set<String> currFBOAndParentFBOId = new Set<String>();
            currFBOAndParentFBOId.add(acc.Id);
            if(acc.ParentId != null && acc.ParentId != acc.Id){
                currFBOAndParentFBOId.add(acc.ParentId);
            }

            for(Sponsor__c spon :[SELECT Distributor__c, Distributor__r.Primary_Last_Name__c, Distributor__r.Primary_First_Name__c, Sponsor_Date__c, Distributor__r.ParentId,
              Distributor__r.Parent.Distributor_Id__c, Distributor__r.Distributor_ID_Format__c, Level__c, Distributor__r.Phone_1__c, Distributor__r.Email__c, Location_Operating_Company__c, 
              Location_Country__c , Distributor_ID__c,Sponsor_Distributor__r.Name,Distributor__r.Name FROM Sponsor__c  WHERE Distributor__c IN :currFBOAndParentFBOId and Location__c IN :operatingLocations 
              ORDER BY Distributor__r.Primary_Last_Name__c]){

                dl.add(spon);

                if(!newCurrUplineList.contains(spon.Distributor__c)){
                    newCurrUplineList += spon.Distributor__c;
                    if(spon.Distributor__r.ParentId != null && !newCurrUplineList.contains(spon.Distributor__r.ParentId)){
                        newCurrUplineList += ',' + spon.Distributor__r.ParentId;
                    }
                    newCurrUplineList += ';';   
                }
            }
        }
        /*/ Special case when distributor does not have a Sponsor record in the specified country, but does have downline there //
        if (dl.size() == 0){
            Account d = [SELECT Id, Primary_Last_Name__c, Primary_First_Name__c, Distributor_ID_Format__c, Level__c, Phone_1__c, Email__c, 
                         Location_Operating_Company__c ,Location_Country__c, Distributor_ID__c,ParentId,Parent.Distributor_Id__c FROM Account WHERE Id = :acc.Id];
            Sponsor__c sTemp = new Sponsor__c(Distributor__r = d, Distributor__c = d.Id, Distributor_ID__c = d.Distributor_ID__c, Level__c = d.Level__c);
            dl.add(sTemp);

          if(!newCurrUplineList.contains(d.Id)){
            newCurrUplineList += d.Id;
            if(d.ParentId != null && !newCurrUplineList.contains(d.ParentId)){
              newCurrUplineList += ',' + d.ParentId;
            }
            newCurrUplineList += ';';  
          }
        }*/
    }else{          

        // Store FBO ID (indexed field) into a set which is later used to perform the query //
        Set<String> currFBOAndParentFBOId = new Set<String>();
        currFBOAndParentFBOId.add(acc.Distributor_ID__c);
        if(acc.ParentId != null && acc.ParentId != acc.Id && acc.Parent.Distributor_Id__c != null && acc.Parent.Distributor_Id__c != ''){
            currFBOAndParentFBOId.add(acc.Parent.Distributor_Id__c);
        }

        dl = [SELECT Distributor__c, Distributor__r.Primary_Last_Name__c, Distributor__r.Primary_First_Name__c, Sponsor_Date__c, Distributor__r.ParentId, 
              Distributor__r.Parent.Distributor_Id__c, Distributor__r.Distributor_ID_Format__c, Level__c, Distributor__r.Phone_1__c,Sponsor_Distributor__r.Name,Distributor__r.Name,
              Distributor__r.Email__c, Location_Operating_Company__c, Location_Country__c , Distributor_ID__c FROM Sponsor__c  WHERE 
              Distributor__r.Distributor_Status__c IN ('Active','Suspended','Restricted','Pending') AND (Sponsor_Distributor_ID__c IN :currFBOAndParentFBOId 
              OR Sponsor_Distributor__r.Parent.Distributor_Id__c IN :currFBOAndParentFBOId) AND Location__c IN :operatingLocations AND 
                Level__c IN :includeLevels ORDER BY Distributor__r.Primary_Last_Name__c];


        /*for(Integer i = offset; i < limitTo; i++){
            dl.add(allSponsors[i]);
        }

        if(limitTo < allSponsors.size()){
            hasMore = true;
        }*/
    }

    // The following logic is used to send out the chunks of data to the VF page instead of all at once to avoid SF limits //
    // Downline Limit //
    Integer downlineLimit = Key_Value_List__c.getValues('PD_MLDR_RecordLimit') != null ? Integer.valueOf(Key_Value_List__c.getValues('PD_MLDR_RecordLimit').Value__c) : 900;
    Integer limitTo = ((dl.Size() - offset) <  downlineLimit ? dl.Size() : downlineLimit) + offset;
    // ------------------------------------------------------------------------------------------------------------------- //

    Set<Id> distList = new Set<Id>();
    Set<String> distIDList = new Set<String>();
    Integer counter = 0;
    for (Sponsor__c s : dl){

      newCurrUplineList = currUplineList;

      Integer flag = 0;
      if(!currUplineList.contains(s.Distributor__c) && !currUplineList.contains(s.Distributor__r.ParentId != null ? s.Distributor__r.ParentId : s.Distributor__c)){
        // Check if the current FBO is in upline and if current FBO's parent is in upline
    // If the current FBO's parent is null do nothing (or check the first condition again)            
        flag = 1;

        if(!newCurrUplineList.contains(s.Distributor__c)){
            newCurrUplineList += ',' + s.Distributor__c;
            if(s.Distributor__r.ParentId != null && !newCurrUplineList.contains(s.Distributor__r.ParentId)){
              newCurrUplineList += ',' + s.Distributor__r.ParentId;
            }
            newCurrUplineList += ';';
          }            
      }else{
        flag = 0;
      }

        if(flag == 1){                 
          counter ++;
          if((counter > offset || level == 0) && counter <= limitTo){
              dataSet.put(s.Distributor__c + '_' + country, new DownlineRecordWrapper(s, level, newCurrUplineList));
              distList.add(s.Distributor__c);
              distIDList.add(s.Distributor_ID__c);

                if(s.Distributor__r.ParentId != NULL && s.Distributor__r.ParentId != s.Distributor__c 
                  && s.Distributor__r.Parent.Distributor_Id__c != NULL && s.Distributor__r.Parent.Distributor_Id__c != ''){
                  distIDList.add(s.Distributor__r.Parent.Distributor_Id__c);

                  // Set map to perform has downline logic - supporting parent records //
                  parentToAssociatedMap.put(s.Distributor__r.ParentId, s.Distributor__c);
              }
          }else if(counter > limitTo){
            hasMore = true;
          }              
        }
    }       


     Map<Id, Account> ccList = new Map<Id, Account>([Select id, 

                                    (SELECT Country__c, Current_Month_Active__c, Distributor__c, 
                                    Personal_CC_MTD__c, Total_CC_MTD__c, Processing_Date__c, Total_Active_CC_MTD__c, Non_Manager_CC_MTD__c, 
                                    Leadership_CC_MTD__c, Processing_Month__c, Processing_Year__c, Global_Case_Credits__c, 
                                    Eagle_Manager_Global_New_CC__c, Chairmans_Bonus_Global_New_CC__c FROM Account.Monthly_CCs__r 
                                    WHERE Processing_Date__c >= Last_N_Months:2 AND Op_Company__c = :country),

                                    (SELECT Total_CC_YTD__c, Total_Active_CC_YTD__c, Non_Manager_CC_YTD__c, Leadership_Qualification_CC_YTD__c,
                                    Operating_Company__c FROM Account.Yearly_CCs__r WHERE Processing_Date__c = THIS_YEAR) 

                                    from Account where ID IN :distList]);





    // Handle FL style Distributor IDs
    List<Account> distributorList = [Select Id, Name, Level__c, Location_Operating_Company__c, Location_Country__c, Distributor_ID__c, Distributor_Id_Format__c, ParentId, 
                                     Parent.Distributor_Id__c From Account Where Id = :accId OR Distributor_ID__c = :accId LIMIT 1];
    List<Account> firstDistributorList = [Select Id, Name, Level__c, Location_Operating_Company__c, Location_Country__c, Distributor_ID__c, Distributor_Id_Format__c, ParentId, 
                                     Parent.Distributor_Id__c From Account Where Id = :firstDistrib OR Distributor_ID__c = :firstDistrib LIMIT 1];

This is the custom label used:

Custom Label Detail

  • 1
    can you provide source code of VF page and controller – Oleksandr Berehovskyi Jun 7 '17 at 9:53
  • 2
    Please do not solicit users to send the conversation off of SFSE, via email or other means. Instead, edit your post to include the code. – Adrian Larson Jun 7 '17 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.