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
SimrinSimrin 

get comon rows from 5 list and popumlate one list


Hello,

I have a list which adds the list to another list.

In one use case, tempResult gets 1 row at a time.
searchResult will have 5 rows

 
public with sharing class Controller {

    public List<ProfileSkillUser> searchResult{get;set;}

    public Controller() {
        List<ProfileSkillUser> tempResult;
        searchResult = new List<ProfileSkillUser>();
        for(integer i=0; i<5; i++){
            tempResult = new List<ProfileSkillUser>();
            tempResult = [SELECT User.FirstName, User.LastName, ProfileSkill.Name
                              FROM ProfileSkillUser WHERE ProfileSkill.Name =:i ORDER BY UserId ];
            searchResult.addAll(tempResult);
        }
    }
}


this is doing a OR kind of operand.

I now need to make a AND kind of operand.
Best Answer chosen by Simrin
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Simrin,

Please use below code.
public with sharing class Controller {

    public List<ProfileSkillUser> searchResult{get;set;}

    public Controller() {
	   
        List<Integer> tempResult = new  List<Integer>();
        for(integer i=0; i<5; i++){
			tempResult.add(i);
        }
		 Map<Id,ProfileSkillUser> mapSearchResultMap = new Map<Id,ProfileSkillUser>([SELECT User.FirstName, User.LastName, ProfileSkill.Name
                              FROM ProfileSkillUser WHERE ProfileSkill.Name in:tempResult ORDER BY UserId ]);
		
    }
}

Let us know if it helps you.
 

All Answers

Ankit AroraAnkit Arora
Make you "searchResult" type MAP and put ID as key, in that case you will always get unique records. Will this help?
SimrinSimrin

If i make searchResult as Map. Wont it owerrite the previous value. 
presently what this is doing is.

for(1st interation){
   tempResult = 1 row
}
for(2nd interation){
   tempResult = 2 row
}
for(3rd interation){
   tempResult = 1 row
}
for(4th interation){
   tempResult = 3 row
}
for(5th interation){
   tempResult = 1 row
}

searchResult = 1+2+1+3+1 (8 rows)s

What i need is. 

tempResult1 in (tempResult2 in(tempResult3) until 5

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Simran,

It will override previous value if you consider it as a map. It should find a unique key in map.

Let us know if it helps you.
SimrinSimrin
Well, I think that, Map will not resolve my issue.  Can you explaing me more how can Map be used for my use case
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Simrin,

Please use below code.
public with sharing class Controller {

    public List<ProfileSkillUser> searchResult{get;set;}

    public Controller() {
	   
        List<Integer> tempResult = new  List<Integer>();
        for(integer i=0; i<5; i++){
			tempResult.add(i);
        }
		 Map<Id,ProfileSkillUser> mapSearchResultMap = new Map<Id,ProfileSkillUser>([SELECT User.FirstName, User.LastName, ProfileSkill.Name
                              FROM ProfileSkillUser WHERE ProfileSkill.Name in:tempResult ORDER BY UserId ]);
		
    }
}

Let us know if it helps you.
 
This was selected as the best answer