+ Start a Discussion
SAHG-SFDCSAHG-SFDC 

How to check if there are associated child records?

HI, I am trying to check a chk box if there is a child record present , if no then unchk this chk box (look up relation to the custom object from Opportunity)
list<id> ids = new list<id>();
    if(trigger.isinsert || trigger.isupdate){
        
        
       for(Apttus_Proposal__Proposal__c p : trigger.new){
            ids.add(p.Apttus_Proposal__Opportunity__c);
        }
        list<opportunity> opp = [select id,Has_Quote_Proposal__c from opportunity where id in :ids];
        list<opportunity> oppttyupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            o.Has_Quote_Proposal__c=true;
            oppttyupdate.add(o);
        }
        update oppttyupdate;
    }
    
    
    if(trigger.isdelete){
        
        list <id> ids3 = new list<id>();
        for(Apttus_Proposal__Proposal__c p : trigger.old){
            ids3.add(p.Apttus_Proposal__Opportunity__c);
        }
      
      
        list<opportunity> opp = [select id,Has_Quote_Proposal__c,(select id,name from XXXXX0000001yUfDEAU__r) from opportunity where id in :ids3];
        list<opportunity> oppttyoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            
            if(o.XXXXXXX0000001yUfDEAU__r.size()<1){
                o.Has_Quote_Proposal__c = false;
                oppttyoupdate.add(o);
            }
        }
        update oppttyoupdate;              
            }
                                     }
                             
                     }
Advance thanks 
Prasad Avala(SFDC)Prasad Avala(SFDC)
Please check the below link, its similar to your case but not exactly same.

You need to write Trigger on Child not on Parent.

http://salesforce.stackexchange.com/questions/78563/update-checkbox-on-parent-when-any-of-its-child-records-are-updated-deleted-p
SAHG-SFDCSAHG-SFDC
HI Prasad,

Thanks for the reply , my req is if there is atleast one child record check the box , I am not sure how to retrive that or check that
Prasad Avala(SFDC)Prasad Avala(SFDC)
Its the same. You need to write the trigger on Apttus_Proposal__Proposal__c (assuming its child) (before insert, Before Update, Before delete).

Let me see if i can give you some sample code as soon as possible once reached home.

 
SAHG-SFDCSAHG-SFDC
Thanks, I wrote the trigger on the Apttus_Proposal__Proposal__c , I am not getting an error however, Not getting the output as well
 
if(trigger.isinsert || trigger.isupdate){ 
        list <id> ids4 = new list<id>();
        for(Apttus_Proposal__Proposal__c p : trigger.new){
            ids4.add(p.Apttus_Proposal__Opportunity__c);
        }
        list<opportunity> op = [select id,Has_Quote_Proposal__c,(select id,name from xxxxxxxxxEAU__r) from opportunity where id in :ids4];
        list<opportunity> oppupdate = new list<opportunity>(); 
        for(opportunity o : op){   
            if(o.xxxxxxxxxEAU__r.size()>0){
                o.Has_Quote_Proposal__c = true;
                oppupdate.add(o);
            }
        }
        
        update oppupdate;              
            }
             }

 
Prasad Avala(SFDC)Prasad Avala(SFDC)
You are almost there and actually need to write the trigger after update , insert and Delete:

Below is the completed code: Its abest practice to write Triger and seperate helper class.


Trigger:
Create a Trigger from Developer console with Name : ApttusProposalTrigger   and paste the below code

==============================================================

trigger ApttusProposalTrigger  on Apttus_Proposal__Proposal__c  (After insert,After update,After Delete) {
   
ApttusProposalTriggerHelper.childRecordExists(trigger.new,trigger.old);
}
==============================================================

Create a apex class Named: ApttusProposalTriggerHelper and paste the below code, if any syntax errors please correct

================================================================

public class ApttusProposalTriggerHelper {
    Public static void childRecordExists(List<Apttus_Proposal__Proposal__c> ApttusNewLst,List<Apttus_Proposal__Proposal__c> ApttusOldLst)
    {    
        Set<Id> OppIds = New Set<Id>();
        if(ApttusNewLst!=Null)
        {            
            for(Apttus_Proposal__Proposal__c Apttus:ApttusNewLst)   
            {
                if(Apttus.Apttus_Proposal__Opportunity__c!=null)
                {
                    OppIds.add(Apttus.Apttus_Proposal__Opportunity__c);
                }
            }
        }
        if(ApttusOldLst!=Null)
        {
            for(Apttus_Proposal__Proposal__c Apttus:ApttusOldLst)   
            {
                if(Apttus.Apttus_Proposal__Opportunity__c!=null)
                {
                    OppIds.add(Apttus.Apttus_Proposal__Opportunity__c);
                }
            }
        }     
        list<Opportunity> opptoupdate = new list<Opportunity>();
        
        List<Opportunity> oppLst = [Select id, (Select id, Apttus_Proposal__Opportunity__c from Apttus_Proposal__Proposals__r ) from Opportunity where id IN : OppIds];
      
        for(Opportunity opp:oppLst)
        {
            if(opp.Apttus_Proposal__Proposals__r.size()>0)
            {             
                opp.Has_Quote_Proposal__c  = True;                
            }
            else
            {
                opp.Has_Quote_Proposal__c  = false;       
            }
            opptoupdate.add(opp);
        }
        update opptoupdate;        
    }
}
SAHG-SFDCSAHG-SFDC
Hi thanks for replying Do we need a separate helper class? Can this not be done with only trigger?
Prasad Avala(SFDC)Prasad Avala(SFDC)
Hi , it can be done .its a best practice to separate the logic. Let me know if still any issues.
SAHG-SFDCSAHG-SFDC
HI, I am not getting an error however, no desired output as well

I wrote a seperate helper class and used that in the trigger , However, during testing even if there is a proposal the check box is not checked
Prasad Avala(SFDC-SF)Prasad Avala(SFDC-SF)
Did you update the existing trigger as below:

trigger ApttusProposalTrigger  on Apttus_Proposal__Proposal__c  (After insert,After update,After Delete) {

It must be after Not before triiger
SAHG-SFDCSAHG-SFDC
I cannot change the trigger name since it is used for others as well
SAHG-SFDCSAHG-SFDC
It should not matter as well correct? since I added the above lines and created a seperate class
Prasad Avala(SFDC-SF)Prasad Avala(SFDC-SF)
I tested in my org and it works fine.

we ned to tweak a little bit i guess:

Please make sure Trigger has the following 

After insert,After update,After Delete

and then from the trigger call the class as below:

if(Trigger.isafter)
{
If(Trigger.isinsert || Trigger.isupdate ||Trigger.isDelete )
ApttusProposalTriggerHelper.childRecordExists(trigger.new,trigger.old);
}

 
SAHG-SFDCSAHG-SFDC
Trying that, I have the events as mentioned After insert,After update,After Delete (These were already present as part of the existing trigger)
 
SAHG-SFDCSAHG-SFDC
Does not work, Let me tell what I did

1. Apex class created with the code you gave
2. Added the trigger lines in the existing trigger at the botttom
3.Replaced the relationship name Apttus_Proposal__Proposals__r  (in your code) to the correct  name 
Prasad Avala(SFDC-SF)Prasad Avala(SFDC-SF)
Can you provide me the steps how you are testing it? My steps are : 1. Createa brand opportunity. 2. check if the check box is un checked since no child records(brand new) 3. Create a record Apttus_Proposal__Proposal__c record and provide required fields and then select opportunity from look up relationship. 4. once saved go to parent opportunity and see if checkbocx is checked. case 2; Now open the child record(Apttus_Proposal__Proposal__c ) remove the look up opprtunity details and save. Check box should be unselected since no child records exists for parent. Thanks & Regards, Prasad Avala | Technical Analyst | RR Donnelley 314 Susan Drive |Normal,IL -61761 | Office:309-585-3269 Email: prasad.avala@rrd.com
SAHG-SFDCSAHG-SFDC
I was checking with the existing opportunity , Let me create a brand new and check
SAHG-SFDCSAHG-SFDC
Checked with the brand new one, Does not seem to work 

I am really thankful for the help here , not sure what is incorrect , I can give the trigger code you provided any where in the trigger correct?
Prasad Avala(SFDC-SF)Prasad Avala(SFDC-SF)
Sure Please send me Trigger and helper class. Cureently i don't have your custom object set up in my Org, lets start debug it. Thanks & Regards, Prasad Avala | Technical Analyst | RR Donnelley 314 Susan Drive |Normal,IL -61761 | Office:309-585-3269 Email: prasad.avala@rrd.com