+ Start a Discussion
kittu9kittu9 

Governor limits error in the following trigger

 

trigger Trigger1 on Task (after insert,after update)
{
List<String> mlist = new List<String>();
List<String> mailAddrs = new List<String>();
String fURL = URL.getSalesforceBaseUrl().toExternalForm() ;


for(Task t : Trigger.new)

{
Account act = [select Name from Account where Id =: t.AccountId];
if(t.Status == 'Completed')
{
if( t.Activity_Type__c == 'Resolved')
{

Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SET A'];

for (GroupMember gm : g.groupMembers)
{
mlist.add(gm.userOrGroupId);
}

User[] usr = [SELECT email FROM user WHERE id IN :mlist];
for(User u : usr)
{
mailAddrs.add(u.email);

}
}
else if ( t.Activity_Type__c == 'Completed')

{
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SET B'];
for (GroupMember gm : g.groupMembers)

{
mlist.add(gm.userOrGroupId);
}

User[] usr = [SELECT email FROM user WHERE id IN :mlist];
for(User u : usr)
{
mailAddrs.add(u.email);
}
}

else if ( t.Activity_Type__c == 'In Progress')

{
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SET C'];
for (GroupMember gm : g.groupMembers)

{
mlist.add(gm.userOrGroupId);

}
User[] usr = [SELECT email FROM user WHERE id IN :mlist];
for(User u : usr)
{
mailAddrs.add(u.email);
}
}

else if ( t.Activity_Type__c == 'On Hold')

{
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SET D'];
for (GroupMember gm : g.groupMembers)

{
mlist.add(gm.userOrGroupId);

}
User[] usr = [SELECT email FROM user WHERE id IN :mlist];
for(User u : usr)
{
mailAddrs.add(u.email);
}
}

else
{
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SET E'];

for (GroupMember gm : g.groupMembers) {
mlist.add(gm.userOrGroupId); }

User[] usr = [SELECT email FROM user WHERE id IN :mlist];
for(User u : usr) { mailAddrs.add(u.email); }

}

if(mailAddrs != null || mailAddrs.size() > 0)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(mailAddrs);

mail.setSubject(t.Activity_Type__c+' : '+act.Name+' Task Information');

mail.setPlainTextBody(' Contact Report has been completed '+'\n'+fURL+'/'+t.Id);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

}
}

 

The above trigger is working fine. Can any one help to avoid Governor limits for this trigger. by using collections can do it. but am not able fix it please help me on this.

jungleeejungleee

I have not added all the if condition. I hope you can build on the below example

 

trigger Trigger1 on Task(after insert, after update) {
    List < String > mlist = new List < String > ();
    List < String > mailAddrs = new List < String > ();
    String fURL = URL.getSalesforceBaseUrl().toExternalForm();
	set<string> groupNameSet = new set<string>{'SET A','SET B'}; //add all the group Names later
	map<string, list<Id>> groupNameUserMapping = new map<string, list<Id>>();
	set<id> allUserId = new set<Id>();
	List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
	set<ID> accountIdSet = new set<id>();
	
	//creating a map of groupmember id and the group name.
	for(groupMembers gm: [select userOrGroupId,group.name from groupMember where group.name IN :groupNameSet]){
		if(groupNameUserMapping.contains(gm.group.name)){
			list<id> tempList = groupNameUserMapping.get(gm.group.Name);
			tempList.add(gm.userOrGroupId);
			groupNameUserMapping.add(gm.group.Name, tempList);
		}else{
			list<id> tempList = new list<Id>();
			tempList.add(gm.userOrGroupId);
			groupNameUserMapping.add(gm.group.Name, tempList);
		}
		allUserId.add(gm.userOrGroupId);
	}
	
	//creating a map of user to fetch the email id.
	map<id, user> userMap = map<id, user>([SELECT id, email FROM user id IN:allUserId]);	
	
	for (Task t: Trigger.new) {
		if(t.status =='Completed'){
			accountIdSet.add(t.accountId);
		}
	}
	
	//creating a map of account id.
	map<id, account> accountMap = map<id, account>([SELECT name, id from account WHERE id IN: accountIDSet])
	
    for (Task t: Trigger.new) {
        if (t.Status == 'Completed') {
            if (t.Activity_Type__c == 'Resolved') {
				for(Id i: groupNameUserMapping.get('SET A').values()){
					mailAddrs.add(userMap.get(i).email);
				}

            } else if (t.Activity_Type__c == 'Completed') {
                for(Id i: groupNameUserMapping.get('SET B').values()){
					mailAddrs.add(userMap.get(i).email);
				}
            }

            if (mailAddrs != null || mailAddrs.size() > 0) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(mailAddrs);

                mail.setSubject(t.Activity_Type__c + ' : ' + accountMap.get(t.accountId).Name + ' Task Information');
                mail.setPlainTextBody(' Contact Report has been completed ' + '\n' + fURL + '/' + t.Id);
                listmail.add(mail);
            }
        }
    }
	messaging.sendEmail(listMail);
}

 

 

kittu9kittu9
trigger Trigger1 on Task(after insert, after update) {
    List < String > mlist = new List < String > ();
    List < String > mailAddrs = new List < String > ();
    String fURL = URL.getSalesforceBaseUrl().toExternalForm();
	set<string> groupNameSet = new set<string>{'SET A','SET B'}; //add all the group Names later
	map<string, list<Id>> groupNameUserMapping = new map<string, list<Id>>();
	set<id> allUserId = new set<Id>();
	List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
	set<ID> accountIdSet = new set<id>();
	
	//creating a map of groupmember id and the group name.
	for(groupMembers gm: [select userOrGroupId,group.name from groupMember where group.name IN :groupNameSet]){
		if(groupNameUserMapping.contains(gm.group.name)){
			list<id> tempList = groupNameUserMapping.get(gm.group.Name);
			tempList.add(gm.userOrGroupId);
			groupNameUserMapping.put(gm.group.Name, tempList);
		}else{
			list<id> tempList = new list<Id>();
			tempList.add(gm.userOrGroupId);
			groupNameUserMapping.put(gm.group.Name, tempList);
		}
		allUserId.add(gm.userOrGroupId);
	}
	
	//creating a map of user to fetch the email id.
	map<id, user> userMap = map<id, user>([SELECT id, email FROM user where id IN:allUserId]);	
	
	for (Task t: Trigger.new) {
		if(t.status =='Completed'){
			accountIdSet.add(t.accountId);
		}
	}
	
	//creating a map of account id.
	map<id, account> accountMap = map<id, account>([SELECT name, id from account WHERE id IN: accountIDSet])
	
    for (Task t: Trigger.new) {
        if (t.Status == 'Completed') {
            if (t.Activity_Type__c == 'Resolved') {
				for(Id i: groupNameUserMapping.get('SET A').values()){
					mailAddrs.add(userMap.get(i).email);
				}

            } else if (t.Activity_Type__c == 'Completed') {
                for(Id i: groupNameUserMapping.get('SET B').values()){
					mailAddrs.add(userMap.get(i).email);
				}
            }

            if (mailAddrs != null || mailAddrs.size() > 0) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(mailAddrs);

                mail.setSubject(t.Activity_Type__c + ' : ' + accountMap.get(t.accountId).Name + ' Task Information');
                mail.setPlainTextBody(' Contact Report has been completed ' + '\n' + fURL + '/' + t.Id);
                listmail.add(mail);
            }
        }
    }
	messaging.sendEmail(listMail);
}

Getting the following error:

Error: Compile Error: unexpected token: 'map' at line 28 column 27 
nileshjnileshj
//creating a map of user to fetch the email id.
	map<id, user> userMap = new map<id, user>([SELECT id, email FROM user where id IN:allUserId]);	

Follow follwing best practices to avoid governor limits :
http://wiki.developerforce.com/page/Apex_Code_Best_Practices
kittu9kittu9

Thanks a lot jungleee & nileshj.

 

Still am phasing issue. 

 

trigger Trigger2 on Task(after insert, after update)
{
List < String > mlist = new List < String > ();
List < String > mailAddrs = new List < String > ();
String fURL = URL.getSalesforceBaseUrl().toExternalForm();
set<string> groupNameSet = new set<string>{'SET A','SET B'}; //add all the group Names later
map<string, list<Id>> groupNameUserMapping = new map<string, list<Id>>();
set<id> allUserId = new set<Id>();
List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
set<ID> accountIdSet = new set<id>();

//creating a map of groupmember id and the group name.
for(groupMember gm: [select userOrGroupId,group.name from groupMember where group.name IN :groupNameSet]){
if(groupNameUserMapping.equals(gm.group.name)){
list<id> tempList = groupNameUserMapping.get(gm.group.Name);
tempList.add(gm.userOrGroupId);
groupNameUserMapping.put(gm.group.Name, tempList);
}else{
list<id> tempList = new list<Id>();
tempList.add(gm.userOrGroupId);
groupNameUserMapping.put(gm.group.Name, tempList);
}
allUserId.add(gm.userOrGroupId);
}

//creating a map of user to fetch the email id.
//creating a map of user to fetch the email id.
//List<user> userMap = [SELECT id, email FROM user where id IN:allUserId];
map<id, user> userMap = new map<id, user>([SELECT id, email FROM user where id IN:allUserId]);


for (Task t: Trigger.new) {
if(t.status =='Completed'){
accountIdSet.add(t.accountId);
}
}
//creating a map of account id.
map<id, account> accountMap = new map<id, account>([SELECT name, id from account WHERE id IN: accountIDSet]);
for (Task t: Trigger.new) {
if (t.Status == 'Completed') {
if (t.Activity_Type__c == 'Resolved') {
System.debug('********41*********');
for(Id i: groupNameUserMapping.get('SET A').values()){
mailAddrs.add(userMap.get(i).email);
}
System.debug('********41*********'+mailAddrs);
} else if (t.Activity_Type__c == 'Completed') {
for(Id i: groupNameUserMapping.get('SET B').values()){
mailAddrs.add(userMap.get(i).email);
}
}

if (mailAddrs != null || mailAddrs.size() > 0) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(mailAddrs);

mail.setSubject(t.Activity_Type__c + ' : ' + accountMap.get(t.accountId).Name + ' Task Information');
mail.setPlainTextBody(' Contact Report has been completed ' + '\n' + fURL + '/' + t.Id);
listmail.add(mail);
}
}
}
messaging.sendEmail(listMail);

}

 

Error Error: Compile Error: Method does not exist or incorrect signature: [LIST<Id>].values() at line 43 column 61

 

I thought to remove values() but it is retriving only one mail id from the group "SET A" or "SET B". Please help me on this

 

 

jungleeejungleee
Please remove the .values() in both the lines
kittu9kittu9

I tried by removing values() But in SET A i have 4 users but it is retriving only user email id not getting 4users email ids. Please let me know where have to change. thanks in advance