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 

I'm so close. Help Needed for newbie apex coder

Hello, 

TIA for your assistance.

 

So the scenario is that I have a Parent case and many child cases.  However they are not in a Master relationship.  When the child cases are being created I have a trigger being fired that will update fields on the parent with a could of the total children.  Ultimately I will also have additional triggers that will also update additional fields based on a second field (Status__c) on the child. 

 

Currently even if I create one new child record I am getting an error with too many SOQL queries.  I am guessing based on what I've read in other posts that I have a query inside a loop.  However I cannot figure out how to get around this.  Any suggestions would be GREATLY appreciated.

 

Here's my trigger and class:

 

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

    AIO_Serials__c [] scl = Trigger.new;
   CountMTM.CountMTMS(scl);
   }

 

public class CountMTM {
public static void CountMTMS (AIO_Serials__c[] scl) {
for (AIO_Serials__c b :scl) {
   String sid=null;
   sid=scl[0].Case__c;
   Integer i = [select count() from AIO_Serials__c where Case__c = :sid];
   Case[] s=[select Count_of_Machines__c from Case where id=:sid];
   s[0].Count_of_Machines__c=i;
   system.debug('value is not there'+i);
   update s[0];
      }
   }
   }

 

Alok_NagarroAlok_Nagarro

Hi,

 

Try the code given below -

Text in Red color is the parent-child relationship name - change accordingly.

 

public class CountMTM {
    public static void CountMTMS (AIO_Serials__c[] scl)
    {
        List<String> sid=new List<String>();
        for(AIO_Serials__c aio:scl)
        {
         sid.add(aio.Case__c);
        }
        list<Case> Allcase =new list<Case>();
        for (Case s :[select Count_of_Machines__c,(select id from Cases) from Case where id IN:sid])
        {
            
           s.Count_of_Machines__c=s.Cases.size();
           Allcases.add(s);
        
        }
        if(Allcases.size()>0)
            update Allcases;
    }
}

Shawnh77Shawnh77

Alok_vibrant wrote:

Hi,

 

Try the code given below -

Text in Red color is the parent-child relationship name - change accordingly.

 

public class CountMTM {
    public static void CountMTMS (AIO_Serials__c[] scl)
    {
        List<String> sid=new List<String>();
        for(AIO_Serials__c aio:scl)
        {
         sid.add(aio.Case__c);
        }
        list<Case> Allcase =new list<Case>();
        for (Case s :[select Count_of_Machines__c,(select id from Cases) from Case where id IN:sid])
        {
            
           s.Count_of_Machines__c=s.Cases.size();
           Allcases.add(s);
        
        }
        if(Allcases.size()>0)
            update Allcases;
    }
}


 

 

Please forgive my ignorance.  I do not have a custom relationship setup.  Would your Cases above be the name of the child object (AIO_Serials) or the paretn (Case)?

 

Thank you

Alok_NagarroAlok_Nagarro

Hi,

 

After analyzing your code , it seems that you have lookup field in AIO_Serials__c object and that field's API name is Case__c.

So you have parent-child relationship between Case and AIO_Serials__c object , right ?

 

If it is then you must have parent-child relationship name. You can find it from goto object's detail page --> Click on "Case__c" in "Custom Fileds and Relationship" section -->here you have parent-child relationship name.

 

If it's not then please explain more in details.