• Swarvi Kumari
  • NEWBIE
  • 25 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
in one drop down, i want to display all object, once i will select the object from dropdown, in second dropdown it will show all the fields for selected objects.
Hi All,

Can anyone Please help to write a test class for my class?

Apex Class:

public without sharing class _CalendarCtrl extends _Ctrl {
    public List<SelectOption> categoryOptions {get; set;}
    public List<SelectOption> regionOptions {get; set;}

    public String selectedCategory {get; set;}
    public String selectedRegion {get; set;}

    public String selectedFromDate {get; set;}
    public String selectedToDate {get; set;}

    public String selectedPage {get; set;}
    public Integer selectedPageNumber {get; set;}

    public List<Calendar_Event__c> ccalendarEvents {get; set;}

    public Map<Id, List<String>> eventIdToCategories {get; set;}

    public Integer numberOfResultsPerPage {get; set;}
    public Integer numberOfPages {get; set;}

    public Map<String, Id> categoryToAttachmentIdMap {get; set;}

    public _CalendarCtrl() {
        populateCategoryOptions();
        populateRegionOptions();

        selectedCategory = ApexPages.currentPage().getParameters().get('category');
        selectedRegion = ApexPages.currentPage().getParameters().get('region');

        selectedFromDate = ApexPages.currentPage().getParameters().get('from-date');
        if(selectedFromDate == null) {
            selectedFromDate = Date.today().format();        
        }
        
        selectedToDate = ApexPages.currentPage().getParameters().get('to-date');

        selectedPage = ApexPages.currentPage().getParameters().get('page');
        
        selectedPageNumber = 1;
        if(String.isNotEmpty(selectedPage)) {
            selectedPageNumber = Integer.valueOf(selectedPage);
        }

        numberOfResultsPerPage = Integer.valueOf(Version.Calendar_Num_Results_Per_Page__c);

        populateCalendarEvents();
        populateEventIdToCategoriesMap();
        
        populateCategoryToAttachmentIdMap();
    }

    public void populateCalendarEvents() {
        List<String> whereClauseConditions = new List<String>();

        if(String.isNotEmpty(selectedRegion) && selectedRegion != 'all') {
            whereClauseConditions.add('Region__c = :selectedRegion');
        }

        if(String.isNotEmpty(selectedCategory) && selectedCategory != 'all') {
            whereClauseConditions.add('Categories__c includes (:selectedCategory)');
        }

        Date fromDate = null;
        if(String.isNotEmpty(selectedFromDate)) {
            fromDate = Date.parse(selectedFromDate);
            whereClauseConditions.add('Event_Date__c >= :fromDate');
        }

        Date toDate = null;
        if(String.isNotEmpty(selectedToDate)) {
            toDate = Date.parse(selectedToDate).addDays(1);
            whereClauseConditions.add('Event_Date__c < :toDate');
        }

        String query = 'SELECT Name, Event_Date__c, Event_End_Date__c,Event_Start_Time__c,Event_End_Time__c,Summary__c, Region__c, Categories__c FROM Calendar_Event__c';
        if(!whereClauseConditions.isEmpty()) {
            query += ' WHERE ' + String.join(whereClauseConditions, ' AND ');
        }
        
        String countQuery = 'SELECT Count() FROM Calendar_Event__c';
        if(!whereClauseConditions.isEmpty()) {
            countQuery += ' WHERE ' + String.join(whereClauseConditions, ' AND ');
        }

        query += ' ORDER BY Event_Date__c ASC LIMIT :numberOfResultsPerPage';

        Integer numResults = Database.countQuery(countQuery);
        numberOfPages = Integer.valueOf(Math.ceil(numResults / (double)numberOfResultsPerPage));

        if(String.isNotEmpty(selectedPage) && selectedPage != '1') {
            Integer offset = (Integer.valueOf(selectedPage) - 1) * numberOfResultsPerPage;
            query += ' OFFSET ' + offset;
        }

        ccalendarEvents = Database.query(query);
        

        //return [SELECT Name, Event_Date__c, Summary__c, Region__c, Categories__c FROM Calendar_Event__c WHERE Region__c = :selectedRegion AND Categories__c includes (:selectedCategory)];
    }

    public List<Integer> getPageNumberList() {
        List<Integer> pageNumberList = new List<Integer>();
        for(Integer i=1; i<=numberOfPages; i++) {
            pageNumberList.add(i);
        }

        return pageNumberList;
    }

    private void populateCategoryOptions() {
        categoryOptions = new List<SelectOption>();
        for(Schema.PicklistEntry f : Calendar_Event__c.Categories__c.getDescribe().getPicklistValues()) {
            categoryOptions.add(new SelectOption(f.getLabel(), f.getValue()));
        }
    }

    private void populateRegionOptions() {
        regionOptions = new List<SelectOption>();
        for(Schema.PicklistEntry f : Calendar_Event__c.Region__c.getDescribe().getPicklistValues()) {
            regionOptions.add(new SelectOption(f.getLabel(), f.getValue()));
        }
    }

    private void populateEventIdToCategoriesMap() {
        eventIdToCategories = new Map<Id, List<String>>();

        for(Calendar_Event__c cEvent : ccalendarEvents) {
            eventIdToCategories.put(cEvent.Id, String.valueOf(cEvent.Categories__c).split(';'));
        }
    }
    
    private void populateCategoryToAttachmentIdMap() {
        categoryToAttachmentIdMap = new Map<String, Id>();
        
        for(__Calendar_Category__c cc : Version.__Calendar_Categories__r) {
            if(cc.Category_Icon_Ref__c != null) {
                categoryToAttachmentIdMap.put(cc.name, cc.Category_Icon_Ref__r.Attachment_Id__c);
            }
        }
    }
}

my Test Class

@isTest

public without sharing class SiteCalendarCtrl_Test{
static testMethod void Testdata() {
Test.startTest();
CalendarCtrl Calendar= new CalendarCtrl();
Calendar.populateCalendarEvents();
Calendar.getPageNumberList();
Test.stopTest();
}
}
Error:

CalendarCtrl_Test.Testdata: line 5, column 1

Appreciate for your quick help!
in one drop down, i want to display all object, once i will select the object from dropdown, in second dropdown it will show all the fields for selected objects.
Visualforce Error
Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 158.109KB 

Hi all,
I am getting this error when i try to import large file using visual force page, can anyone please tell me how to show this error like "Please selct the file less than 100kb".
Hi all,
In opportunity object there is stage field...
in that depend on stage value no.of opportunitys available in day to day..
for this we have to show in report.
 please help me..

regards,
NAGARJUNA REDDY NANDIREDDY