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
Shawn ReichnerShawn Reichner 

SOQL 101 Error Help Please

Thank you for any insight and for spending a few minutes to look over the following code.  I am fairly new to apex and I am having issues getting this trigger to run in a batch scenario.  I am constantly getting SOQL 101 Govenor Limit error and can not figure out how to properly "Bulkify" this code. 

Hoping someone can help out with some advice....Thanks in advance!!!!

The error is showing as hittong the limit from line 27...


Trigger Code:
1. trigger InstallProdDuplicatePreventer on AVISPL_Client_Product__c (before insert, before update) {
2.
3.    //They can have the same name provided they have different record types
4.
5.    Map<String, AVISPL_Client_Product__c> IPMap = new Map<String, AVISPL_Client_Product__c>();
6.    Set<String> ipNameAndRecordTypeIdConcatSet = new Set<String>();
7.    for (AVISPL_Client_Product__c IP : System.Trigger.new) {
8.        If(IP.Virtual_Product__c == false){
9.        String ipNameAndRecordTypeId = IP.name + '' + IP.recordTypeId;
10.        
11.        // Make sure we don't treat a name that isn't changing during an update as a duplicate.  
12.        if ((IP.Name != null) &&
13.                (System.Trigger.isInsert ||
14.                    IP.Name != System.Trigger.oldMap.get(IP.Id).Name)) {
15.        
16.            // Make sure another new Installed Product isn't also a duplicate  
17.            if (IPMap.containsKey(IP.Name) && ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) {
18.                IP.Name.addError('Another new Installed Product has the same Name for this Record Type.');
19.            } else {
20.                IPMap.put(IP.Name, IP);
21.                ipNameAndRecordTypeIdConcatSet.add(ipNameAndRecordTypeId);
22.            }
23.        }
24.    }
25.    
26.    // Using a single database query, find all the Installed Products in the database that have the same name as any of the Installed Products being inserted or updated.  
27.    for (AVISPL_Client_Product__c Ip2 : [SELECT Name, RecordTypeId FROM AVISPL_Client_Product__c WHERE Name IN :IPMap.KeySet()]) {
28.        AVISPL_Client_Product__c newIP = IPMap.get(IP2.Name);
29.        String ipNameAndRecordTypeId = Ip2.name + '' + Ip2.recordTypeId;
30.        
31.        if(ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) {
32.            newIP.Name.addError('A Installed Product with this name already exists for this Record Type.');
33.        }
34.        }
35.    }
36.}
Best Answer chosen by Shawn Reichner
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger InstallProdDuplicatePreventer on AVISPL_Client_Product__c (before insert, before update) {
   //They can have the same name provided they have different record types

    Map<String, AVISPL_Client_Product__c> IPMap = new Map<String, AVISPL_Client_Product__c>();
    Set<String> ipNameAndRecordTypeIdConcatSet = new Set<String>();
    for (AVISPL_Client_Product__c IP : System.Trigger.new) 
	{
        If(IP.Virtual_Product__c == false)
		{
			String ipNameAndRecordTypeId = IP.name + '' + IP.recordTypeId;
        
			// Make sure we don't treat a name that isn't changing during an update as a duplicate.  
			if ( (IP.Name != null) &&
					(System.Trigger.isInsert ||
						IP.Name != System.Trigger.oldMap.get(IP.Id).Name) ) 
			{
		
				// Make sure another new Installed Product isn't also a duplicate  
				if (IPMap.containsKey(IP.Name) && ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) 
				{
					IP.Name.addError('Another new Installed Product has the same Name for this Record Type.');
				} 
				else 
				{
					IPMap.put(IP.Name, IP);
					ipNameAndRecordTypeIdConcatSet.add(ipNameAndRecordTypeId);
				}
			}
		}
    }
	
	// Using a single database query, find all the Installed Products in the database that have the same name as any of the Installed Products being inserted or updated.  
	for (AVISPL_Client_Product__c Ip2 : [SELECT Name, RecordTypeId FROM AVISPL_Client_Product__c WHERE Name IN :IPMap.KeySet()]) 
	{
		AVISPL_Client_Product__c newIP = IPMap.get(IP2.Name);
		String ipNameAndRecordTypeId = Ip2.name + '' + Ip2.recordTypeId;
		
		if(ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) 
		{
			newIP.Name.addError('A Installed Product with this name already exists for this Record Type.');
		}
	}
    
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger InstallProdDuplicatePreventer on AVISPL_Client_Product__c (before insert, before update) {
   //They can have the same name provided they have different record types

    Map<String, AVISPL_Client_Product__c> IPMap = new Map<String, AVISPL_Client_Product__c>();
    Set<String> ipNameAndRecordTypeIdConcatSet = new Set<String>();
    for (AVISPL_Client_Product__c IP : System.Trigger.new) 
	{
        If(IP.Virtual_Product__c == false)
		{
			String ipNameAndRecordTypeId = IP.name + '' + IP.recordTypeId;
        
			// Make sure we don't treat a name that isn't changing during an update as a duplicate.  
			if ( (IP.Name != null) &&
					(System.Trigger.isInsert ||
						IP.Name != System.Trigger.oldMap.get(IP.Id).Name) ) 
			{
		
				// Make sure another new Installed Product isn't also a duplicate  
				if (IPMap.containsKey(IP.Name) && ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) 
				{
					IP.Name.addError('Another new Installed Product has the same Name for this Record Type.');
				} 
				else 
				{
					IPMap.put(IP.Name, IP);
					ipNameAndRecordTypeIdConcatSet.add(ipNameAndRecordTypeId);
				}
			}
		}
    }
	
	// Using a single database query, find all the Installed Products in the database that have the same name as any of the Installed Products being inserted or updated.  
	for (AVISPL_Client_Product__c Ip2 : [SELECT Name, RecordTypeId FROM AVISPL_Client_Product__c WHERE Name IN :IPMap.KeySet()]) 
	{
		AVISPL_Client_Product__c newIP = IPMap.get(IP2.Name);
		String ipNameAndRecordTypeId = Ip2.name + '' + Ip2.recordTypeId;
		
		if(ipNameAndRecordTypeIdConcatSet.contains(ipNameAndRecordTypeId)) 
		{
			newIP.Name.addError('A Installed Product with this name already exists for this Record Type.');
		}
	}
    
}

Let us know if this will help you
 
This was selected as the best answer
Shawn ReichnerShawn Reichner
Thank you!!!!  Looks like we made the second For outside the first For loop.  Makes sense  Thanks again