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
deplaideplai 

Inefficient trigger reaching governor limits

Hello,

We had a trigger written for us by a developer who no longer works fo us.  When I do a bulk insert/update I get errors against the governor limits.  What should be done to my code to make it more effiecient?  I'm not a coder, but I kinda figure out how things work.   Any help would be greatly appreciated! Thanks!

 

trigger BeforeCaseInsert on Case (before insert,before update) {

private final Id recordTypeId = [SELECT id FROM RecordType WHERE name =: 'JIRA'].id;
private final Id recordProdSquadID = [SELECT id FROM RecordType WHERE name =: 'Prod Specialist'].id;
private final Id recordBPID = [SELECT id FROM RecordType WHERE name =: 'BenchPress'].id;
private final Id recordSupport = [SELECT id FROM RecordType WHERE name =: 'Support'].id;
private List<Id> lstAccount = new List<Id>();
private Map<Id, Account> accMap = new Map <Id,Account>();
private List <Sites__c> sitesF;

// Get a list of Accounts
for (Case c : trigger.new){
lstAccount.add(c.accountId);
}

// Create a Map with AccountId, Account to reference later in the code
for (Account a : [Select id,publication_Manager2__c from Account where id in: lstAccount]){
accMap.put(a.id,a);
}

sitesF = new List<Sites__c>([SELECT id, Name, Feedback__c, Account__c, Account__r.Publication_Manager2__c
FROM Sites__c LIMIT 50000]);

for (Case c : trigger.new) {
for(Sites__c s : sitesF) {
if (s.Feedback__c != null
&& c.Description != null
&& c.Description != ''
&& c.recordTypeID == recordSupport
&& c.Description.contains(s.Feedback__c)) {
c.sites__c = s.id;
c.accountId = s.account__c;
accMap.put(s.account__c,s.Account__r);
break;
}
}
// This is a second bit of code
// if (c.recordTypeId == recordTypeId
// && accMap.get(c.accountId)!= null
// && accMap.get(c.accountId).Publication_Manager2__c != null) {
// c.OwnerId = accMap.get(c.accountId).publication_Manager2__c;
// }

// Paul code
if (c.recordTypeId == recordProdSquadID
&& c.JCode__c != null
&& c.JCode__c != ''
) {
for(Sites__c s : sitesF) {
if (c.JCode__c == s.Name){
c.accountId = s.account__c;
c.Sites__c = s.id;
accMap.put(s.account__c,s.Account__r);
break;
}
}

}
if (c.recordTypeId == recordBPID)
{
for(Sites__c s : sitesF) {

if (c.Journal_Code__c == s.id){
c.Sites__c = s.id;
c.accountId = s.account__c;

accMap.put(s.account__c,s.Account__r);
break;
}
}
}

}

Starz26Starz26

What limit are you hitting and what is the exact message?

Anup JadhavAnup Jadhav
It'd also help if you could explain what this trigger is supposed to do. Post as much information as you can about the error message without actually posting the error message dump.

Thanks,
Anup
priyanka.mv26priyanka.mv26

I have modified the code..

 

trigger BeforeCaseInsert on Case (before insert,before update)
{
List<String> RecordTypeNameList = {'JIRA','Prod Specialist','BenchPress','Support'};

List<RecordType> RecordTypeResult = [SELECT id,Name FROM RecordType WHERE name in: RecordTypeNameList];

Map<String,Id> RecordTypeNameIDMap = new Map<String,Id>();

for(RecordType rt : RecordTypeResult)
{
RecordTypeNameIDMap.put(rt.Name,rt.id);
}

/* private final Id recordTypeId = [SELECT id FROM RecordType WHERE name =: 'JIRA'].id;
private final Id recordProdSquadID = [SELECT id FROM RecordType WHERE name =: 'Prod Specialist'].id;
private final Id recordBPID = [SELECT id FROM RecordType WHERE name =: 'BenchPress'].id;
private final Id recordSupport = [SELECT id FROM RecordType WHERE name =: 'Support'].id; */
private List<Id> lstAccount = new List<Id>();
private Map<Id, Account> accMap = new Map <Id,Account>();
private List <Sites__c> sitesF;

// Get a list of Accounts
for (Case c : trigger.new){
lstAccount.add(c.accountId);
}

// Create a Map with AccountId, Account to reference later in the code
for (Account a : [Select id,publication_Manager2__c from Account where id in: lstAccount]){
accMap.put(a.id,a);
}

sitesF = new List<Sites__c>([SELECT id, Name, Feedback__c, Account__c, Account__r.Publication_Manager2__c
FROM Sites__c LIMIT 50000]);

for (Case c : trigger.new) {
for(Sites__c s : sitesF) {
if (s.Feedback__c != null
&& c.Description != null
&& c.Description != ''
&& c.recordTypeID == RecordTypeNameIDMap.get('Support')
&& c.Description.contains(s.Feedback__c)) {
c.sites__c = s.id;
c.accountId = s.account__c;
accMap.put(s.account__c,s.Account__r);
break;
}
}
// This is a second bit of code
// if (c.recordTypeId == RecordTypeNameIDMap.get('JIRA')
// && accMap.get(c.accountId)!= null
// && accMap.get(c.accountId).Publication_Manager2__c != null) {
// c.OwnerId = accMap.get(c.accountId).publication_Manager2__c;
// }

// Paul code
if (c.recordTypeId == RecordTypeNameIDMap.get('Prod Specialist')
&& c.JCode__c != null
&& c.JCode__c != ''
) {
for(Sites__c s : sitesF) {
if (c.JCode__c == s.Name){
c.accountId = s.account__c;
c.Sites__c = s.id;
accMap.put(s.account__c,s.Account__r);
break;
}
}

}
if (c.recordTypeId == RecordTypeNameIDMap.get('BenchPress'))
{
for(Sites__c s : sitesF) {

if (c.Journal_Code__c == s.id){
c.Sites__c = s.id;
c.accountId = s.account__c;

accMap.put(s.account__c,s.Account__r);
break;
}
}
}

}

 

Hope this solves your problem... I think the error may be too many SOQL statements 101 when you perform bulk trigger...

 

If this code doesn't solves your problem, then provide me details about the issue which help me to fix the issue..

 

deplaideplai

Yes that was exactly the error I was running into.  I will test this code and let you know if it has worked.  Thank you very much for taking the time to look into this!