• Kelsey Shannon
  • NEWBIE
  • 30 Points
  • Member since 2018
  • CRM Systems Development Manager
  • Honeywell


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 5
    Replies
Hello - I'm struggling to get the proper coverage with my invocable method that I'm using in a visual flow, specifically the lines inside the for loop are being listed as not covered. How would I test those? 

Class:
public class Invocable_eG_ProductSBUSearch
{
    @InvocableMethod(label='Find Valid Approvers' description ='Searchs the database for matches in SEA Approvers')
    //productSBUSearch comes from the flow and contains the eGreensheet ID and Tier selected concatenated. Example = 'a1s2D0000005DQAQA2,Tier II'
    public static List<List<Approver__c>> approverIds (List<String> productSBUSearch){
        
        // One sObject per search eGreensheet record (bulkify)
        List<List<SObject>> wrapsApprovers = new List<List<SObject>>();
        for (String searchTerm : productSBUSearch) {
            wrapsApprovers.add(new List<SObject>());
            }
        
        //Split out productSBUSearch into tier and eG ID
        String[] VarIds = productSBUSearch[0].split(',');
        
        //Pull eGreensheet Product SBU into a list
        List<Workflow_Details__c> eGs = [SELECT Product_SBU__c FROM Workflow_Details__c where Id in :VarIds];
        system.debug('eGs list has ' + eGs.size() + ' values returned');
        system.debug(database.query('select id,Product_SBU__c from workflow_details__c where Id in :VarIds'));
                     
        //Pull all possible approvers
        
        List<Approver__c> possibleApprovers = [SELECT Id, Product_SBU__c, Tier__c, Approver_Name__c, Name FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :VarIds ORDER BY Approver_Name__r.LastName];
        system.debug('List of approvers:' + possibleApprovers);
                
        //Add all approvers who match on Product SBU to the final list
        for (Approver__c approver : possibleApprovers) {
            //Query for matches
            system.debug(approver.Product_SBU__c);
            system.debug(eGs[0].Product_SBU__c);
           if(approver.Product_SBU__c!=null && eGs[0].Product_SBU__c!=null && eGs[0].Product_SBU__c.contains(approver.Product_SBU__c)){
                //Return fields to the flow collection
                 wrapsApprovers[0].add(approver);
                 System.debug(approver);
                }
        }
        return wrapsApprovers; 
 }
}
Test:
@isTest 
private class Invocable_eG_ProductSBUSearchTest {
    static testMethod void approverIdsTest(){
      //Create Users for the SEA Approval Matrix
       Profile pf = [select id from profile where name='eGreensheet Approver'];    
       User u = new User();
           u.LastName = 'Approver Test User';
           u.Username = 'egreensheetApprover@test.com.honeywell';
           u.Email = 'Approver@test.com';
           u.Alias = 'appuser' ;
           u.CommunityNickname= 'approveruser';
           u.TimeZoneSidKey = 'America/Los_Angeles';
           u.LocaleSidKey='en_US';
           u.EmailEncodingKey= 'ISO-8859-1';
           u.ProfileId = pf.Id;
           u.LanguageLocaleKey = 'en_US';
           u.Functional_Role__c='testrole';
           u.SBU_User__c='testsbu';
           u.CBT__c='HTSI';
           u.CBT_Team__c ='testcbtteam';
       insert u; 
        
       User u2 = new User();
           u2.LastName = 'Approver Test User 2';
           u2.Username = 'egreensheetApprover2@test.com.honeywell';
           u2.Email = 'Approver2@test.com';
           u2.Alias = 'appuser2' ;
           u2.CommunityNickname= 'approveruser2';
           u2.TimeZoneSidKey = 'America/Los_Angeles';
           u2.LocaleSidKey='en_US';
           u2.EmailEncodingKey= 'ISO-8859-1';
           u2.ProfileId = pf.Id;
           u2.LanguageLocaleKey = 'en_US';
           u2.Functional_Role__c='testrole';
           u2.SBU_User__c='testsbu';
           u2.CBT__c='HTSI';
           u2.CBT_Team__c ='testcbtteam';
       insert u2; 
        
      //Add approvers to SEA Approval Matrix where Type = Product Approver
      Approver__c a = new Approver__c();
        a.Name = 'Approver Test user';
        a.Approver_Name__c = u.Id;
        a.Approver_type__c = 'Product Approver';
        a.Tier__c = 'Tier II';
        string productSBU = 'Electronic Solutions';
        a.Product_SBU__c = productSBU;
       insert a;
        
      Approver__c a2 = new Approver__c();
        a2.Name = 'Approver Test user 2';
        a2.Approver_Name__c = u2.Id;
        a2.Approver_type__c = 'Product Approver';
        a2.Tier__c = 'Tier II';
        string productSBU2 = 'Electronic Solutions; Engines & Power Systems';
        a.Product_SBU__c = productSBU2;
       insert a2;
        
      //Add eGreensheet record test 1
      Workflow_details__c eg = new Workflow_details__c();
        string productSBUeg = 'Electronic Solutions';
        eg.Product_SBU__c = productSBUeg;
        eg.Sales_Channel__c = 'Americas AM';
        eg.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg.Approval_Level_Required__c = 'Tier II';
        eg.Sales_K__c = 0;
        eg.X5_Year_Revenue_K__c = 0;
        eg.Terms_and_Conditions__c = 'Standard';
        eg.Front_End_Loss__c = 0;
        eg.Exposed_Cost__c = 0;
        eg.Documentum__c = 'test.com';
      insert eg;
        
      //Send through eGreensheet record into class
        String productSBUSearchString = eg.Id+','+eg.Approval_Level_Required__c;
     	List<String> productSBUSearch = new List<String>();
		productSBUSearch.add(productSBUSearchString);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch);
        system.debug(productSBUSearch);
        
        
      //Add eGreensheet record test 2
      Workflow_details__c eg2 = new Workflow_details__c();
        string productSBUeg2 = 'Other Aero';
        eg2.Product_SBU__c = productSBUeg2;
        eg2.Sales_Channel__c = 'Americas AM';
        eg2.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg2.Approval_Level_Required__c = 'Tier II';
        eg2.Sales_K__c = 0;
        eg2.X5_Year_Revenue_K__c = 0;
        eg2.Terms_and_Conditions__c = 'Standard';
        eg2.Front_End_Loss__c = 0;
        eg2.Exposed_Cost__c = 0;
        eg2.Documentum__c = 'test.com';
      insert eg2;
       
         //Send through invalid eGreensheet record
        String productSBUSearchString2 = eg2.Id+','+eg2.Approval_Level_Required__c;
     	List<String> productSBUSearch2 = new List<String>();
		productSBUSearch2.add(productSBUSearchString2);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch2);
                     
    }                                                               
}

 
I'm struggling with finishing off this piece of code, anyone have a suggestion? Its returning an error at line 22: invalid loop variable type expected Approver__c was List
public class Invocable_eG_ProductSBUSearch
{
	@InvocableMethod(label='Find Valid Approvers' description ='Searchs the database for matches in SEA Approvers')
    //productSBUSearch comes from the flow and contains the eGreensheet ID and Tier selected concatenated
    public static List<List<Approver__c>> approverIds (List<String> productSBUSearch){
      	
 		// One sObject per search eGreensheet record (bulkify)
		List<List<SObject>> wrapsApprovers = new List<List<SObject>>();
		for (String searchTerm : productSBUSearch) {
			wrapsApprovers.add(new List<SObject>());
			}
        
        //Pull eGreensheet Product SBU into a list
        List<Workflow_Details__c> eGs = [SELECT Product_SBU_List__c FROM Workflow_Details__c where Id in :productSBUSearch];
        
        //Pull all possible approvers
        List<Approver__c> possibleApprovers = [SELECT Product_SBU__c, Tier__c FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :productSBUSearch];
        system.debug('wwwwwwwwwwwww' + possibleApprovers);
        
		//Add all approvers who qualify to the final list
		//WFList[0].Product_SBU__c.contains(Alist[j].Product_SBU__c)
		for (List<SObject> listOfApprovers : possibleApprovers){
			for (SObject returnList : listOfApprovers) {
            //Query for matches  
                if(eGs[0].Product_SBU_List__c.contains(possibleApprovers[0].Product_SBU__c)){
			//Return fields to the flow
			wrapsApprovers[0].add(returnList);
                }
			}
		}        
        return wrapsApprovers;   
 }
}
Hello - Newbie developer here and we have a trigger written a few years ago that has started to throw Apex CPU errors. I read through the articles on how to make them more efficient but I'm stuck on this one, any suggestions?
trigger OpportunityProduct_UpdateTechSales on OpportunityLineItem (before insert,before update, after update,after delete) {
    list<id> prodlineids = new list<id>(); list<id> oppids = new list<id>();set<id> plcrids = new set<id>();set<id> plcrids1 = new set<id>();
    transient  list<Product_Line_Tech_Sales__c> lstplts1 = new list<Product_Line_Tech_Sales__c>();
    list<Opportunity> lstopp = new list<Opportunity>();
    map<id,Product_Line_Cross_Ref__c> mapplcr; map<id,Product_Line_Cross_Ref__c> mapplcr1;
    list<string> usernames = new list<string>();
    map<string,id> usermap = new map<string,id>(); map<id,Opportunity> oppmap;
    list<Opportunity_Sales_Team__c> lstoppteam= new list<Opportunity_Sales_Team__c>();
    map<id,id> dupmap = new map<id,id>();list<user> userlst = new list<user>();
    set<id> contids = new set<id>(); Boolean op = true;
    if( TriggerInactive.TestOpportunityProduct_UpdateTechSales == True){
        if(trigger.isinsert){ 
            for(integer i=0;i<trigger.new.size();i++){
            oppids.add(trigger.new[i].Opportunityid);
                if(trigger.new[i].M_PM_Product__c!= null){plcrids.add(trigger.new[i].M_PM_Product__c);
                 system.debug('@@plcridsinsert'+plcrids);
                }    
            }
        }  
        if(Trigger.isupdate){
            for(integer i=0;i<trigger.new.size();i++){
                oppids.add(trigger.new[i].Opportunityid);
                if(trigger.new[i].M_PM_Product__c!=trigger.old[i].M_PM_Product__c){
                    plcrids.add(trigger.new[i].M_PM_Product__c);plcrids1.add(trigger.old[i].M_PM_Product__c);
                 system.debug('@@plcridsupdate'+plcrids);
                  system.debug('**plcrids1update'+plcrids1);
                }
            }
        }
        if(Trigger.isdelete){
            for(integer i=0;i<trigger.old.size();i++){
                plcrids1.add(trigger.old[i].M_PM_Product__c);oppids.add(trigger.old[i].Opportunityid);
                system.debug('**plcrids1update'+plcrids1);
                system.debug('$$oppidsupdate'+oppids);
            }
        }
        if(oppids.size()>0 ){
            oppmap = new map<id,Opportunity>([select id,Tech_Sales__c,Tech_Sales_Product_Area__c, Tech_Sales1__c, Tech_Sales2__c, Default_Tech_Sales__c,Default_Tech_Sales_Product__c,Default_Tech_Sales_Manager__c,Default_Tech_Sales_Manager__r.Name,Default_Tech_Sales_Manager_Secondary__c,Default_Tech_Sales_Manager_Secondary__r.Name,(select id,Contact__c from Opportunity.Opportunity_Sales_Teams__r),(select id,M_PM_Product__c,M_PM_Product__r.Product_Line_Chief_Engineer__c, M_PM_Product__r.Product_Leader__c, M_PM_Product__r.Product_Line__r.Product_Line_Finance_POC__c from OpportunityLineItems ) from Opportunity where id=:oppids]);
            if( Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore )){
                if(!Test.isRunningTest()) { 
                    lstplts1 = [select id,CBT__c,SBU__c,Region__c,Country__c,Tech_Sales__c,Tech_Sales_Product__c,Product_Type__c,Tech_Sales_Manager__r.Name,Tech_Sales_Manager__c,Tech_Sales_Manager_Secondary__r.Name,Tech_Sales_Manager_Secondary__c,Product_Line__r.Name,Product_Line__c from Product_Line_Tech_Sales__c Limit 20000];
                system.debug('%%lstplts1'+lstplts1);
                }
                else{
                               
                lstplts1 = [select id,CBT__c,SBU__c,Region__c,Country__c,Tech_Sales__c,Tech_Sales_Product__c,Product_Type__c,Tech_Sales_Manager__r.Name,Tech_Sales_Manager__c,Tech_Sales_Manager_Secondary__r.Name,Tech_Sales_Manager_Secondary__c,Product_Line__r.Name,Product_Line__c from Product_Line_Tech_Sales__c limit 10];
                system.debug('%%lstplts1else'+lstplts1);
                }       
            }
        }  
        if(plcrids.size()>0 && (Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore ))){
            mapplcr = new map<id,Product_Line_Cross_Ref__c>([select id,Product_Line__c,Product_Line__r.Product_Line_Finance_POC__c,Product_Leader__c,Product_Line_Chief_Engineer__c from Product_Line_Cross_Ref__c where id=:plcrids]);
            system.debug('##mapplcr'+mapplcr);
        }
        if(plcrids1.size()>0 && (Trigger.isdelete || ( Trigger.isupdate && Trigger.isAfter ))){
            mapplcr1 = new map<id,Product_Line_Cross_Ref__c>([select id,Product_Line__c,Product_Line__r.Product_Line_Finance_POC__c,Product_Leader__c,Product_Line_Chief_Engineer__c from Product_Line_Cross_Ref__c where id=:plcrids1]);
            system.debug('##mapplcr1'+mapplcr1);    
        }  
        for(Opportunity opp:oppmap.values()){
            usernames.add(opp.Tech_Sales1__c);usernames.add(opp.Tech_Sales2__c);
        }
        if(usernames.size()>0){
            userlst = [SELECT Id,Name FROM User WHERE Name =: usernames AND IsPortalEnabled = false];
        for(user u :userlst ){usermap.put(u.name,u.id);}
        }  
        if( Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore )){
            for(OpportunityLineItem oplt:trigger.new){
                op = true;
                system.debug('##op'+op);
                if(oppmap.containsKey(oplt.Opportunityid) && (oppmap.get(oplt.Opportunityid).Default_Tech_Sales__c!=null || oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Product__c!=null)){
                    oplt.Tech_Sales__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales__c;
                    oplt.Tech_Sales_Product__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Product__c;
                    oplt.Tech_Sales_Manager__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Manager__c;
                    oplt.Tech_Sales_Manager_Secondary__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Manager_Secondary__c;oplt.OpportunityAccountChange__c = false;
                }
                else{
                    if(op == true){
                    system.debug('&&Op'+op);
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                        
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }
                    }
                    if(op == true){
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c; 
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false; 
                            } 
                        }//lstplts1.clear();
                    }
                    if(op == true){
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }// lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c; 
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//stplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c; 
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false;   
                                op = false;
                            }
                        } //lstplts1.clear();
                    }
                    if(op == true){ 
                        
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                           // system.debug('oplt value:'+oplt);
                            //system.debug('plts value:'+plts);
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c; 
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                           if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false;   
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){
                        oplt.Tech_Sales__c = oppmap.get(oplt.Opportunityid).Tech_Sales__c;
                        oplt.Tech_Sales_Product__c = oppmap.get(oplt.Opportunityid).Tech_Sales_Product_Area__c;
                        oplt.OpportunityAccountChange__c = false;
                        oplt.Tech_Sales_Manager__c =  usermap.get(oppmap.get(oplt.Opportunityid).Tech_Sales1__c);
                        oplt.Tech_Sales_Manager_Secondary__c = usermap.get(oppmap.get(oplt.Opportunityid).Tech_Sales2__c);
                    }
                }
                map<id,id> contmap = new map<id,id>();
                /**for(Opportunity_Sales_Team__c oppsateam :oppmap.get(oplt.Opportunityid).Opportunity_Sales_Teams__r){
                    contmap.put(oppsateam.Contact__c,oppsateam.Contact__c);
                }  
                system.debug('@@mapplcr'+mapplcr);
                if(mapplcr!=null && !mapplcr.isempty()){
                    system.debug('Line187'+mapplcr);
                    system.debug('Line231'+(oplt.M_PM_Product__c));
                    system.debug('Line189'+contmap);
                    system.debug('!!mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c);
                    system.debug('##mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c);
                    system.debug('$$mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c);
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c;
                            oppteam.Opportunity__c = oplt.Opportunityid; 
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c,oplt.Opportunityid);lstoppteam.add(oppteam);
                        }
                    }
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c;
                            oppteam.Opportunity__c = oplt.Opportunityid; 
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c,oplt.Opportunityid); lstoppteam.add(oppteam);
                        }
                    }           
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c;
                            oppteam.Opportunity__c = oplt.Opportunityid;
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c,oplt.Opportunityid); lstoppteam.add(oppteam);
                        }
                    }
                }**/
            }
        }
        if(lstoppteam.size()>0){ insert lstoppteam; }
        system.debug('**mapplcr1'+mapplcr1);
        if((Trigger.isdelete && mapplcr1!=null && !mapplcr1.isempty() ) || (mapplcr1!=null && !mapplcr1.isempty() && Trigger.isupdate && Trigger.isAfter  )){
            set<id> setids = new set<id>(); list<Opportunity_Sales_Team__c> oppsalteamlst = new list<Opportunity_Sales_Team__c>();
            for(OpportunityLineItem oplt:trigger.old){
                map<id,Opportunity_Sales_Team__c> contmap1 = new map<id,Opportunity_Sales_Team__c>();
                for(Opportunity_Sales_Team__c oppsateam :oppmap.get(oplt.Opportunityid).Opportunity_Sales_Teams__r){
                    contmap1.put(oppsateam.Contact__c,oppsateam);
                } 
                for(OpportunityLineItem oplt2:oppmap.get(oplt.Opportunityid).OpportunityLineItems){
                    setids.add(oplt2.M_PM_Product__r.Product_Leader__c);
                    setids.add(oplt2.M_PM_Product__r.Product_Line_Chief_Engineer__c);
                    setids.add(oplt2.M_PM_Product__r.Product_Line__r.Product_Line_Finance_POC__c);
                } 
                    /*system.debug('Line230'+mapplcr1);
                    system.debug('Line231'+(oplt.M_PM_Product__c));
                    system.debug('Line232'+setids);
                    system.debug('Line233'+contmap1);
                    system.debug('!!mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c);
                    system.debug('##mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c);
                    system.debug('$$mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c);
                    */
                if(oplt.M_PM_Product__c !=null && mapplcr1.containsKey(oplt.M_PM_Product__c)){
                    if(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c!=null){
                        if(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c));
                        }
                        if(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c));
                        }
                        if( mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c));              
                        }
                    }
                }
            }
            if(oppsalteamlst.size()>0){delete oppsalteamlst;}      
        } 
    }
}
Can anyone recommend videos/articles/best practices for cleaning up old code in an org?

I inherited a messy org - 10 yrs old, 20+ triggers per object, hundreds of workflows/declarative items, and we are constantly hitting governor limits. Our code coverage is also below 75% now preventing us from deploying anything new...

My management team has granted us a 4 month new code/new project freeze to get this fixed and I don't want to waste it. 
Hello - I inherited an older organization and am a newbie developer. I would appreciate any advice you could give me!

We have a trigger that runs whenever a Opportunity Team Member is added or removed from an Opportunity and updates a custom field on the Opportunity populated with the name and employee ID of each team member. The end result could look something like this: "Shannon E334355, DeSanti E445466, Crosman E244562".

However, if we run data loader to insert or update team members with large batch sizes the trigger adds the names of team members for users on different opportunities. For example, if Opp A has User1 and User2 on the team, and Opp B had User3 and User4 on the team, after doing a bulk update that touches both Opps each will have the following text on the custom field: "User1, User2, User3, User4". 

Is there a best practice or way to prevent this from happening? Currently we are making sure to run in batches of 1 (which takes forever!).

Here is the actual code if that helps (and yes whoever wrote this added the code into the trigger rather than a separate class that the trigger calls...boo):
 
trigger OpportunityTeamMembers on Opportunity_Sales_Team__c (after delete,  after insert,after update) {

List<Id> oppList= new List<Id>();
List<Opportunity> oppListUpdate= new List<Opportunity>();
List<Opportunity_Sales_Team__c> oppTeamList= new List<Opportunity_Sales_Team__c>();
String teamName;
String tmName;

if (trigger.isInsert || trigger.isUpdate)
{
 for (Opportunity_Sales_Team__c oppTeamMember: Trigger.new)
    {
      if(trigger.isUpdate){
       //if(System.Trigger.oldMap.get(oppTeamMember.Id).User__c!=System.Trigger.NewMap.get(oppTeamMember.Id).User__c)
           if(oppTeamMember.User__c != null)
          oppList.add(oppTeamMember.Opportunity__c);
       }
      if(trigger.isInsert){
       if(oppTeamMember.User__c != NULL)
          oppList.add(oppTeamMember.Opportunity__c);
       }
   }   
}

if (Trigger.isDelete)
{  
    for (Opportunity_Sales_Team__c oppTeamMembers: Trigger.old)    {
          oppList.add(oppTeamMembers.Opportunity__c);  
           
    }
}   

if(oppList.size()>0){
   oppListUpdate=[Select Id,Programme_Manager__c from Opportunity where Id in :oppList];
   try{
   
   oppTeamList=[Select Id,User__r.User_EID__c,Team_Member_Full_Name__c,Opportunity__c, User__r.Name, User__c  from Opportunity_Sales_Team__c  where Opportunity__c in
                  :oppList order by createddate desc];
   }
   catch(Exception e){}              
   }
     try {
     for (integer i=0;i<oppListUpdate.size();i++){
     teamName='';
     tmName='';
        for(integer j=0;j<oppTeamList.size();j++){
        
          if(oppTeamList[j].User__c != NULL || oppTeamList[j].User__c != ''){
          if(oppTeamList[j].User__r.Name != NULL || oppTeamList[j].User__r.Name != ''){
              if(oppTeamList[j].User__r.Name !=null){
                  teamName = teamName + ',' + oppTeamList[j].User__r.Name.substring(oppTeamList[j].User__r.Name.lastIndexOf(' '))+' '+oppTeamList[j].User__r.User_EID__c;
              }
          }
          }
        }
        tmName = teamName.substring(teamName.indexOf(',')+1);

        oppListUpdate[i].TeamMemberLastName__c=tmName;
        
     
     }
     }catch(Exception ex){
        system.debug('Exception at 60:'+ex.getMessage());
     }
     try {
     if(oppListUpdate.size()>0)
          update oppListUpdate;
     }catch(Exception ex){
        system.debug('Exception at 66:'+ex.getMessage());
     }
}

 
Hello - I'm struggling to get the proper coverage with my invocable method that I'm using in a visual flow, specifically the lines inside the for loop are being listed as not covered. How would I test those? 

Class:
public class Invocable_eG_ProductSBUSearch
{
    @InvocableMethod(label='Find Valid Approvers' description ='Searchs the database for matches in SEA Approvers')
    //productSBUSearch comes from the flow and contains the eGreensheet ID and Tier selected concatenated. Example = 'a1s2D0000005DQAQA2,Tier II'
    public static List<List<Approver__c>> approverIds (List<String> productSBUSearch){
        
        // One sObject per search eGreensheet record (bulkify)
        List<List<SObject>> wrapsApprovers = new List<List<SObject>>();
        for (String searchTerm : productSBUSearch) {
            wrapsApprovers.add(new List<SObject>());
            }
        
        //Split out productSBUSearch into tier and eG ID
        String[] VarIds = productSBUSearch[0].split(',');
        
        //Pull eGreensheet Product SBU into a list
        List<Workflow_Details__c> eGs = [SELECT Product_SBU__c FROM Workflow_Details__c where Id in :VarIds];
        system.debug('eGs list has ' + eGs.size() + ' values returned');
        system.debug(database.query('select id,Product_SBU__c from workflow_details__c where Id in :VarIds'));
                     
        //Pull all possible approvers
        
        List<Approver__c> possibleApprovers = [SELECT Id, Product_SBU__c, Tier__c, Approver_Name__c, Name FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :VarIds ORDER BY Approver_Name__r.LastName];
        system.debug('List of approvers:' + possibleApprovers);
                
        //Add all approvers who match on Product SBU to the final list
        for (Approver__c approver : possibleApprovers) {
            //Query for matches
            system.debug(approver.Product_SBU__c);
            system.debug(eGs[0].Product_SBU__c);
           if(approver.Product_SBU__c!=null && eGs[0].Product_SBU__c!=null && eGs[0].Product_SBU__c.contains(approver.Product_SBU__c)){
                //Return fields to the flow collection
                 wrapsApprovers[0].add(approver);
                 System.debug(approver);
                }
        }
        return wrapsApprovers; 
 }
}
Test:
@isTest 
private class Invocable_eG_ProductSBUSearchTest {
    static testMethod void approverIdsTest(){
      //Create Users for the SEA Approval Matrix
       Profile pf = [select id from profile where name='eGreensheet Approver'];    
       User u = new User();
           u.LastName = 'Approver Test User';
           u.Username = 'egreensheetApprover@test.com.honeywell';
           u.Email = 'Approver@test.com';
           u.Alias = 'appuser' ;
           u.CommunityNickname= 'approveruser';
           u.TimeZoneSidKey = 'America/Los_Angeles';
           u.LocaleSidKey='en_US';
           u.EmailEncodingKey= 'ISO-8859-1';
           u.ProfileId = pf.Id;
           u.LanguageLocaleKey = 'en_US';
           u.Functional_Role__c='testrole';
           u.SBU_User__c='testsbu';
           u.CBT__c='HTSI';
           u.CBT_Team__c ='testcbtteam';
       insert u; 
        
       User u2 = new User();
           u2.LastName = 'Approver Test User 2';
           u2.Username = 'egreensheetApprover2@test.com.honeywell';
           u2.Email = 'Approver2@test.com';
           u2.Alias = 'appuser2' ;
           u2.CommunityNickname= 'approveruser2';
           u2.TimeZoneSidKey = 'America/Los_Angeles';
           u2.LocaleSidKey='en_US';
           u2.EmailEncodingKey= 'ISO-8859-1';
           u2.ProfileId = pf.Id;
           u2.LanguageLocaleKey = 'en_US';
           u2.Functional_Role__c='testrole';
           u2.SBU_User__c='testsbu';
           u2.CBT__c='HTSI';
           u2.CBT_Team__c ='testcbtteam';
       insert u2; 
        
      //Add approvers to SEA Approval Matrix where Type = Product Approver
      Approver__c a = new Approver__c();
        a.Name = 'Approver Test user';
        a.Approver_Name__c = u.Id;
        a.Approver_type__c = 'Product Approver';
        a.Tier__c = 'Tier II';
        string productSBU = 'Electronic Solutions';
        a.Product_SBU__c = productSBU;
       insert a;
        
      Approver__c a2 = new Approver__c();
        a2.Name = 'Approver Test user 2';
        a2.Approver_Name__c = u2.Id;
        a2.Approver_type__c = 'Product Approver';
        a2.Tier__c = 'Tier II';
        string productSBU2 = 'Electronic Solutions; Engines & Power Systems';
        a.Product_SBU__c = productSBU2;
       insert a2;
        
      //Add eGreensheet record test 1
      Workflow_details__c eg = new Workflow_details__c();
        string productSBUeg = 'Electronic Solutions';
        eg.Product_SBU__c = productSBUeg;
        eg.Sales_Channel__c = 'Americas AM';
        eg.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg.Approval_Level_Required__c = 'Tier II';
        eg.Sales_K__c = 0;
        eg.X5_Year_Revenue_K__c = 0;
        eg.Terms_and_Conditions__c = 'Standard';
        eg.Front_End_Loss__c = 0;
        eg.Exposed_Cost__c = 0;
        eg.Documentum__c = 'test.com';
      insert eg;
        
      //Send through eGreensheet record into class
        String productSBUSearchString = eg.Id+','+eg.Approval_Level_Required__c;
     	List<String> productSBUSearch = new List<String>();
		productSBUSearch.add(productSBUSearchString);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch);
        system.debug(productSBUSearch);
        
        
      //Add eGreensheet record test 2
      Workflow_details__c eg2 = new Workflow_details__c();
        string productSBUeg2 = 'Other Aero';
        eg2.Product_SBU__c = productSBUeg2;
        eg2.Sales_Channel__c = 'Americas AM';
        eg2.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg2.Approval_Level_Required__c = 'Tier II';
        eg2.Sales_K__c = 0;
        eg2.X5_Year_Revenue_K__c = 0;
        eg2.Terms_and_Conditions__c = 'Standard';
        eg2.Front_End_Loss__c = 0;
        eg2.Exposed_Cost__c = 0;
        eg2.Documentum__c = 'test.com';
      insert eg2;
       
         //Send through invalid eGreensheet record
        String productSBUSearchString2 = eg2.Id+','+eg2.Approval_Level_Required__c;
     	List<String> productSBUSearch2 = new List<String>();
		productSBUSearch2.add(productSBUSearchString2);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch2);
                     
    }                                                               
}

 
I'm struggling with finishing off this piece of code, anyone have a suggestion? Its returning an error at line 22: invalid loop variable type expected Approver__c was List
public class Invocable_eG_ProductSBUSearch
{
	@InvocableMethod(label='Find Valid Approvers' description ='Searchs the database for matches in SEA Approvers')
    //productSBUSearch comes from the flow and contains the eGreensheet ID and Tier selected concatenated
    public static List<List<Approver__c>> approverIds (List<String> productSBUSearch){
      	
 		// One sObject per search eGreensheet record (bulkify)
		List<List<SObject>> wrapsApprovers = new List<List<SObject>>();
		for (String searchTerm : productSBUSearch) {
			wrapsApprovers.add(new List<SObject>());
			}
        
        //Pull eGreensheet Product SBU into a list
        List<Workflow_Details__c> eGs = [SELECT Product_SBU_List__c FROM Workflow_Details__c where Id in :productSBUSearch];
        
        //Pull all possible approvers
        List<Approver__c> possibleApprovers = [SELECT Product_SBU__c, Tier__c FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :productSBUSearch];
        system.debug('wwwwwwwwwwwww' + possibleApprovers);
        
		//Add all approvers who qualify to the final list
		//WFList[0].Product_SBU__c.contains(Alist[j].Product_SBU__c)
		for (List<SObject> listOfApprovers : possibleApprovers){
			for (SObject returnList : listOfApprovers) {
            //Query for matches  
                if(eGs[0].Product_SBU_List__c.contains(possibleApprovers[0].Product_SBU__c)){
			//Return fields to the flow
			wrapsApprovers[0].add(returnList);
                }
			}
		}        
        return wrapsApprovers;   
 }
}
I created a checkbox field ‘subscribe__c’ on account object that I want to be updated
base on subscription charge names annual, monthly, quarterly on another object called subscription and charges
  • October 24, 2018
  • Like
  • 0
Hello - Newbie developer here and we have a trigger written a few years ago that has started to throw Apex CPU errors. I read through the articles on how to make them more efficient but I'm stuck on this one, any suggestions?
trigger OpportunityProduct_UpdateTechSales on OpportunityLineItem (before insert,before update, after update,after delete) {
    list<id> prodlineids = new list<id>(); list<id> oppids = new list<id>();set<id> plcrids = new set<id>();set<id> plcrids1 = new set<id>();
    transient  list<Product_Line_Tech_Sales__c> lstplts1 = new list<Product_Line_Tech_Sales__c>();
    list<Opportunity> lstopp = new list<Opportunity>();
    map<id,Product_Line_Cross_Ref__c> mapplcr; map<id,Product_Line_Cross_Ref__c> mapplcr1;
    list<string> usernames = new list<string>();
    map<string,id> usermap = new map<string,id>(); map<id,Opportunity> oppmap;
    list<Opportunity_Sales_Team__c> lstoppteam= new list<Opportunity_Sales_Team__c>();
    map<id,id> dupmap = new map<id,id>();list<user> userlst = new list<user>();
    set<id> contids = new set<id>(); Boolean op = true;
    if( TriggerInactive.TestOpportunityProduct_UpdateTechSales == True){
        if(trigger.isinsert){ 
            for(integer i=0;i<trigger.new.size();i++){
            oppids.add(trigger.new[i].Opportunityid);
                if(trigger.new[i].M_PM_Product__c!= null){plcrids.add(trigger.new[i].M_PM_Product__c);
                 system.debug('@@plcridsinsert'+plcrids);
                }    
            }
        }  
        if(Trigger.isupdate){
            for(integer i=0;i<trigger.new.size();i++){
                oppids.add(trigger.new[i].Opportunityid);
                if(trigger.new[i].M_PM_Product__c!=trigger.old[i].M_PM_Product__c){
                    plcrids.add(trigger.new[i].M_PM_Product__c);plcrids1.add(trigger.old[i].M_PM_Product__c);
                 system.debug('@@plcridsupdate'+plcrids);
                  system.debug('**plcrids1update'+plcrids1);
                }
            }
        }
        if(Trigger.isdelete){
            for(integer i=0;i<trigger.old.size();i++){
                plcrids1.add(trigger.old[i].M_PM_Product__c);oppids.add(trigger.old[i].Opportunityid);
                system.debug('**plcrids1update'+plcrids1);
                system.debug('$$oppidsupdate'+oppids);
            }
        }
        if(oppids.size()>0 ){
            oppmap = new map<id,Opportunity>([select id,Tech_Sales__c,Tech_Sales_Product_Area__c, Tech_Sales1__c, Tech_Sales2__c, Default_Tech_Sales__c,Default_Tech_Sales_Product__c,Default_Tech_Sales_Manager__c,Default_Tech_Sales_Manager__r.Name,Default_Tech_Sales_Manager_Secondary__c,Default_Tech_Sales_Manager_Secondary__r.Name,(select id,Contact__c from Opportunity.Opportunity_Sales_Teams__r),(select id,M_PM_Product__c,M_PM_Product__r.Product_Line_Chief_Engineer__c, M_PM_Product__r.Product_Leader__c, M_PM_Product__r.Product_Line__r.Product_Line_Finance_POC__c from OpportunityLineItems ) from Opportunity where id=:oppids]);
            if( Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore )){
                if(!Test.isRunningTest()) { 
                    lstplts1 = [select id,CBT__c,SBU__c,Region__c,Country__c,Tech_Sales__c,Tech_Sales_Product__c,Product_Type__c,Tech_Sales_Manager__r.Name,Tech_Sales_Manager__c,Tech_Sales_Manager_Secondary__r.Name,Tech_Sales_Manager_Secondary__c,Product_Line__r.Name,Product_Line__c from Product_Line_Tech_Sales__c Limit 20000];
                system.debug('%%lstplts1'+lstplts1);
                }
                else{
                               
                lstplts1 = [select id,CBT__c,SBU__c,Region__c,Country__c,Tech_Sales__c,Tech_Sales_Product__c,Product_Type__c,Tech_Sales_Manager__r.Name,Tech_Sales_Manager__c,Tech_Sales_Manager_Secondary__r.Name,Tech_Sales_Manager_Secondary__c,Product_Line__r.Name,Product_Line__c from Product_Line_Tech_Sales__c limit 10];
                system.debug('%%lstplts1else'+lstplts1);
                }       
            }
        }  
        if(plcrids.size()>0 && (Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore ))){
            mapplcr = new map<id,Product_Line_Cross_Ref__c>([select id,Product_Line__c,Product_Line__r.Product_Line_Finance_POC__c,Product_Leader__c,Product_Line_Chief_Engineer__c from Product_Line_Cross_Ref__c where id=:plcrids]);
            system.debug('##mapplcr'+mapplcr);
        }
        if(plcrids1.size()>0 && (Trigger.isdelete || ( Trigger.isupdate && Trigger.isAfter ))){
            mapplcr1 = new map<id,Product_Line_Cross_Ref__c>([select id,Product_Line__c,Product_Line__r.Product_Line_Finance_POC__c,Product_Leader__c,Product_Line_Chief_Engineer__c from Product_Line_Cross_Ref__c where id=:plcrids1]);
            system.debug('##mapplcr1'+mapplcr1);    
        }  
        for(Opportunity opp:oppmap.values()){
            usernames.add(opp.Tech_Sales1__c);usernames.add(opp.Tech_Sales2__c);
        }
        if(usernames.size()>0){
            userlst = [SELECT Id,Name FROM User WHERE Name =: usernames AND IsPortalEnabled = false];
        for(user u :userlst ){usermap.put(u.name,u.id);}
        }  
        if( Trigger.isinsert || (Trigger.isupdate && Trigger.isbefore )){
            for(OpportunityLineItem oplt:trigger.new){
                op = true;
                system.debug('##op'+op);
                if(oppmap.containsKey(oplt.Opportunityid) && (oppmap.get(oplt.Opportunityid).Default_Tech_Sales__c!=null || oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Product__c!=null)){
                    oplt.Tech_Sales__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales__c;
                    oplt.Tech_Sales_Product__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Product__c;
                    oplt.Tech_Sales_Manager__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Manager__c;
                    oplt.Tech_Sales_Manager_Secondary__c = oppmap.get(oplt.Opportunityid).Default_Tech_Sales_Manager_Secondary__c;oplt.OpportunityAccountChange__c = false;
                }
                else{
                    if(op == true){
                    system.debug('&&Op'+op);
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                        
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }
                    }
                    if(op == true){
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c; 
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false; 
                            } 
                        }//lstplts1.clear();
                    }
                    if(op == true){
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }// lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Line__c == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c; 
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c; 
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//stplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c; 
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && oplt.CBT__c == plts.CBT__c && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false;   
                                op = false;
                            }
                        } //lstplts1.clear();
                    }
                    if(op == true){ 
                        
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                           // system.debug('oplt value:'+oplt);
                            //system.debug('plts value:'+plts);
                            if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && oplt.Tech_Sales_Country__c == plts.Country__c && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c; 
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false; 
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){ 
                        for(Product_Line_Tech_Sales__c plts:lstplts1){
                           if( oplt.Tech_Sales_Product_Type__c == plts.Product_Type__c && '*All*' == plts.Product_Line__r.Name && oplt.SBU__c == plts.SBU__c && plts.CBT__c == null && oplt.Tech_Sales_Region__c == plts.Region__c && plts.Country__c == null && op == true){
                                oplt.Tech_Sales__c=plts.Tech_Sales__c;
                                oplt.Tech_Sales_Product__c = plts.Tech_Sales_Product__c;
                                oplt.Tech_Sales_Manager__c = plts.Tech_Sales_Manager__c;
                                oplt.Tech_Sales_Manager_Secondary__c = plts.Tech_Sales_Manager_Secondary__c;
                                oplt.OpportunityAccountChange__c = false;   
                                op = false;
                            }
                        }//lstplts1.clear();
                    }
                    if(op == true){
                        oplt.Tech_Sales__c = oppmap.get(oplt.Opportunityid).Tech_Sales__c;
                        oplt.Tech_Sales_Product__c = oppmap.get(oplt.Opportunityid).Tech_Sales_Product_Area__c;
                        oplt.OpportunityAccountChange__c = false;
                        oplt.Tech_Sales_Manager__c =  usermap.get(oppmap.get(oplt.Opportunityid).Tech_Sales1__c);
                        oplt.Tech_Sales_Manager_Secondary__c = usermap.get(oppmap.get(oplt.Opportunityid).Tech_Sales2__c);
                    }
                }
                map<id,id> contmap = new map<id,id>();
                /**for(Opportunity_Sales_Team__c oppsateam :oppmap.get(oplt.Opportunityid).Opportunity_Sales_Teams__r){
                    contmap.put(oppsateam.Contact__c,oppsateam.Contact__c);
                }  
                system.debug('@@mapplcr'+mapplcr);
                if(mapplcr!=null && !mapplcr.isempty()){
                    system.debug('Line187'+mapplcr);
                    system.debug('Line231'+(oplt.M_PM_Product__c));
                    system.debug('Line189'+contmap);
                    system.debug('!!mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c);
                    system.debug('##mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c);
                    system.debug('$$mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c'+mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c);
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c;
                            oppteam.Opportunity__c = oplt.Opportunityid; 
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Leader__c,oplt.Opportunityid);lstoppteam.add(oppteam);
                        }
                    }
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c;
                            oppteam.Opportunity__c = oplt.Opportunityid; 
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c,oplt.Opportunityid); lstoppteam.add(oppteam);
                        }
                    }           
                    if( mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c!=null && (!contmap.containsKey(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c))){
                        if( dupmap.get(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c) != oplt.Opportunityid ){
                            Opportunity_Sales_Team__c oppteam = new Opportunity_Sales_Team__c();
                            oppteam.Contact__c = mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c;
                            oppteam.Opportunity__c = oplt.Opportunityid;
                            dupmap.put(mapplcr.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c,oplt.Opportunityid); lstoppteam.add(oppteam);
                        }
                    }
                }**/
            }
        }
        if(lstoppteam.size()>0){ insert lstoppteam; }
        system.debug('**mapplcr1'+mapplcr1);
        if((Trigger.isdelete && mapplcr1!=null && !mapplcr1.isempty() ) || (mapplcr1!=null && !mapplcr1.isempty() && Trigger.isupdate && Trigger.isAfter  )){
            set<id> setids = new set<id>(); list<Opportunity_Sales_Team__c> oppsalteamlst = new list<Opportunity_Sales_Team__c>();
            for(OpportunityLineItem oplt:trigger.old){
                map<id,Opportunity_Sales_Team__c> contmap1 = new map<id,Opportunity_Sales_Team__c>();
                for(Opportunity_Sales_Team__c oppsateam :oppmap.get(oplt.Opportunityid).Opportunity_Sales_Teams__r){
                    contmap1.put(oppsateam.Contact__c,oppsateam);
                } 
                for(OpportunityLineItem oplt2:oppmap.get(oplt.Opportunityid).OpportunityLineItems){
                    setids.add(oplt2.M_PM_Product__r.Product_Leader__c);
                    setids.add(oplt2.M_PM_Product__r.Product_Line_Chief_Engineer__c);
                    setids.add(oplt2.M_PM_Product__r.Product_Line__r.Product_Line_Finance_POC__c);
                } 
                    /*system.debug('Line230'+mapplcr1);
                    system.debug('Line231'+(oplt.M_PM_Product__c));
                    system.debug('Line232'+setids);
                    system.debug('Line233'+contmap1);
                    system.debug('!!mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c);
                    system.debug('##mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c);
                    system.debug('$$mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c'+mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c);
                    */
                if(oplt.M_PM_Product__c !=null && mapplcr1.containsKey(oplt.M_PM_Product__c)){
                    if(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c!=null){
                        if(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Leader__c));
                        }
                        if(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Line_Chief_Engineer__c));
                        }
                        if( mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c!=null && (!setids.contains(mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c)) && (contmap1.containsKey(mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c))){
                            oppsalteamlst.add( contmap1.get( mapplcr1.get(oplt.M_PM_Product__c).Product_Line__r.Product_Line_Finance_POC__c));              
                        }
                    }
                }
            }
            if(oppsalteamlst.size()>0){delete oppsalteamlst;}      
        } 
    }
}