Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

EDIT: Is there a way where I could just loop through my list and remove the duplicates? It should be easy but I've been having trouble with it.

I have tried a bunch of different methods of getting rid of the duplicates out of the query. The problem is that it is related field where it's close to something like Contact__r.LastName. Here is my current code:

List<Object> names = new List<object>();

public List<Object__c> getNames(){
    if(names==null){
        results = [select Contact__r.LastName FROM OBJECT WHERE Field = True];
        new Set<Object> mySet = new Set<Object>();
        mySet.addAll(results);
        names.addAll(mySet);

    }
    return names;
}  //added so code could be formatted into code block

it is no longer giving me an error message but is still giving me duplicates.

Any suggestions on better ways to dedupe or why this solution isn't working would be greatly appreciated.

share|improve this question
    
where are the types of results, mySet ? Is mySet initialized to an empty set before you do addAll(); names also must be initialized to an empty list before doing addAll() –  crop1645 Aug 7 at 17:21
    
Ok there was a small problem there but I fixed that and now there's no error message but there are still duplicates. I updated my code above to show my changes –  user3858610 Aug 7 at 17:26
    
I'm not sure this actually the answer to your question - but the null object would be because no results are found. Instead, pull the results into a List and check for the size of the list to determine if action is needed (or simply iterate through) –  joshbirk Aug 7 at 17:30
    
Yeah I thought about that before but I know there are results found especially as it is working now but just giving tons of repeats. Thanks –  user3858610 Aug 7 at 17:32
    
Are you trying to be generic or just excluding the name of your object? Your method has Object__c then the other references are to Object –  Phil B Aug 7 at 17:39

4 Answers 4

After you check if names equals null you need to instantiate List like this:

if ( names == null ) {
    names = new List<Object__c>();
    // here goes the rest of the code
}

Because when you try to add all elements of mySet to names if names is null it will throw this error. Did you try to see in debug log what line of code is throwing this error?

share|improve this answer
    
That is being done, but before a recent edit, the init code wasn't in the code block. –  Jagular Aug 7 at 17:57
    
Unfortunately this isn't the problem. I have tried to instantiate in several different places just in case but it does not change the results –  user3858610 Aug 7 at 18:05
    
I saw it, then he needs to check if it has elements, because it's never null if it's instantiated. instead of if (names == null) he needs to have something like this: if ( names.isEmpty()) –  Paul Boiko Aug 7 at 18:09
    
It would really help if you could say what line of code is throwing this error. –  Paul Boiko Aug 7 at 18:18
    
When I try using isEmpty the error is on that line, saying the same thing: Attempt to de-reference a null object –  user3858610 Aug 7 at 19:44

Do you only want to do the query the first time through? If so, you probably want

if(names.isEmpty()){

instead of

if(names==null){

when I run the following code in Execute Anonymous in the Developer Console, I do see the debug message.

List<attendance__c> names = new List<attendance__c>();
if(names!=null){
system.debug('names is not null on init');
}

Your current query code runs every time that getNames is called

share|improve this answer
    
I think I just want to query the first time. However, I just tried your suggestion and got back another error saying that it's trying to de-reference null object –  user3858610 Aug 7 at 18:13
    
My third block of code works fine in "Execute Anonymous" with this swapped in for line 2 = " if(names.isEmpty()){". Are you certain there's no typos in your version? –  Jagular Aug 7 at 18:18
    
When I use your third suggestion (names!=null) I get an empty list returned because the names list starts out as empty –  user3858610 Aug 7 at 19:39
    
Please show us the code that causes the problem. I'm not sure if the code you are talking about is using "names.isEmpty()" or something else. You can edit your question to include the new code. –  Jagular Aug 7 at 19:50

Do you need a list of objects or a list of strings?

public List<String> getNames()
{
  Set<String> nameSet = new Set<String>();
  for (Object obj: [SELECT Contact__r.LastName FROM OBJECT WHERE Field = True])
  {
    nameSet.add(obj.Contact__r.LastName);
  }

  return new List<String>().addAll(nameSet);
}
share|improve this answer
    
I need a list of objects –  user3858610 Aug 7 at 19:40
2  
@user3858610 Ok, can you explain to me what you consider a duplicate then? –  dphil Aug 7 at 20:04

If you want the unique set of related contacts, then you can use a semi-join, e.g.

[select LastName FROM contact WHERE id in (select contact__c from OBJECT where Field = True)];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.