You need to sign in to do that
Don't have an account?
shabbir ahmmed 17
How to display the count of each type in the parent details record.
create a custom object and it should have a field which is a lookup relation to Opportunity. Create one more picklist field is Type__c(Category_A, Category_B, Category_C). three fields should created on Opportunity(Count of CategoryA,Count of CategoryB, Count of CategoryC). display the count of eachtype in the parent details record.
On line 16,20,24 you need to specify the apiname for the opportunity lookup field which you created on new object.
Hope this will help
if this solve your query please mark this as best answer.
Thanks,
Suresh Beniwal
All Answers
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Please see the below code which will solve your query.
Hope this will help
if this solve your query please mark this as best answer.
Thanks,
Suresh Beniwal
Hey, Thanks for the solution.
I tried customizing the apex code, but it is throwing a error(Screenshot Attached)
Can anyone find out why it is showing that compile time error.
trigger newtest on Devtest__c (after insert) {
list<Opportunity> ct = new list<Opportunity>();
//set<id> cIDs = new set<id>();
Map<Id,Integer> mapCountOfA = new Map<Id, Integer>();
Map<Id,Integer> mapCountOfB = new Map<Id, Integer>();
Map<Id,Integer> mapCountOfC = new Map<Id, Integer>();
Integer countOfA = 0;
Integer countOfB = 0;
Integer countOfC = 0;
if(trigger.isInsert){
for(Devtest__c newObject : Trigger.new)
{
if (newObject.Category__c == 'Category_A')
{
mapCountOfA.put(newObject.Opportunity, countOfA++);
}
if (newObject.Category__c == 'Category_B')
{
mapCountOfB.put(newObject.Opportunity, countOfB++);
}
if (newObject.Category__c == 'Category C')
{
mapCountOfC.put(newObject.Opportunity, countOfC++);
}
}
}
AggregateResult[] groupedResults = [SELECT Category__c,COUNT(Id) FROM Devtest__c GROUP BY Category__c];
Map<Id, Opportunity> updateOpportunity = new Map<Id, Opportunity>();
for(AggregateResult ar:groupedResults)
{
Id custid = (ID)ar.get('Opportunity');
Integer count = (INTEGER)ar.get('expr0');
String type = (String) ar.get('category__c');
Opportunity cust1 = new Opportunity();
if (!updateOpportunity.containsKey(custid))
{
cust1 = new Opportunity(Id=custid);
} else
{
cust1 = updateOpportunity.get(custid);
}
if (type == 'Category_A')
{
cust1.Count_of_CategoryA__c = count + (mapCountOfA.ContainsKey(custid) == true ? mapCountOfA.get(custid) : 0);
}if (type == 'Category_B')
{
cust1.Count_of_CategoryB__c = count + (mapCountOfB.ContainsKey(custid) == true ? mapCountOfB.get(custid) : 0);
}if (type == 'Category_C' )
{
cust1.Count_of_CategoryC__c = count + (mapCountOfC.ContainsKey(custid) == true ? mapCountOfC.get(custid) : 0);
}
//system.assert(false,cust1);
updateOpportunity.put(custid, cust1);
}
// system.assert(false,updateOpportunity);
update updateOpportunity.Values();
}
On line 16,20,24 you need to specify the apiname for the opportunity lookup field which you created on new object.
Hope this will help
if this solve your query please mark this as best answer.
Thanks,
Suresh Beniwal
Thanks...It worked...