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
impalacrazy69impalacrazy69 

System.LimitException: Too many SOQL queries: 101 Trigger.pullSE: line 5, column 1".

So i have the attached trigger on my case object. When trying to do a mahor upload we received the following errror:

 

System.LimitException: Too many SOQL queries: 101 Trigger.pullSE: line 5, column 1".

 

From reading other post it seems that i have to take the select statement out of the for loop. But my issue is that when i try its not pulling the information as it was. I tried <list> and <map> functions and nothing works so i have put it back to normal and now the code works again. Can anyone help me figure the best approach to get rid of the error.

 

Code:

trigger pullSE on Case (before update, before insert) {
    for (case t: trigger.new) {
        try {
            if (t.SE__c == null) {
                t.SE__c = [select User__c from Site__c where Id = :t.Site__c].User__c; //t.Site__r.User__c;
            }
        }
        catch (Exception e) {
        }
    }
}

 

Thanks....

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97
yes put it in []

for(site__c s:[select id,user__c from site__c where id in:userids]){

All Answers

testrest97testrest97

trigger pullSE on Case (before update, before insert) {
Set<id> userids=new Set<id>();
Map<id,string> siteMap=new Map<id,string>();
    for (case t: trigger.new) {

        try {
            if (t.SE__c == null) {
                userids.add(t.site__c);                
            }
        }
        catch (Exception e) {
        }
    }
if(userids!=null){
for(site__c s:select id,user__c from site__c where id in:userids){
siteMap.put(s.id,s.user__c);
}
}
for(case t:trigger.new){
t.se__c=sitemap.get(t.site__c);
}
}

 

 

 

impalacrazy69impalacrazy69

Thanks testrest97, but now im getting a save error that reads: Unexpected Token: 'select' - The error is in the section of code below

if(userids!=null){
		for(site__c s:select id,user__c from site__c where id in:userids){
			siteMap.put(s.id,s.user__c);
			}
		}

 

 

Should this be in brackets?

testrest97testrest97
yes put it in []

for(site__c s:[select id,user__c from site__c where id in:userids]){
This was selected as the best answer
impalacrazy69impalacrazy69

Awesome that fixed it. Thanks im going to bulk upload and test the code to see if the error returns.

 

Thanks for your help the code and fix helped.

impalacrazy69impalacrazy69

HI testrest97,

 

So the help you gave me was great it fixed the issue i was having with my code for bulk changes and closes. But in doing that i have a new issue. Now when u go to change the SE field and select a new user after selecting save it clears our the field. I think i need an else statement in here somewhere to keep whatever is already there but not sure where as when i try i keep getting errors. could you help.

	trigger pullSE on Case (before update, before insert) {
Set<id> userids=new Set<id>();
Map<id,string> siteMap=new Map<id,string>();
    for (case t: trigger.new) {

        try {
            if (t.SE__c == null) {
                userids.add(t.site__c);                
            }
        }
        catch (Exception e) {
        }
    }



	if(userids!=null){
		for(site__c s:[select id,user__c from site__c where id in:userids]){
			siteMap.put(s.id,s.user__c);
			}
		}
		for(case t:trigger.new){
			t.se__c=sitemap.get(t.site__c);
		}
	}