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
NishaCNishaC 

Problem in getting parent child records with its associated field

can anyone tell me how to get this hierarchy

Parent Id
            ChildID    CountOfPickupDays(formula field)...decimal
            CHildID    CountOfPickupDays
            ChildID    CountOfPickupDays
                      

Example: By name rather than ID

Caffe Nero Group Ltd                  Count Of PickUp days Field(Formmula Field)
                          Caffe Nero   =           2
                          CaffeNero    =          1
                          Starbucks    =           0
                          Caffe Nero Group Ltd = 5
                           Caffe Nero          =    1

i want to get parent Id----->Child IDs-----> All Pickup Days

i have used map for getting parent ID assoiated to its child
Map<Id,List<Id>> servMap = new Map<Id,List<Id>>();

for(Service__c serObj: serviceList){
Id parentAccId = serObj.Customer__r.Grand_Parent_Account__c;
Id AccountId = serObj.Customer__r.id;

if(serObj.Status__c == 'Active'){
if(servMap.get(parentAccId)==null)
{
servMap.put(parentAccId,new list<Id>{AccountId});
}
else
{
servMap.get(parentAccId).add(AccountId);
}
}
got all the child IDs according to parent  

now i want to get count of pickup days according to its child ids

 

Give me some idea

Best Answer chosen by Admin (Salesforce Developers) 
Anoop AsokAnoop Asok

Hi,

From what I understand, you're trying to get the count for a particular child record while you're having the parentid in hand.
If my understanding is right,  you can try the below code.

 

Map<Id,List<List<String>>> servMap = new Map<Id,List<List<String>>>(); //Updated

for(Service__c serObj: serviceList){
Id parentAccId = serObj.Customer__r.Grand_Parent_Account__c;
String AccountId = serObj.Customer__r.id;
String count = serObj.Customer__r.CountOfPickupDays; //Added
List<String> childDetailsList = new List<String>{AccountId, count}; //Added
if(serObj.Status__c == 'Active'){
if(servMap.get(parentAccId)==null)
{
servMap.put(parentAccId,new list<List<String>>{childDetailsList}); //updated
}
else
{
servMap.get(parentAccId).add(childDetailsList); //updated
}
}

 



With this, you can get the count as follows if you've the parentAccId.

servMap.get(parentAccId).get(0) -> first child record detail for your parentAccId
childDetails.get(0) -> child record id
childDetails.get(1) -> count
 

Note: Please validate the code against your api names, as I might be using some random names for unknown attributes.

 

Thanks,
Anoop

All Answers

Anoop AsokAnoop Asok

What about updating your map to Map<Id, List<List<String>>>?

A sample entry in this collection will look something like this: ParentId (Key) -> ((ChildId, count for child), (ChildId, count for child), (ChildId, count for child)... )

 

Thanks,

Anoop

 

NishaCNishaC

actually m not familiar with map<id,list<list<>>>

can u give me some example

and is it proviiding the desired result?

Anoop AsokAnoop Asok

Hi,

From what I understand, you're trying to get the count for a particular child record while you're having the parentid in hand.
If my understanding is right,  you can try the below code.

 

Map<Id,List<List<String>>> servMap = new Map<Id,List<List<String>>>(); //Updated

for(Service__c serObj: serviceList){
Id parentAccId = serObj.Customer__r.Grand_Parent_Account__c;
String AccountId = serObj.Customer__r.id;
String count = serObj.Customer__r.CountOfPickupDays; //Added
List<String> childDetailsList = new List<String>{AccountId, count}; //Added
if(serObj.Status__c == 'Active'){
if(servMap.get(parentAccId)==null)
{
servMap.put(parentAccId,new list<List<String>>{childDetailsList}); //updated
}
else
{
servMap.get(parentAccId).add(childDetailsList); //updated
}
}

 



With this, you can get the count as follows if you've the parentAccId.

servMap.get(parentAccId).get(0) -> first child record detail for your parentAccId
childDetails.get(0) -> child record id
childDetails.get(1) -> count
 

Note: Please validate the code against your api names, as I might be using some random names for unknown attributes.

 

Thanks,
Anoop

This was selected as the best answer
NishaCNishaC

i have implmented ur idea but the problem is

 

Account ID is ID type and count of pickup days is decimal type field

so how to do that because it is giving error because type of list is ID

Error: Initial expression is of incorrect type, expected: Id

 

Id AccountId = serObj.Customer__r.id;
Decimal count = serObj.Count_of_Pickup_Days__c; //Added
List<Id> childDetailsList = new List<Id>{AccountId, count};

NishaCNishaC

thanks for ur reply.....

i have implemented it and it works:

:)

but now the next step is 

i wanted to Sum that Count value 

system debug of servMap:-

--servMap---{0012000000WekRxAAJ=((0012000000klmcVAAQ, 0), (0012000000oqfRSAAY, 1), (0012000000oqfRSAAY, 2), (0012000000eX4psAAC, 1), (0012000000eX4psAAC, 3), (0012000000cVX9hAAG, 2), (0012000000bGLicAAG, 1), (0012000000eZWRAAA4, 5), (0012000000eZWRAAA4, 5), (0012000000mft7CAAQ, 1), ...)

i have parent id and corresponding to that i have child ids and their count.......now i want to Sum that value of all count

ANY idea ?

Anoop AsokAnoop Asok

Hi,

I assume that you're trying to calculate the sum of all count for a particular parent id.

If that is the case, you can add a new Map<Id, Integer> to your code as below.

 

 

Map<Id,List<List<String>>> servMap = new Map<Id,List<List<String>>>(); //Updated
Map<Id, Integer> servCountMap = new Map<Id, Integer>();
for(Service__c serObj: serviceList){
Id parentAccId = serObj.Customer__r.Grand_Parent_Account__c;
String AccountId = serObj.Customer__r.id;
String count = serObj.Customer__r.CountOfPickupDays; //Added
List<String> childDetailsList = new List<String>{AccountId, count}; //Added
if(serObj.Status__c == 'Active'){
if(servMap.get(parentAccId)==null)
{
servMap.put(parentAccId,new list<List<String>>{childDetailsList}); //updated
} else { servMap.get(parentAccId).add(childDetailsList); //updated
count = String.valueOf(Integer.valueOf(count) + servCountMap.get(parentAccountId));
}
servCountMap.put(parentAccId, Integer.valueOf(count)); }

 

Whenever you want to get the total count for a particular parentId, you can use servCountMap.get(parentId).

 

 

Thanks,

Anoop

Anoop AsokAnoop Asok

Oops, forgot that your count is decimal. Please note that I'd used Integer in the above code. If you're using this code, make sure proper type casting is done.

 

Thanks,

Anoop

Anoop AsokAnoop Asok

Just an afterthought, with all the type casting and complicated collections, doesn't the code look a bit unclean?

For the two requirements you stated above, I think the following code will be a much more cleaner version. :)

 

Map<Id, List<Id>> servCustomerMap = new Map<Id, Id>();
Map<Id, Decimal> servCountMap = new Map<Id, Integer>();
for(Service__c serObj: serviceList){
    Id parentAccId = serObj.Customer__r.Grand_Parent_Account__c;
    Id AccountId = serObj.Customer__r.id;
    Decimal count = serObj.Customer__r.CountOfPickupDays;
    if(serObj.Status__c == 'Active'){
        if(servCustomerMap.get(parentAccId)==null)
            servCustomerMap.put(parentAccId,new List<String>{AccountId});
        else
            servCustomerMap.get(parentAccId).add(AccountId);
        servCountMap.put(AccountId, count);
    }
}

//Now assuming that you've the parent record for calculating the count in the variable parentId.
Decimal totalCount = 0;
for (Id accountId : servCustomerMap.get(parentId)){
    totalCount = totalCount + servCountMap.get(accountId);
}

Here, note that there will be no calculations involved even if you've multiple Service records with the same Customer record as parent. Another assumption I'd made. :D

 

Please feel free to share your thoughts on this.

 

 

Thanks,

Anoop

NishaCNishaC

thanks anoop for ur reply
i want to show google charts according to date from and date to
so i have created two new fields Date_From__c and Date_To__c
now i want to show them on my visualforce page and in controller coding
can u help me in that

My Vf Page

 

<apex:page controller="WasteSplit" sidebar="false">
<!-- Google API inclusion -->
<apex:includeScript id="a" value="https://www.google.com/jsapi" />
<apex:sectionHeader title="Google Charts" subtitle="Chart 2"/>

<!-- Google Charts will be drawn in this DIV -->

<div id="chartBlock" style="width: 600px; height: 500px;"/>

<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(initCharts);

function initCharts() {
WasteSplit.WasteSource(
function(result, event){
// load Column chart
var visualization = new google.visualization.PieChart(document.getElementById('chartBlock'));
// Prepare table model for chart with columns
var data = new google.visualization.DataTable();
data.addColumn('string', 'Waste Name');
data.addColumn('number', 'Waste Type');

// add rows from the remoting results
for(var i=0; i<result.length;i++){
var finalBean= result[i];
data.addRow([finalBean.WasteName,finalBean.wastetype]);
}
// all done, lets draw the chart with some options to make it look nice.
visualization.draw(data,{title:'Waste Service Split at Source',legend : {position: 'bottom', textStyle: {color: 'blue', fontSize: 10}}, width:window.innerWidth,vAxis:{textStyle:{color:'red', fontSize: 15}},hAxis:{title: 'Record Count',textStyle:{fontSize: 10},showTextEvery:1,slantedText:true}} );
}, {escape:true});



}
</script>
</apex:page>

 

cud u tell me where i can locate these two fields so that chart is showing between those dates

Anoop AsokAnoop Asok

Sorry Nisha, doesn't look like something I can help you with. Way beyond my ability it is.


Why don't you put this as a new Board topic, that way you'll make this issue visible to a larger set of people. Someone might be able to help you out.

 

Thanks,

Anoop