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
Eric Anderson 54Eric Anderson 54 

load list from list

Hi there everyone,

I have a requirement to build a report that has sub totals and grand totals in it. Since I can't seem to figure out how to do this in the Salesforce reporting functionality, I'm doing it with a Visual force form. My thought was that I would process through a Object and group the entries by 'Activity'. When the activity changed, I would do a 'Sub total' for the activity. My thinking was that i would take the rows out of the 'SourceEntries' list, and add them to the 'TimeEntrires' list. When the Activity changes, then I would add an extra row to the list that serves as a 'Subtotal' like. Though I have some of the psuedo code in there and commented out, I'm finding that I can't even take information from one list to another.

Can anyone guide me on what I might be doing wrong. I am getting 'Attempt to de-reference a null object'.
The problem is with the statement 'TimeEntries.add(a);
The value of the debug statement is: DEBUG|Time_Entry__c:{Id=a04r0000002303wAAA, Activity__c=Research, Date_Worked__c=2017-04-14 00:00:00, Hours_Worked__c=1, Minutes_Worked__c=00, Work_Description__c=This is the first v1.2 time entry record for create time entry.}

The code involved is the following:
public class TERepController {

    //This is the list for handling the group of time entries related to the parent object. 
    public list<Time_Entry__c> SourceEntries {get;set;}
    //This is the list that will handle the detail with the totals added too it. 
    public List<Time_Entry__c> TimeEntries;

    //Used to get the parent request object ID from the Visual force page to the controller. 
    public string ReqID {get;set;}
    
    //Used to identify when there is an activity change for total generation. 
    public string strOldActivity = 'None';
    
    //Used to detect if a Row entered without selecting an action. 
    public TERepController(ApexPages.StandardController controller) 
    {
        //Obtain the parent object Id from the the visualforce form. 
        ReqId = Apexpages.currentPage().getparameters().get('Id');
        //Call the loaddata method to laod the time entries related to the parent object. 
        LoadData();
     }
    public void LoadData()
    {
        //Obtain the first 15 bytes of the Parent Object ID to use for record selection. 
        ReqId = 'a03r0000000rwC0';
        string RequestID = ReqId.substring(0, 15);
        //Load the related time entry records from the database. 
        SourceEntries = [select id, Activity__c, Date_Worked__c, Hours_Worked__c, Minutes_Worked__c, Work_Description__c from Time_Entry__c WHERE Related_Object__c=:RequestID order by Activity__c, ID]; 
        for(Time_Entry__c a : SourceEntries)
        {
//            if(a.Activity__c == strOldActivity)
//            {
//            	TimeEntries.add(new SourceEntries(a));
//            }
//            	else if(strOldActivity == 'None')
//                    {
                        System.debug(a);
             			TimeEntries.add(a);
//                        strOldActivity = a.Activity__c;
//                    }
//                    	else
//                        {
//                            Time_Entry__c b = new Time_Entry__c(Activity__c=strOldActivity, Work_Description__c='Activity Total', Hours_Worked__c='8', Minutes_Worked__c='30');
//                            TimeEntries.add(new SourceEntries(b));
//             				TimeEntries.add(new SourceEntries(a));
//                        	strOldActivity = a.Activity__c;
//                        }
        } 
        insert TimeEntries;
        return;
    }

}
I'd welcome any assistance anyone can provide me.

Thanks!

Eric Anderson

 
Best Answer chosen by Eric Anderson 54
James LoghryJames Loghry

Hi Eric,  

You're not initializing the TimeEntries list.  You can do this by calling the following code prior to the loop:

timeEntries = new List<Time_Entry__c>();


That being said, have you looked at alternative solutions prior to jumping to Visualforce, such as using either Roll-up summary fields OR a roll-up tool like the following? https://github.com/afawcett/declarative-lookup-rollup-summaries

 

All Answers

James LoghryJames Loghry

Hi Eric,  

You're not initializing the TimeEntries list.  You can do this by calling the following code prior to the loop:

timeEntries = new List<Time_Entry__c>();


That being said, have you looked at alternative solutions prior to jumping to Visualforce, such as using either Roll-up summary fields OR a roll-up tool like the following? https://github.com/afawcett/declarative-lookup-rollup-summaries

 

This was selected as the best answer
Eric Anderson 54Eric Anderson 54
Thank you for the help James... I will take a look at Roll-up Summaries and see what it can do for us. Thanks!