0

My Opportunity object has an child object P_ODR__C. Opportunity obejct has checkbox field Nd_Check__c. I am updating it by trigger. I have a trigger on P_ODR__C updating checkbox on Opportunity record to true when when related record field TMMI__C start with 10. if Any related record had other than '10' then uncheck.It is not updating checkbox properly. Anyone can please help.

Method in My Trigger trigger handler to update checkbox :

Boolean QueryrunOnce = false;
Public void updatecheck(List<P_ODR__C> triggerNew) 
{
    set<Id> OppIds = new set<id> ();
    set<Id> OppIdsFalse = new set <Id> ();
    List<Opportunity> ListOpp;
    If (Trigger.isInsert ||Trigger.isUpdate ) 
    {
        For (P_ODR__C od: triggerNew )
        { 
            If(!od.TMMI__C.startWith('10')) 
            {
                OppIdsFalse.add(od.Opportunity); 
            }else 
            {
                OppIds.add(od.Opportunity);
            }
        }
    }
    if(OppIds.size() > 0 && !QueryrunOnce ) 
    {
        ListOpp = New List<Opportunity> [Select Id,Nd_Check__c,(Select TMMI__C from Plan_sum__r where NOT TMMI__C Like '10' ) from Opportunity where id:=OppIds];
    }
    If(ListOpp.size()> 0 && !ListOpp.isEmpty()) 
    {
        For (Opportunity Opps: ListOpp ) 
        {
            If (OppIdsFalse.contains(Opps.id) ||Opps.Plan_sum__r.size() > 0 ) 
            {
                Opps.Nd_Check__c = false;
            }
            Else 
            {
                Opps.Nd_Check__c = true; 
            }
        }
        update ListOpp;
    }
}

Developer Org Code :

trigger UpdateOppForReleaseRec on Release__c (before update,before delete,after insert,after update,after delete) 
{   
    try 
    {
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
        for(Release__c record: trigger.New) 
        {
            if (record.Plan_Sum__r.sub_segment__c!='MM') {
                system.debug('This record is Added for update '+ record.Plan_Sum__r.sub_segment__c );
                oppMap.put(record.Opportunity__c, new Opportunity(Id=record.Opportunity__c, set_rel__c=true));
            }
            //oppMap.put(record.Opportunity__c, new Opportunity(Id=record.Opportunity__c, set_rel__c=true));
            system.debug('Testing  >> '+ record);
        }
        for(AggregateResult result : [SELECT Opportunity__c Id 
                                      FROM Release__c 
                                      WHERE Opportunity__c = :oppMap.keySet() 
                                      AND (NOT Test__c LIKE '14%') 
                                      //AND Plan_Sum__r.sub_segment__c!='MM'
                                      GROUP BY Opportunity__c]) 
        {
            system.debug('Testing-In >>'+ result);
            oppMap.get((Id)result.get('Id')).set_rel__c = false;   
        }
        update oppMap.values();
    }
    catch(exception e) 
    {
        System.debug('The following exception has occurred: ' + e.getMessage());

    }
}
  • This code shouldn't compile. Please verify your code. – sfdcfox Nov 13 '18 at 2:26
  • @sdfcfox Yes because I had to change names so it has some error do you see any logical issue in it ? It would be great if you can give some clue !! Thanks – Sfdc_1184 Nov 13 '18 at 2:52
3

Your main problem is that the checkbox will sometimes not be unchecked because any opportunities in OppIdFalse will not be in ListOpp, because they were never queried. The easiest way to resolve this is a complete restructuring of your code.

As a related error, if you want "starts with", you need to use a wildcard, as in LIKE '10%'. That was probably part of your problem as well. Also, the entire code was a lot more verbose than it needed to be.

First, we build a map of opportunities we want to update, then we query for those records that meet our criteria, finish up checking the appropriate boxes, and perform an update. Here's what that would look like:

Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
for(P_ODR__c record: triggerNew) {
  opps.put(record.Opportunity__c, new Opportunity(Id=record.Opportunity__c, Nd_Check__c=true));
}
for(AggregateResult result: [SELECT Opportunity__c Id FROM P_ODR__c 
     WHERE Opportunity__c = :opps.keySet() AND NOT TMMI__c LIKE '10%' GROUP BY Opportunity__c]) {
  opps.get((Id)result.get('Id')).Nd_Check__c = false;
}
update opps.values();
| improve this answer | |
  • @Sdfcfox.. Simple and Awesome approach. Thanks you much!! I changed my logic as you suggested and it worked perfectly and better performance. One question anything needed to stop recursive because existing method has boolean QueryrunOnce. – Sfdc_1184 Nov 13 '18 at 14:44
  • @ArvindSingh Unless you have an opportunity trigger updating those records, there's not much else you need to do. – sfdcfox Nov 13 '18 at 16:39
  • I just checked and i can see opportunity trigger also update those record based on some other condition. do i need to use any static variable ? – Sfdc_1184 Nov 13 '18 at 17:52
  • @ArvindSingh If you're experiencing an infinite loop, then maybe, otherwise don't worry about it. All things being equal, a better solution than a static variable is to query the opportunities involved and see if they need to be updated based on their current status (e.g. if the opportunity is already unchecked, and the new value is unchecked, do not perform an update). – sfdcfox Nov 13 '18 at 17:59
  • I have a question if you can help me. Logic suggested by you is working fine for me but i need to add one more condition for Sub_segment__c to query so my loop will be like for(AggregateResult result: [SELECT Opportunity__c Id FROM P_ODR__c WHERE Opportunity__c = :opps.keySet() AND Opportunity__r.Sub_Segment__c != 'MM' AND (NOT TMMI__c LIKE '10%') GROUP BY Opportunity__c]) { opps.get((Id)result.get('Id')).Nd_Check__c = false; } If i do like this will it work fine and what about performance ? Please advise. – Sfdc_1184 Apr 19 '19 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.