+ Start a Discussion
KNKKNK 

Modifications to Query based on certainfield in two obejcts

Hi Team,

 

I am new to Salesforce. I have a field Commercial Unit in Territory and in Campaign.

 

I want to display in territories listed in “Territories” page should be belonging the Commercial Units in that campaign

 

public with sharing class AddCampaignTerritories {

    public List<TerritoryWrapper> territories{get; set;}
    //public List<String> fieldsToDisplay{get; set;}
    Campaign c;
    
    //variables for page size and offset
    public Integer pageSize{get; set;}
    public Integer offset{get; set;}
    
    //variables for checking if there is next page/previous page or not
    public Boolean hasPrev{get; set;}
    public Boolean hasNext{get; set;}

    public AddCampaignTerritories(ApexPages.StandardController controller) {
        //Get the campaign record passed
        c = (Campaign) controller.getRecord();
        //initialize territories list
        territories = new List<TerritoryWrapper>();
        
        //initialize pagesize and offset
        pageSize = 25;
        offset = 0;
        
        //default hasPrev and hasNext to false
        hasPrev = false;
        hasNext = false;
        
        //If campaign id is not null, get the list of territories that can be added to the campaign
        if (c.Id != null) {
            refreshList();
        }
        else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, 'Please open this page from Campaign detail page'));
        }
        
    }
    
    public void refreshList() {
        try {
            territories.clear();
            List<Territory__c> terrList = new List<Territory__c>();
            
            //Get the ids of territories that are already added to the campaign to exclude them from the list displayed
            List<String> existingIds = new List<String>();
            try {
                List<Campaign_Territory__c> ctExisting = [SELECT Territory__c FROM Campaign_Territory__c WHERE Campaign__c=:c.Id];
                for (Campaign_Territory__c ct : ctExisting) {
                    existingIds.add(ct.Territory__c);
                }
            }
            catch(System.QueryException e){ApexPages.addMessages(e);}
            
            //Get the list of available territories that can be added
            terrList = [SELECT Name, CU_Manager__c, Area_Sales_Manager__c, Territory_Sales_Manager__c FROM Territory__c WHERE Id NOT IN :existingIds LIMIT :(pageSize+1) OFFSET :offset];
            
            if (terrList.size() > pageSize) { hasNext = true; terrList.remove(pageSize); }
            else hasNext = false;
            if (offset == 0) hasPrev = false;
            else hasPrev = true;
            
            for (Territory__c t : terrList){
                territories.add(new TerritoryWrapper(t));
            }
        }
        catch(System.QueryException e) {
            ApexPages.addMessages(e);
        }
    }
    
    public void previous() {
        if (hasPrev) { offset -= pageSize; refreshList(); }
    }
    
    public void next() {
        if (hasNext) { offset += pageSize; refreshList(); }
    }
    
    public PageReference save() {
        List<Campaign_Territory__c> ctList = new List<Campaign_Territory__c>();
        
        for (TerritoryWrapper tw : territories) {
            if (tw.isSelected) {
                ctList.add(new Campaign_Territory__c(Campaign__c = c.Id, Territory__c = tw.t.Id));
            }
        }
        try {
            if (!ctList.isEmpty()) {
                insert ctList;
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.INFO, 'Territories have been successfully added'));
            return new ApexPages.StandardController(c).view();
        }
        catch(System.DMLException e) {
            ApexPages.addMessages(e);
        }    
        return null;
    }

    class TerritoryWrapper {
        public Territory__c t {get; set;}
        public Boolean isSelected{get; set;}
        
        public TerritoryWrapper(Territory__c t) {
            this.t = t;
            isSelected = false;
        }
    }
}

Vinit_KumarVinit_Kumar

Not sure what you are looking for,Can you be specific about your requirement.