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
shabbir ahmmed 17shabbir 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.
Best Answer chosen by shabbir ahmmed 17
suresh beniwal 21suresh beniwal 21
Hi Shabbir,

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

Khan AnasKhan Anas (Salesforce Developers) 
Hi Shabbir,

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.
 
trigger CustTypeCountOnOpp on Custom_Object__c (after insert, after delete,after undelete,after update) {
    
    Set<Id> AccID = new Set<Id>();
    list<Custom_Object__c> ConAdminList =new list<Custom_Object__c>();
    list<Custom_Object__c> ConBrokerList =new list<Custom_Object__c>();
    list<Custom_Object__c> ConPrincipalList =new list<Custom_Object__c>();
    if(Trigger.isInsert || Trigger.isUndelete)
    {
        for(Custom_Object__c con : Trigger.New)
        {
            AccID.add(con.Opportunity__c);
        }
        List<Opportunity> acc_list = [SELECT Id, Count_of_CategoryA__c,Count_of_CategoryB__c,Count_of_CategoryC__c,(SELECT Type__c FROM Custom_Objects__r) FROM Opportunity WHERE Id IN:AccID];
        List<Custom_Object__c> con_list = [SELECT Id, Type__c FROM Custom_Object__c WHERE Opportunity__c IN :AccID];
        for (Custom_Object__c con: con_list) 
        {
            if(con.Type__c=='Category_A')
            {
                ConAdminList.add(con);
            }
            if(con.Type__c=='Category_B')
            {
                ConBrokerList.add(con);
            }
            if(con.Type__c=='Category_C')
            {
                ConPrincipalList.add(con);
            }
        }
        
        UPDATE ConAdminList;
        UPDATE ConBrokerList;
        UPDATE ConPrincipalList;
        for(Opportunity a : acc_list)
        {
            a.Count_of_CategoryA__c=ConAdminList.size();
            a.Count_of_CategoryB__c=ConBrokerList.size();
            a.Count_of_CategoryC__c=ConPrincipalList.size();
        }
        try
        {
            UPDATE acc_list;
        }
        catch(DMLException e) 
        {
            for (Custom_Object__c con : trigger.new) 
            {
                con.addError(e.getDmlMessage(0));
            }
        }     
    }
    
    if(Trigger.isDelete)
    {
        for(Custom_Object__c con : Trigger.old)
        {
            AccID.add(con.Opportunity__c);
        }
        List<Opportunity> acc_list = [select id,Count_of_CategoryA__c,Count_of_CategoryB__c,Count_of_CategoryC__c,(select Type__c from Custom_Objects__r) from Opportunity where Id in:AccID];
        List<Custom_Object__c> con_list = [select id,Type__c from Custom_Object__c where Opportunity__c in :AccID];
        for (Custom_Object__c con: con_list) 
        {
            if(con.Type__c=='Category_A')
            {
                ConAdminList.add(con);
            }
            if(con.Type__c=='Category_B')
            {
                ConBrokerList.add(con);
            }
            if(con.Type__c=='Category_C')
            {
                ConPrincipalList.add(con);
            }
        }
        update ConAdminList;
        update ConBrokerList;
        update ConPrincipalList;
        for(Opportunity a : acc_list)
        {
            a.Count_of_CategoryA__c=ConAdminList.size();
            a.Count_of_CategoryB__c=ConBrokerList.size();
            a.Count_of_CategoryC__c=ConPrincipalList.size();
        }
        try
        {
            update acc_list;
        }
        catch(DMLException e) 
        {
            for (Custom_Object__c con : trigger.new) 
            {
                con.addError(e.getDmlMessage(0));
            }
        }    
    }
    
    if(Trigger.isUpdate)
    {
        Set<Id> OldAId_set = new Set<Id>(); 
        for(Custom_Object__c con : Trigger.new)
        {
            //For new Custom Objects
            if(con.Opportunity__c != Trigger.oldMap.get(con.id).Opportunity__c)
            {
                AccID.add(con.Opportunity__c);
            }
            //For old Custom Objects with updated type
            if(con.Opportunity__c == Trigger.oldMap.get(con.id).Opportunity__c && con.type__c !=Trigger.oldMap.get(con.id).type__c)
            {
                AccID.add(con.Opportunity__c);
            }
            OldAId_set.add(Trigger.oldMap.get(con.id).Opportunity__c);
        }
        if(!AccID.isEmpty())
        {
            //for new Opportunities
            List<Opportunity> acc_list = [select id,Count_of_CategoryA__c,Count_of_CategoryB__c,Count_of_CategoryC__c, (select Type__c from Custom_Objects__r) from Opportunity where Id in:AccID];
            //For New Opportunity Custom Objects
            List<Custom_Object__c> con_list = [select id,Type__c from Custom_Object__c where Opportunity__c in :AccID];
            for (Custom_Object__c con: con_list) 
            {
                if(con.Type__c=='Category_A')
                {
                    ConAdminList.add(con);
                }
                if(con.Type__c=='Category_B')
                {
                    ConBrokerList.add(con);
                }
                if(con.Type__c=='Category_C')
                {
                    ConPrincipalList.add(con);
                }
            }
            update ConAdminList;
            update ConBrokerList;
            update ConPrincipalList;
            /* This is For Old Custom Objects Count */
            
            //for Old Opportunities
            List<Opportunity> Oldacc = [select id,Count_of_CategoryA__c,Count_of_CategoryB__c,Count_of_CategoryC__c, (select Type__c from Custom_Objects__r) from Opportunity where Id in:OldAId_set];
            
            //For Old Opportunities Custom Objects
            List<Custom_Object__c> OldConAdmin = [select id,Type__c from Custom_Object__c where Opportunity__c in :OldAId_set and Type__c='Category_A'];
            List<Custom_Object__c> OldConBroker = [select id,Type__c from Custom_Object__c where Opportunity__c in :OldAId_set and Type__c='Category_B'];
            List<Custom_Object__c> OldConPrincipal = [select id,Type__c from Custom_Object__c where Opportunity__c in :OldAId_set and Type__c='Category_C'];
            //For New Opportunities
            for(Opportunity a : acc_list)
            {
                a.Count_of_CategoryA__c=ConAdminList.size();
                a.Count_of_CategoryB__c=ConBrokerList.size();
                a.Count_of_CategoryC__c=ConPrincipalList.size();
            }
            try
            {
                update acc_list;
            }
            catch(DMLException e) 
            {
                for (Custom_Object__c con : trigger.new) 
                {
                    con.addError(e.getDmlMessage(0));
                }
            }    
            
            //For Old Opportunities
            for(Opportunity a : Oldacc)
            {
                a.Count_of_CategoryA__c=OldConAdmin.size();
                a.Count_of_CategoryB__c=OldConBroker.size();
                a.Count_of_CategoryC__c=OldConPrincipal.size();
            }
            try
            {
                update Oldacc;
            }
            catch(DMLException e) 
            {
                for (Custom_Object__c con : trigger.new) 
                {
                    con.addError(e.getDmlMessage(0));
                }
            }    
        }
    }
}

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
suresh beniwal 21suresh beniwal 21
Hi Shabbir,

Please see the below code which will solve your query.
trigger newObjectTrigger on New_Object__c (after delete,before update, after update, after insert, after undelete) {
    list<Opportunity> ct = new list<Opportunity>();
    set<id> classroomIDs = 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(New_Object__c newObject : Trigger.new){
            if (newObject.category__c == 'Category A') {
                mapCountOfA.put(newObject.Opportunity__c, countOfA++);
            }
            if (newObject.category__c == 'Category B') {
                mapCountOfB.put(newObject.Opportunity__c, countOfB++);
            }
            if (newObject.category__c == 'Category C') {
                mapCountOfC.put(newObject.Opportunity__c, countOfC++);
            }
        }
    }
    else if(trigger.isDelete){
        for(New_Object__c newObject : Trigger.old){
            if (newObject.category__c == 'Category A') {
                mapCountOfA.put(newObject.Opportunity__c, countOfA++);
            }
            if (newObject.category__c == 'Category B') {
                mapCountOfB.put(newObject.Opportunity__c, countOfB++);
            }
            if (newObject.category__c == 'Category C') {
                mapCountOfC.put(newObject.Opportunity__c, countOfC++);
            }
        }
    }
    else if(trigger.isUnDelete){
        for(New_Object__c newObject : Trigger.new){
            if (newObject.category__c == 'Category A') {
                mapCountOfA.put(newObject.Opportunity__c, countOfA++);
            }
            if (newObject.category__c == 'Category B') {
                mapCountOfB.put(newObject.Opportunity__c, countOfB++);
            }
            if (newObject.category__c == 'Category C') {
                mapCountOfC.put(newObject.Opportunity__c, countOfC++);
            }
        }
    }
    else if(trigger.isUpdate){
        for(New_Object__c newObject : trigger.new){
            if(trigger.oldmap.get(newObject.id).category__c !=newObject.category__c || trigger.oldmap.get(newObject.id).Opportunity__c !=newObject.Opportunity__c){
                if (newObject.category__c == 'Category A') {
                    mapCountOfA.put(newObject.Opportunity__c, countOfA++);
                }
                if (newObject.category__c == 'Category B') {
                    mapCountOfB.put(newObject.Opportunity__c, countOfB++);
                }
                if (newObject.category__c == 'Category C') {
                    mapCountOfC.put(newObject.Opportunity__c, countOfC++);
                }
            }
        }
    }
    
    
    
    AggregateResult[] groupedResults = [SELECT COUNT(Id), Opportunity__c, category__c FROM New_Object__c GROUP BY category__c, Opportunity__c];
    
    Map<Id, Opportunity> updateOpportunity = new Map<Id, Opportunity>();
    for(AggregateResult ar:groupedResults) {
        Id custid = (ID)ar.get('Opportunity__c');
        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.countOfA__c = count + (mapCountOfA.ContainsKey(custid) == true ? mapCountOfA.get(custid) : 0);
        }if (type == 'Category B') {
            cust1.countOfB__c = count + (mapCountOfB.ContainsKey(custid) == true ? mapCountOfB.get(custid) : 0);
        }if (type == 'Category C' ) {
            cust1.countOfC__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();
    
}

Hope this will help
if this solve your query please mark this as best answer.

Thanks,
Suresh Beniwal
shabbir ahmmed 17shabbir ahmmed 17
@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.
User-added image
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();
    
}
 
suresh beniwal 21suresh beniwal 21
Hi Shabbir,

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
This was selected as the best answer
shabbir ahmmed 17shabbir ahmmed 17
Hi Suresh,

Thanks...It worked...