+ Start a Discussion
SAHG-SFDCSAHG-SFDC 

Check/Unchk chk box on Parent Object

HI,

I am trying to check the chkbox on oppty if their are atleast one child record (Lookup relationship to custom object) , If all records attached to Oppty are deleted, this box should be unchecked.

Any suggestions?
Best Answer chosen by SAHG-SFDC
EldonEldon
Hi,

use the below code. (Assuming the checkbox field in opportunity is WithChild__c and the lookup field to opportunity is opportunity__c and the child relationship name is pobs__r )

 
trigger Devcompob22 on pob__c (after insert, after update, after delete) {
    list<id> ids = new list<id>();
    if(trigger.isinsert || trigger.isupdate){
        
        
        for(pob__c p : trigger.new){
            ids.add(p.opportunity__c);
        }
        list<opportunity> opp = [select id,withchild__c from opportunity where id in :ids];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            o.withchild__c=true;
            opptoupdate.add(o);
        }
        update opptoupdate;
    }
    
    
    if(trigger.isdelete){
        
        list <id> ids2 = new list<id>();
        for(pob__c p : trigger.old){
            ids2.add(p.opportunity__c);
        }
      
      
        list<opportunity> opp = [select id,withchild__c,(select id,name from pobs__r) from opportunity where id in :ids2];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            
            if(o.pobs__r.size()<1){
                o.WithChild__c = false;
                opptoupdate.add(o);
            }
        }
        update opptoupdate;
    }
}


Let me know if you have any issues.

Mark it as best answer if it works.

Regards

All Answers

EldonEldon
Hi,

use the below code. (Assuming the checkbox field in opportunity is WithChild__c and the lookup field to opportunity is opportunity__c and the child relationship name is pobs__r )

 
trigger Devcompob22 on pob__c (after insert, after update, after delete) {
    list<id> ids = new list<id>();
    if(trigger.isinsert || trigger.isupdate){
        
        
        for(pob__c p : trigger.new){
            ids.add(p.opportunity__c);
        }
        list<opportunity> opp = [select id,withchild__c from opportunity where id in :ids];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            o.withchild__c=true;
            opptoupdate.add(o);
        }
        update opptoupdate;
    }
    
    
    if(trigger.isdelete){
        
        list <id> ids2 = new list<id>();
        for(pob__c p : trigger.old){
            ids2.add(p.opportunity__c);
        }
      
      
        list<opportunity> opp = [select id,withchild__c,(select id,name from pobs__r) from opportunity where id in :ids2];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            
            if(o.pobs__r.size()<1){
                o.WithChild__c = false;
                opptoupdate.add(o);
            }
        }
        update opptoupdate;
    }
}


Let me know if you have any issues.

Mark it as best answer if it works.

Regards
This was selected as the best answer
MoggyMoggy
you don't need code for that
create two custom fields , one to be a roll-up summary ( Count) on the related records, one to be a formula of type checkbox
IF(Rollup summary > 0,true,false)
replace Rollup summary with the name of the new field usually ending "__c"

advantage, no code  
EldonEldon
Moggy we cant do roll up in look up relationship
MoggyMoggy
yeah i over saw the look up relation, but if it is important enough to know if there is a child, why not master detail then?
The other question would be , can they use code at all ( lower then enterprise edition ? )
 
SAHG-SFDCSAHG-SFDC
Hi Thanks for replying,  This might be a seemingly simple question, However, What is lookup field to opportunity? Where does the relationship name resides?
SAHG-SFDCSAHG-SFDC
I think Trigger is the option we will go with, However, I am Not sure on referencing the child object with correct name, Eldon and Moggy thanks for responding
EldonEldon
Look up field is the field on your custom object where you select the opportunity related to that object. And if you view that lookup field you can see the child relationship name replace the pobs__r with what you have given there as child relationship name. 
SAHG-SFDCSAHG-SFDC
Thanks for pointing that, this name is alphanumeric and this custom object is part of managed package , does it matter?
SAHG-SFDCSAHG-SFDC
Thanks this should work ,  I modified the quote, I have another question of similar kind - This custom object has a field (chkbox-Primary),If an opportunity has this record with primary checked and stage = Approved - then chk (chkbox2) on opportunity 
If the record which has this chked (on custom object) is  deleted or the stage for this record is changed from Approved to something else then unchk (chkbox2) on opportunity

Can This logic be in the same trigger as above? (This is the same custom object as above)

Many THANKS! :)
EldonEldon
Try this and let me know if it worked. (second checkbox in custom object is checkbox2__c and stage in custom object is stage__c)

 
trigger Devcompob22 on pob__c (after insert, after update, after delete) {
    list<id> ids = new list<id>();
    boolean flag = false;
    if(trigger.isinsert || trigger.isupdate){
        
        
        for(pob__c p : trigger.new){
            ids.add(p.opportunity__c);
        }
        list<pob__c> pobslist = [select id,name,status__c,primaryCheck__c from pob__c where id in :ids];
        
        list<opportunity> opp = [select id,withchild__c from opportunity where id in :ids];
        
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            flag = false;
            o.withchild__c=true;
            for(pob__c p : pobslist){
                if(p.opportunity__c == o.id ){
                    if(p.status__c == "Approved" && p.primaryCheck__c == true ){
                        o.checkbox2__c = true;
                        flag = true;
                    }
                    
                }
            }
            
            if(flag == false){
                o.checkbox2__c = false;
            }
            
            opptoupdate.add(o);
        }
        update opptoupdate;
    }
    
    
    if(trigger.isdelete){
        
        list <id> ids2 = new list<id>();
        
        for(pob__c p : trigger.old){
            
            ids2.add(p.opportunity__c);
            
        }
        list<pob__c> pobslist = [select id,name,status__c,primaryCheck__c from pob__c where id in :ids2];
        
        list<opportunity> opp = [select id,withchild__c,(select id,name from pobs__r) from opportunity where id in :ids2];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            
            if(o.pobs__r.size()<1){
                
                o.WithChild__c = false;
                opptoupdate.add(o);
            }
            
            flag = false;
            
            for(pob__c p : pobslist){
                if(p.opportunity__c == o.id ){
                    if(p.status__c == "Approved" && p.primaryCheck__c == true ){
                        o.checkbox2__c = true;
                        flag = true;
                    }
                    
                }
            }
            
            if(flag == false){
                o.checkbox2__c = false;
            }
        }
        update opptoupdate;
    }
}


Regards
SAHG-SFDCSAHG-SFDC
Hi Thanks for replying, What is flag here? Do we need the relationship in the query?  I am getting an error - && can only be  used for Boolean operations
SAHG-SFDCSAHG-SFDC
I got the second functionality working , but this original functionality does not seem to work

"HI,

I am trying to check the chkbox on oppty if their are atleast one child record (Lookup relationship to custom object) , If all records attached to Oppty are deleted, this box should be unchecked.

Any suggestions?"


No errors , and not working

How do we check if the parent actually has any child record?  When I tried with the  relatioship name it gave an error "Aliasing not allowed"
EldonEldon
Hi

Flag is a boolean variable to know if there is any record satisfying your req.
Was it working before adding this second functionality?

Try this code and let me know
 
trigger Devcompob22 on pob__c (after insert, after update, after delete) {
    list<id> ids = new list<id>();
    boolean flag = false;
    if(trigger.isinsert || trigger.isupdate){
        
        
        for(pob__c p : trigger.new){
            ids.add(p.opportunity__c);
        }
        list<pob__c> pobslist = [select id,name,status__c,primaryCheck__c from pob__c where id in :ids];
        
        list<opportunity> opp = [select id,withchild__c from opportunity where id in :ids];
        
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            flag = false;
            o.withchild__c=true;
            for(pob__c p : pobslist){
                if(p.opportunity__c == o.id ){
                    if(p.status__c == "Approved" && p.primaryCheck__c == true ){
                        o.checkbox2__c = true;
                        flag = true;
                    }
                    
                }
            }
            
            if(flag == false){
                o.checkbox2__c = false;
            }
            
            opptoupdate.add(o);
        }
        update opptoupdate;
    }
    
    
    if(trigger.isdelete){
        
        list <id> ids2 = new list<id>();
        
        for(pob__c p : trigger.old){
            
            ids2.add(p.opportunity__c);
            
        }
        list<pob__c> pobslist = [select id,name,status__c,primaryCheck__c from pob__c where id in :ids2];
        
        list<opportunity> opp = [select id,withchild__c,(select id,name from pobs__r) from opportunity where id in :ids2];
        list<opportunity> opptoupdate = new list<opportunity>(); 
        for(opportunity o : opp){
            
            if(o.pobs__r.size()<1){
                
                o.WithChild__c = false;
               
            }
            
            flag = false;
            
            for(pob__c p : pobslist){
                if(p.opportunity__c == o.id ){
                    if(p.status__c == "Approved" && p.primaryCheck__c == true ){
                        o.checkbox2__c = true;
                        flag = true;
                    }
                    
                }
            }
            
            if(flag == false){
                o.checkbox2__c = false;
            }
             opptoupdate.add(o);
        }
        update opptoupdate;
    }
}

 
SAHG-SFDCSAHG-SFDC
Hi, Thanks for replying, the second functionality is working, This -----" I am trying to check the chkbox on oppty if their are atleast one child record (Lookup relationship to custom object) , If all records attached to Oppty are deleted, this box should be unchecked.""  is not

I am unable to check how to see if there are any child records, If there is atleast one check the box

Why are you using the relationship field? I think this is from line 47 correct? Apologize if its too silly just trying to get it together
EldonEldon
Check with the code i first send you and tell me if first functionality is working or not
Relationship field pobs__r is used to query all childs relatd to opportunities. 
SAHG-SFDCSAHG-SFDC
I checked with the first code, I was able to get the check box uncheck if no child records exist but not check if atleast one record exist

Any idea what we are missing ?