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
Shawnh77Shawnh77 

Newbie question for a custom count

Hello,

 

I am trying to count the number of child records based on a custom object of "Status_c".  My Parent is "Case" and the child records are "AIO".  On the AIO record there is the Status and a few other fields.  What I am trying to do is count the children based on the status. Once I have that count I am writing it back to another custom object in the parent "Count_of_M".   I've been looking through other posts and have been unsuccessful in adapting the examples posted to do what I need. Thank you in advance for any suggestions!!

 

Shawn.

 

 

trigger CountRelatedCallLogs_fromLog on AIO__c (after insert, after update) {


    AIO__c [] scl = Trigger.new;
    String sid = null;
//    progress = "In Progress";
    sid = scl[0].Related_Case__c;
        
         Integer i = [select count() from AIO__c where Status_c = :sid];    

        // update the master record
    Case_ID__c [] s =[select id, Count_of_Pending__c from Case_ID__c where id = :sid];
 
    s[0].Call_Count__c = i; 
        // write to database
        update s[0];

}

 

Starz26Starz26

Is this a master detail relationship? If so, seems a roll up summary would work well.

Sam27Sam27
trigger CountRelatedCallLogs_fromLog on AIO__c (after insert, after update) {


    for(AIO__c scl : Trigger.new){
    String sid = null;
//    progress = "In Progress";
    sid = scl.Related_Case__c;
        
         Integer i = Database.countquery('select count() from AIO__c where Status_c ='+sid);    

        // update the master record
    Case_ID__c [] s =[select id, Count_of_Pending__c from Case_ID__c where id = :sid];
 
    s[0].Call_Count__c = i; 
        // write to database
        update s[0];
    }
}

 Hope it helps

Shawnh77Shawnh77

Sam,

 

Thank you for the feedback.  I clearly do not understand the code.  What I am trying to do is count the number of child objects that meet a certain status.  I have a custom object called "Status__c" Becuase they are seperate records I do not have the child (related case) as an option in a roll up summary.  I tried using the code you supplied but get an error on compile that the object is not supported on Line12.

 

Thank you for your help. 

Shawnh77Shawnh77

Starz26 wrote:

Is this a master detail relationship? If so, seems a roll up summary would work well.




 

Unfortunately no.  They are related objects in a Parent/Child relationship.

Starz26Starz26

When you say/ parent/child, you mean a Lookup relationship?

Shawnh77Shawnh77

Starz26 wrote:

When you say/ parent/child, you mean a Lookup relationship?


Yes,  They are two separate records.  One is the standard CASE and the other is a different layout of CASE where I am adding the first case number to the layout.  I have a field on the first CASE that will serve to hold the quantity of the second record type where a certain status (a custom object picklist) meets the criteria.  There are four total fields on the first record:  Total count, total pending, total complete, total new. 

 

I really appreciate your assistance.


Shawn

Shawnh77Shawnh77

Ok, so I think this should do what I need.However I am getting the folowing error.  "Error: Compile Error: Invalid type: Case__c at line 10 column 5".  Any Ideas?  I was thinking that I needed to use Case__c to reference back to the main record.  AIO_Serials__c is my second record.

 

trigger CountMTM on AIO_Serials__c (after insert, after update) {

    AIO_Serials__c [] scl = Trigger.new;
    String sid = null;
    sid = scl[0].Case__c;
        
         Integer i = [select count() from AIO_Serials__c where Case__c = :sid]; 

        // update the master record
    Case__c [] s =[select id from AIO_Serials__c where id = :sid];
 
    s[0].Count_of_Machines__c = i; 
        // write to database
        update s[0];

}