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

move nested SOQL out of for loop
Public void mymethod(set<id> subord1,map<id,user> usersMap)
{
for(id subid :subord1)
{
Id roleId=usersMap.get(subid).userRoleId;
user u1=usersMap.get(subid);
List<user> users;
if(roleId!=null)
{
users = [select Id from User where UserRoleId IN (select Id from UserRole where ParentRoleId = :roleId)];
}
}
}
I want to move the SOQL out of for loop---Or change it into single SOQL.
Thanks in adv.
Laxman
{
for(id subid :subord1)
{
Id roleId=usersMap.get(subid).userRoleId;
user u1=usersMap.get(subid);
List<user> users;
if(roleId!=null)
{
users = [select Id from User where UserRoleId IN (select Id from UserRole where ParentRoleId = :roleId)];
}
}
}
I want to move the SOQL out of for loop---Or change it into single SOQL.
Thanks in adv.
Laxman
if I understand correctly, this could be solution to your problem (without nested soql):
You can try something like -
Hope, it will help you.
Thanks,
Sumit Kumar Singh
The following is my actual method:
Public void mymethod(set<id> subord1,map<id,user> usersMap1)
{
List<id> unediteduseridlist1 = new List<id>();
for(id subid :subord1)
{
Id roleId=usersMap.get(subid).userRoleId;
user u1=usersMap.get(subid);
List<user> users = [select Id from User where UserRoleId IN (select Id from UserRole where ParentRoleId = :roleId)];
system.debug('Number of Subordinate Users :' + users.size());
Set<Id> resultIds = (new Map<Id,SObject>(users)).keySet();
system.debug('size of set-------------'+resultIds.size());
List<user> lst_TeamLead1 = new List<user>();
If(resultIds .size()>0 && resultIds !=NULL)
{
for(Id Id2 : resultIds ){
user uu1=usersMap.get(id2);
If((uu1.Last_Edited_Object_Date__c < d || uu1.Last_Edited_Object_Date__c==NULL) && (uu1.Last_Activity_Date__c < d ||uu1.Last_Activity_Date__c==NULL))
{
unediteduseridlist1.add(Id2);
lst_TeamLead1.add(u1);
}
}
If(unediteduseridlist1.size()>0 && unediteduseridlist1 !=NULL)
sendmail(unediteduseridlist1,lst_TeamLead1);
unediteduseridlist1.clear();
}
}
}
I am not ablr to use the u1 which i need to pass to other method in this case..
This method takes subord1 and checks if the users have users below them in role hierarchy and then verified if they modified any records in last n days. If not modifeid they will be added to list and sent to sendmail method which sends mail to owners(u1).
Getting an error saying there is no field named ParentRoleId on user object.
Map<Id, User> users = new Map([select Id, ParentRoleId from User where UserRoleId IN (select Id from UserRole where ParentRoleId in :roleIds]);
Thanks.
Laxman
I just wanted to give you an idea of what you need. And you're correct, ParentRoleId does not exist for User, so you will need to somehow create that relationship. I suggest you take a cleaner approach using recursion:
http://salesforce.stackexchange.com/questions/924/is-there-a-way-to-query-role-hierarchy
Once you get that code snippet in place, you can associate your Parent Roles with their respective suboordinates:
What do you think?