You need to sign in to do that
Don't have an account?
Trigger for updating parent records based on child object records
Hi am writing the trigger updating the parent object record based on child records
i have 2 custom objects
1 Opportunity__c,
2 work__c (look up to opportunity) --- this object i have one check box field Received__c if it is true i need to update the parent object field Received__c =true,
if in any case user will try to update the parent object field Received__c=true ,it should have to check the child records ,in child records it is true ot not,
if its is not true i need to show error message.
can any one tell me where am missing
trigger Received on Opportunity__c(before update) {
for(Opportunity__c op:trigger.new)
{
for(work__c ar:[select id,Received__c from work__c where Opportunity__c=:op.id])
{
system.debug('error message1');
if((ar.Received__c==false) && (op.Received__c==true))
{
system.debug('error messagesssssssssssss');
op.adderror('Received is not checked please check in work records');
system.debug('error message'+op.Received__c);
}
if(ar.Received__c=true)
{
op.Received__c=true;
}
}
}
}
regards
venkatesh
1) you mean to say check all the child record = true then only allow to make Received__c=true on parent?
or
2) if one child is true, allow the parent Received__c=true
how should the code work?
hi devender
thanks for your replay
i need this
2)if one child is true, allow the parent Received__c=true
Try this code.
trigger Received on Opportunity__c(before update) {
for(Opportunity__c op:trigger.new)
{
if(op.Received__c==true){
for(work__c ar:[select id,Received__c from work__c where Opportunity__c=:op.id])
{
system.debug('error message1');
if(ar.Received__c==false)
{
system.debug('error messagesssssssssssss');
op.adderror('Received is not checked please check in work records');
system.debug('error message'+op.Received__c);
}
else if(ar.Received__c==true){
op.Received__c=true;
}
}
}
}
}
Thanks,
Subhani,
Salesforce Certified Developer,
www.mydbsync.com.
trigger Received on Opportunity__c(before update) {
set<Id> setOfids = new set<Id>();
for(Opportunity__c op:trigger.new)
{
if(op.Received__c==true){
setOfids.add(op.Id);
}
}
if(!setOfids.isEmpty()) {
list<work__c> listOfwork = [select Opportunity__c,id,Received__c from work__c where Opportunity__c =: setOfids];
map<Id, Boolean> map_OppId_HasChildActive = = new map<Id, Boolean>();
for(work__c work : listOfwork) {
if(work.Received__c == true) {
map_OppId_HasChildActive.put(work.Opportunity__c , true);
}
}
for(Opportunity__c op:trigger.new)
{
if(!map_OppId_HasChildActive.containsKey(op.Id)) {
op.adderror('Received is not checked please check in work records');
}
}
}
}