function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nihar SharmaNihar Sharma 

isAfter is not working properly in apex trigger

I have built following apex trigger code and isAfter code is not executing because I have used recursive method in this trigger.

Please have a look into this and let me know what i have did wrong in this code :
 
trigger CreateOrderForOpportunity on Opportunity (before insert, after insert, after update) {
    
    if(trigger.isInsert){
        
        Map<ID,Schema.RecordTypeInfo> rt_Map = Opportunity.sObjectType.getDescribe().getRecordTypeInfosById();
        List<SBQQ__Quote__c> quotelist = new List<SBQQ__Quote__c>();
        List<SBQQ__QuoteLine__c> quoteLineList = new  List<SBQQ__QuoteLine__c>();
        List<SBQQ__QuoteLine__c> quoteLineDataServiceList = new  List<SBQQ__QuoteLine__c>();
        
        for(Opportunity opp : trigger.New){
            
            if(rt_map.get(opp.recordTypeID).getName().contains('CONECTIVIDAD') && opp.Coming_from_Portal__c == True){
                
                if(trigger.isBefore){
                    if(opp.Owner_Email_Address_coming_from_portal__c != Null){
                        List<user> uList = [select Id, Email from user where Email =: opp.Owner_Email_Address_coming_from_portal__c Limit 1];
                        if(uList.size() > 0)
                            for(User u : uList){
                                opp.OwnerId = u.Id;
                            }
                    }
                    if(opp.Portal_Account_Name__c != Null){
                        List<Account> AccList = [select Id, Name from Account where Name =: opp.Portal_Account_Name__c Limit 10000];
                        if(AccList.size() > 0){
                            for(Account a : AccList){
                                opp.AccountId = a.Id;
                            }
                        }
                    }
                }
            }
        }
        if(checkRecursive.checkOneTime()){
            for(opportunity oppAfter : trigger.New){
                if(rt_map.get(oppAfter.recordTypeID).getName().contains('CONECTIVIDAD') && oppAfter.Coming_from_Portal__c == True){
                    if(trigger.isAfter){
                        system.debug('--isAfter->>>>');
                        SBQQ__Quote__c quote = new SBQQ__Quote__c();
                        quote.SBQQ__Account__c = oppAfter.AccountId;
                        quote.Quote_Type__c = 'Draft';
                        quote.SBQQ__Opportunity2__c = oppAfter.Id;
                        quote.Requerimientos_del_cliente__c = 'PORTAL';
                        quote.SBQQ__PriceBook__c = '01s70000000JxjL';
                        quote.Primary_Contact__c = [Select Id from Contact where AccountId =: oppAfter.AccountId Order By createdDate Limit 1].Id;
                        if(oppAfter.PlazoContratacion__c != Null){
                            quote.SBQQ__SubscriptionTerm__c = oppAfter.PlazoContratacion__c;
                        }
                        quotelist.add(quote);
                        
                        if(oppAfter.Quote_line_Product_Name__c != Null){
                            for(Product2 p : [select Id, Name from Product2 where Name =: oppAfter.Quote_line_Product_Name__c]){
                                SBQQ__QuoteLine__c qli = new SBQQ__QuoteLine__c();
                                qli.SBQQ__Product__c = p.Id;
                                if(oppAfter.Product_Special_Price_coming_from_portal__c != Null){
                                    qli.SBQQ__SpecialPrice__c = oppAfter.Product_Special_Price_coming_from_portal__c;
                                }
                                qli.SBQQ__Quote__c = quote.Id;
                                qli.SBQQ__SpecialPriceType__c = 'Custom';
                                if(oppAfter.Quote_line_TipoClearChanel_Velocidad__c != Null){
                                    qli.TipoClearChanel__c = String.ValueOf(oppAfter.Quote_line_TipoClearChanel_Velocidad__c);    
                                }
                                quoteLineList.add(qli);
                            }
                            
                        }
                        SBQQ__QuoteLine__c qliNew = new SBQQ__QuoteLine__c();
                        qliNew.SBQQ__Product__c = '01t7000000Wt7xj';
                        qliNew.SBQQ__Quote__c = quote.Id;
                        qliNew.SBQQ__SpecialPriceType__c = 'Custom';
                        quoteLineDataServiceList.add(qliNew);
                    }
                }
            }
        }
        insert quotelist;
        insert quoteLineList;
        insert quoteLineDataServiceList;
    }
    if(trigger.isUpdate){
        .
		.
		.
    }
}

Please guys help me out !!
Jefferson FernandezJefferson Fernandez
Hi Nihar,
There are red flags on your code and we want to point them out to avoid potential breakage in your code in the future so you may want to refactor it as early as now. One rule is never put SOQL queries inside for loops. Also, you may want to create a class handler for the process and not code them all at once on your trigger. Please read the apex best practices here (https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html). This may not answer your question directly but I want to give you the methods you will need moving forward.
Thanks,
Nihar SharmaNihar Sharma
Thank you Jeff & Malni !!

I have bulkified my code and it's working properly. :)

Regards,
Nihar