You need to sign in to do that
Don't have an account?
MrHammy
First bulk apex, where to start
I have my first trigger up and runing but i do not understand where to start for bulkifacaton. It works fine on up to 9 updates and crashes after that. I am used to php and mysql. if someone could look at my code and point me in the right direction it would be great! here is my code:
trigger setProject_Territory_Link on LNA_Project__c (after update){ List<Project_Territory_Link__c> newptl = new List<Project_Territory_Link__c>(); for (LNA_Project__c p : Trigger.new) { List<Project_Territory_Link__c> ter_link_all = [SELECT id FROM Project_Territory_Link__c WHERE name = :p.Id ]; List<Project_Territory_Link__c> ter_link_used = new List<Project_Territory_Link__c>(); if (p.Territory_VN__c != null){ sobject vn_ter = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_VN__c limit 1 ]; if (vn_ter.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :vn_ter.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = vn_ter.id ); newptl.add(ptl); } } } if (p.Territory_CDC__c != null){ sobject cdc_ter = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_CDC__c limit 1 ]; if (cdc_ter.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :cdc_ter.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = cdc_ter.id ); newptl.add(ptl); } } } if (p.Territory_EWS__c != null){ sobject ews_ter = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_EWS__c limit 1 ]; if (ews_ter.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :ews_ter.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = ews_ter.id ); newptl.add(ptl); } } } if (p.Territory_WS__c != null){ sobject ws_ter = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_WS__c limit 1 ]; if (ws_ter.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :ws_ter.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = ws_ter.id ); newptl.add(ptl); } } } if (p.Territory_WM_C__c != null){ sobject WM_C_link = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_WM_C__c limit 1 ]; if (WM_C_link.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :WM_C_link.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = WM_C_link.id ); newptl.add(ptl); } } } if (p.Territory_PS_C__c != null){ sobject PS_C_link = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_PS_C__c limit 1 ]; if (PS_C_link.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :PS_C_link.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = PS_C_link.id ); newptl.add(ptl); } } } if (p.Territory_CF_C__c != null){ sobject CF_C_link = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_CF_C__c limit 1 ]; if (CF_C_link.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :CF_C_link.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = CF_C_link.id ); newptl.add(ptl); } } } if (p.Territory_OQ__c != null){ sobject OQ_link = [Select Id , Name From LNA_Territory__c WHERE Name = :p.Territory_OQ__c limit 1 ]; if (OQ_link.id != null){ try{ sobject ter_link = [Select Id From Project_Territory_Link__c where LNA_Territory__c = :OQ_link.id and LNA_Project__c = :p.Id limit 1 ]; } catch(System.QueryException e){ Project_Territory_Link__c ptl = new Project_Territory_Link__c (LNA_Project__c = p.ID, LNA_Territory__c = OQ_link.id ); newptl.add(ptl); } } } } insert newptl;
Hi,
You are trying to use SQOL (select) statements inside FOR loop which should not be used.
Instead use map/list before for loop and use that inside loop to get required data.
Thanks,
JBabu.