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
Lance BrownLance Brown 

Apex Trigger caused an unexpected exception "System.Limit.Exception:Too many SOQL queries:101"

WHat did I do wrong?  I'm unable to dataload any changes.


trigger LOC_on_IP on SVMXC__Installed_Product__c (before insert, before update) {

    List<SVMXC__Installed_Product__c> locationToUpdate = new List<SVMXC__Installed_Product__c>();

    for (SVMXC__Installed_Product__c ip : Trigger.new)
    {    
      if (ip.SVMXC__Company__c != NULL)
      {
          // Find the Loaction for the current Acct
          List<SVMXC__Site__c> loc = [select Loc_ID__c from SVMXC__Site__c
                             where SVMXC__Account__c = :ip.SVMXC__Company__c limit 1];     
               
          // if you found one
          if (loc.size() > 0)
          {   
              //assign the Location to the Accounts Location
              ip.SVMXC__Site__c  = loc[0].Loc_ID__c;
         
              locationToUpdate.add(ip);
         
          }
       }
    }
}
AshlekhAshlekh
Hi,

This problem is coming because you are using soql in for loop,

Use below code 
trigger LOC_on_IP on SVMXC__Installed_Product__c (before insert, before update) 
{

    List<SVMXC__Installed_Product__c> locationToUpdate = new List<SVMXC__Installed_Product__c>();
	Set<ID> SVMXCAccountId = new Set<ID>();
	for (SVMXC__Installed_Product__c ip : Trigger.new)
    {
		if (ip.SVMXC__Company__c != NULL)
			SVMXCAccountId.add(ip.SVMXC__Company__c);
	}
	
	Map<id,SVMXC__Site__c> MapOFSite = new Map<id,SVMXC__Site__c>();
	
	for(SVMXC__Site__c loc : [select Loc_ID__c,SVMXC__Account__c  from SVMXC__Site__c where SVMXC__Account__c in :SVMXCAccountId ])
	{
		MapOFSite.put(loc.SVMXC__Account__c,loc);
	}	
    
	
    for (SVMXC__Installed_Product__c ip : Trigger.new)
    {    
      if (ip.SVMXC__Company__c != NULL)
      {
          if (MapOFSite.containsKey(ip.SVMXC__Company__c))
          {   
              //assign the Location to the Accounts Location
              ip.SVMXC__Site__c  = MapOFSite.containsKey(ip.SVMXC__Company__c).Loc_ID__c;
              locationToUpdate.add(ip);
          }
       }
    }
}

Lance BrownLance Brown
Thank you so much for responding, I am getting an error with the code.  Line 27 Initial term of field expression must be a concrete SObject: Boolean
Lance BrownLance Brown
Addition insight.

The goal is to populate a lookup (SVMXC__Site__c) on the Installed Products Object  that has a lookup (SVMXC__Company__c)  to the Account Page .  The Account Page has a lookup to the "SVMXC__Site__c" called (EQU_Location__c).  SO I need to get the Id from the Accounts "EQU_Location__c" to the Installed Products "SVMXC__Site__c"
Justin JulicherJustin Julicher
Hi Lance,

The issue in Ashlekh code is that he is calling 'containsKey' on the map 

So replace line 27 with the following and it should resolve your issue:

ip.SVMXC__Site__c = MapOFSite.get(ip.SVMXC__Company__c).Loc_ID__c;