0

I have a method in trigger handler class but method is taking more CPU time and sometime give CPU time error. I have to set the the AMM_OPP_Check__c to true based on related record when all the related record field TTPI__C from Summury_Flex__Jo__r start with '17' then will set the AMM_OPP_Check__c and toggle to true else false. Any record do not start with '17' then set false.

Anyone Please suggested better approach of doing this. Thanks in Advance!!

Public void updatecheckandtoggle(List<AMM_ODR__REL__C> ListRec) 
{
Try{
    Set<Id> OppIds = new set<id> ();
    List<AMM_Opportunity__c> ListAMMOpptoUpdate;
    If (Trigger.isInsert ||Trigger.isUpdate ) 
    {
        for (AMM_ODR__REL__C Odr : ListRec )
        { 
        OppIds.add(Odr.AMM_Opportunity__c);
        }

    }
    if(OppIds.size() > 0 && QueryrunOnce ) 
    {
    QueryrunOnce = false;
        ListAMMOpptoUpdate = New List<AMM_Opportunity__c> ([Select Id,AMM_OPP_Check__c,(Select TTPI__C from Summury_Flex__Jo__r) from AMM_Opportunity__c where id:=OppIds];
    }
    If(!ListAMMOpptoUpdate.isEmpty()) 
    {
        for (AMM_Opportunity__c Opps: ListAMMOpptoUpdate ) 
        {
          If (Opps.Summury_Flex__Jo__r.size()>0)
           {
             for (AMM_ODR__REL__C Odrr : Opps.Summury_Flex__Jo__r) 
                {
                  if(Odrr!null && odrr.TTPI__C!=null && Odrr.TTPI__C.startwith('17') )
                 {
                   Opps.AMM_OPP_Check__c = true;
                   Opps.AMM_OPP_toggle__c=true;
                 } else 
                 {
                   Opps.AMM_OPP_Check__c = true;
                   Opps.AMM_OPP_toggle__c=true;
                 } 
                }
              } 
            } Update ListAMMOpptoUpdate;
      }
      } Catch (Exception ex) {system.debug('The Exception Occur - ')}
  • Your trigger effectively does nothing, and wastes a lot of time doing nothing. You need to fix your trigger's logic first. – sfdcfox Nov 20 '18 at 3:20
  • Also you have violated one of the most principal rules in all of programming. Never, ever, use an empty catch block. And debug statements absolutely do not count as non-empty. – Adrian Larson Nov 20 '18 at 3:59
  • @sfdcfox & Adrian - Thanks. I will try to change method !! I do not have much exp working on apex. Trying my best to learn and implement better approach. – Sfdc_1184 Nov 20 '18 at 4:28
  • 1
    You will get better help here if you explain what you are trying to achieve rather than just dumping code into your post and expecting us to figure it out. Try to edit this post to explain using words what your requirements are, being as specific as possible. – Adrian Larson Nov 20 '18 at 14:52
  • @AdrianLarson Thanks!! I have updated my comment section. I had something similar issue before and sfdcfox suggested approach in another post it work fine. But i am not able to implement same for this. So i was looking if we can do using map or anyother better way. If you have any sample pls share. – Sfdc_1184 Nov 21 '18 at 13:36
1

I dont think my below code will work if just copy and paste it, but I hope it gives you some direction, (direction I didnt have when started ;) )

As a best practice is to have 1 trigger per object so just in case your trigger should looks like:

trigger mytriggerNameHere on AMM_ODR__REL__C (after insert, after update, after delete, before insert, before delete, before delete) {
    if (trigger.isAfter) {
        if (trigger.isInsert || trigger.isUpdate) {
            mytriggerNameHere_hdl.anActionName(trigger.new)
        }
    }
}

then your handler should be looking like this:

public with sharing class mytriggerNameHere_hdl {
    public static void anActionName(List<AMM_ODR__REL__C> ammLIST){
        Set<Id> OppIds = new set<id> ();
        for (AMM_ODR__REL__C: a:ammLIST) {
            OppIds.add(a.AMM_Opportunity__c);
        }

        if (OppIds.isEmpty()) {
            return;
        }

        //I DONT UNDERSTAND WHERE YOU ARE SETTING QueryrunOnce? 
        //I WILL CHANGE YOUR := to IN : ...
        List<AMM_Opportunity__c> ListAMMOpptoUpdate = new List<AMM_Opportunity__c> ([SELECT Id, AMM_OPP_Check__c, (SELECT TTPI__C FROM Summury_Flex__Jo__r) FROM AMM_Opportunity__c WHERE id IN : OppIds]; 
        if (ListAMMOpptoUpdate.isEmpty()) {
            return;
        }

        //THIS IS COPIED AND PASTE FROM YOUR CODE I THINK YOU HAVE A COUPLE OF THINGS THAT ARE WRONG
        for (AMM_Opportunity__c Opps : ListAMMOpptoUpdate) {
            if (Opps.Summury_Flex__Jo__r.size()>0)) {
                for (AMM_ODR__REL__C Odrr : Opps.Summury_Flex__Jo__r) {                    
                    if(odrr.TTPI__C!=null && Odrr.TTPI__C.startwith('17') ) {
                        Opps.AMM_OPP_Check__c = true;
                        Opps.AMM_OPP_toggle__c = true;
                    } else {
                        Opps.AMM_OPP_Check__c = false;
                        Opps.AMM_OPP_toggle__c = false;
                    } 
                }
            }
        }

        //BELOW SOME IDEAS OF HOW TO HANDLE AN ERROR
        try {
            //Savepoint sp = Database.setSavepoint();
            update ListAMMOpptoUpdate; 
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
            //Database.rollback(sp);
            return;
        }

    }
}

I hope it helps

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.