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

send a list of users to email body
I have a user list. I need this user list to be passed to sendemail method, so that the user names are displayed in the email body as below:
"There has been no activity from the following sales people of your team:
1. User1
2. User2
3.User 3 ……. "
Could you please let me know as of how to achieve this/could yo send any relevent code which would be helpful.
Thanks in adv
"There has been no activity from the following sales people of your team:
1. User1
2. User2
3.User 3 ……. "
Could you please let me know as of how to achieve this/could yo send any relevent code which would be helpful.
Thanks in adv
Code will check if any of their subordinate have not edited any record in last seven days and only then add their lead to list to send email. Check this code out.
Thanks,
Fahad Akhtar
All Answers
Try below code in developer console and replace "Test@hotmail.com" with your actial email address and this should work.
Thanks,
Fahad Akhtar
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_creating.htm
https://developer.salesforce.com/page/VisualForceEmailTemplates_sample
One Sample with Contact and Case record. Please let us know if this will help you
Thanks
Amit Chaudhary
Try with below code it will help you .
Replace this list with your user name list .pass that list in your mathod .
Then in your email body manipulate like above .
Check in developer console ,let me know if it helps .
Thanks
Manoj
Thanks for the code, that helped a bit in email body.
I need top pass list(userlist which holds multiiple user names) to this sendemail method.
Can I pass a list as parameter to sendemail method.?
Or how it should be done.?
Thanks,
jagadish jagz
Not sure, if i got your question correctly, let me try. sendEmail method expect list of "SingleEmailMessage" object, it will not accpet a list of user name, if you are questions is that you need to send email to multiple users you can pass a list of email addresses if your question is that you have a list of username as string, you can do something link this
Thanks,
Fahad Akhtar
I guess I have coded to an extent to retreive VP(I specified Custom Label) and pass his Id to utility class which returns set of all users below him in heirarchy.
I am processing them and adding ton list if they did not modify any record in last week.
Now I need this list to be passed to sendemail method, andmention all users who did not modify any records in the EMAIL BODY.
The list which holds ids are below in unediteduseridlist.
This class needs to be scheduled weekly. So implemented schedulable and did code in execute() method. I guess this is fine
Hope you got my task. Kindly let me know if there are any changed rerquired.
Thanks in advance,.
CLASS:
Global class MyClass implements schedulable {
Global void execute(schedulablecontext SC)
{
date d = system.today().addDays(-7);
List<Account> acclist= [select id,name,LastModifiedById from account where LastModifiedDate >:d];
List<Opportunity> opplist= [select id,name,LastModifiedById from Opportunity where LastModifiedDate >:d];
List<Contact> conlist= [select id,name,LastModifiedById from Contact where LastModifiedDate >:d];
List<Lead> leadlist= [select id,name,LastModifiedById from Lead where LastModifiedDate >:d];
// List<activity> actlist = [select id,name,LastModifiedById from Activity where LastModifiedDate >:d]
set<id> accset = new set<Id>();
for(Account Acc : acclist){ accset.add(acc.LastModifiedById); }
set<id> oppset = new set<Id>();
for(Opportunity Opp : opplist){ oppset.add(opp.LastModifiedById); }
set<id> conset = new set<Id>();
for(contact con : conlist){ conset.add(con.LastModifiedById); }
set<id> leadset = new set<Id>();
for(lead ld : leadlist){ leadset.add(ld.LastModifiedById); }
List<Id> subordlist=new List<Id>();
List<id> unediteduseridlist = new List<id>();
List<user> uneditedusernamelist= new List<user>();
List<user> TLSPuserlist=[Select id,Name from User where UserRole.Name =: 'Label.Chief'];
for(user u : TLSPuserlist)
{
set<id> subord= new set<id>();
subord=Roleutils.getRoleSubordinateUsers(u.Id);
If(subord.size()>0 && subord!=NULL)
{
for(Id id1 : subordlist)
{
If(!accset.contains(id1) || !oppset.contains(id1) || conset.contains(id1) || leadset.contains(id1))
{
unediteduseridlist.add(id1);
}
}
}
}
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{''};
email.setSubject('CRM not updated this week');
String s = 'There has been no activity from the following sales people of your team: <br/>';
Integer i = 0;
for(user u : [SELECT id,firstname,lastname FROM user LIMIT 10]){
i++;
s += String.ValueOf(i) + '. ' + u.FirstName + ' ' + u.LastName + '<br/>';
}
email.setPlainTextBody(s);
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}
--------------------------------------------------------
RoleUtils CLASS:
public with sharing class RoleUtils {
public static Set<ID> getRoleSubordinateUsers(Id userId) {
// get requested user's role
Id roleId = [select UserRoleId from User where Id = :userId].UserRoleId;
// get all of the roles underneath the user
Set<Id> allSubRoleIds = getAllSubRoleIds(new Set<ID>{roleId});
// get all of the ids for the users in those roles
Map<Id,User> users = new Map<Id, User>([Select Id, Name From User where
UserRoleId IN :allSubRoleIds]);
// return the ids as a set so you can do what you want with them
return users.keySet();
}
private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {
Set<ID> currentRoleIds = new Set<ID>();
// get all of the roles underneath the passed roles
for(UserRole userRole :[select Id from UserRole where ParentRoleId IN :roleIds AND ParentRoleID != null])
currentRoleIds.add(userRole.Id);
// go fetch some more rolls!
if(currentRoleIds.size() > 0)
currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));
return currentRoleIds;
}
}
I have made following changes in your code:
1) Added a parameter to your send email method
2) Pass your list of ids to your send email method
3) add a filter to your user query
Thanks,
Fahad Akhtar
Please mark one best answer, if this has solved your problem.
Thanks,
Fahad Akhtar
In this case i need to pass the email of the id1 to sendmail alomg with the list.
How can i send the email id of the id1 user..(I need to pass the email id of id1 user to sendmail method. And then need to set it as toaddress , so that email will go to respective Team lead)
unediteduseridlist is a list of user who have not updated the CRM in last seven days, you want send email to their team lead and not to themselves is that correct or are you looking to send an email to themselves?
Thanks,
Fahad Akhtar
Code will check if any of their subordinate have not edited any record in last seven days and only then add their lead to list to send email. Check this code out.
Thanks,
Fahad Akhtar
Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(Set<String>)
Rather than passing a set<string> we can pass a simple string as we need pass single email address(TL address) at a time..I guess
setToAddresses method,this shoudl work as String and list should be same.
Thanks,
Fahad Akhtar
One more thing is like..
I have role heirarchies multiple level deep..
A
|
-----------------------------------
| | | | |
B C D E F
|
---------------------
| | |
B1 B2 B3
I have role heirarchy defined as above.
Through above code I guess I can only get olny upto one level deep.
But I need to go multiple levels deep.(n levels)
If users(B,C,D,E,F) did not edit record in last one week, mail will be sent to A. That works fine.
What i need is I need to query B1 B2 B3 ... and send mail to B as well.
A recursive call has to be made to util functiion n check should be implemented I guess..
where should I modify the code....Any suggestion./help??
This should be easy as you can query all roles
and create a map, put parentid as a key and all child as a list<ids>,loop through your all the child record for the same parent and check the same logic as you already have, you can use a where clause to only work for certain roles and also your send email code will also be in a loop of your map which will only send email to one parent role of their respective subordinate have inactivity for last seven days.
Thanks,
Fahad Akhtar
I tried modifying the code, But its not working as expected,
Could you please modify and add those couuple of lines of code if possible..Thanks a lot ...
I have created a map which will have parentroleid as a key and list of all direct subordinate ids as list, you can use map.keyset() to query all the user with parents record id and use filter role query by role name, one you query all the subrodiate you can use the same logic to identofy if they have modified any record and send a seprate email to all map keys for their suboridinates, attached is a basic sample code.
Thanks,
Fahad Akhtar
The code below (Util Class) only gives the ids of the Rolenames (All roles..multiple levels deep) ..
NOT THE USERS IDs below that Role.
Could you provide code which fetches me the users below passed Role. through the userroleid??
Thanks
Jagadish Jagz
You can do a SOQL query to using for all KeySet and Values in your map to get all the user with the roles that you have and create a map with Role id as key and user id as value, so you can get the user id using roleid directly, this should solve your problem.
Thanks,
Fahad Akhtar
I have a user Id. I want to fetch the users below him, only the ones who report to him(only 1 level deep).
Not multiple levels deep.
parentrole id works only on userrole..i guess,,
how to achieve dis?