You need to sign in to do that
Don't have an account?
SAHG-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
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
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
Let me see if i can give you some sample code as soon as possible once reached home.
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;
}
}
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
trigger ApttusProposalTrigger on Apttus_Proposal__Proposal__c (After insert,After update,After Delete) {
It must be after Not before triiger
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);
}
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
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?