You need to sign in to do that
Don't have an account?

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.}
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.}
Let us know if this will help you
All Answers
Let us know if this will help you