从包装器对象salesforce中删除重复的帐户

时间:2015-05-12 14:06:35

标签: set duplicates salesforce wrapper apex

我正在尝试从包装器对象中删除重复的帐户。 但无法得到那个。 这是我的顶级课程。请帮我删除重复的帐户添加到列表中。 此行正在添加重复的帐户ID。

allaccts.add(new AccountWrapper(objContact.account.name,objContact.AccountId));

Public with sharing class Customaccountlookupcontroller{

Public list<Customaccountlookupcontroller.AccountWrapper> allaccts{get;set;}
public string searchString{get;set;}
Public list<account> results{get;set;}
Public contact objcontact{get;set;}
Public list<id> accids;

public class AccountWrapper {
public String name {get; set;}
public Id accountId {get; set;}
public AccountWrapper(String name, String accountId) {
  this.name = name;     this.accountId = accountId;
  }
}
public Customaccountlookupcontroller() {
allaccts =  new list<Customaccountlookupcontroller.AccountWrapper>();
user u= [select id,contactId  from user  where id=:userinfo.getuserID()];
if(u.contactID !=null)
{
 Contact objContact =[select AccountId,Account.name,contact.name,(select AccountId,account.name from AccountContactRoles) from Contact where Id=: u.contactID];   
 for(AccountContactRole obj: objContact.AccountContactRoles)
 {   
   allaccts.add(new AccountWrapper( obj.account.name, obj.AccountId ));
   allaccts.add(new AccountWrapper( objContact.account.name, objContact.AccountId ));
    }
       }
 System.debug('*******objcontact:'+allaccts);
         }
       }

public string getFormTag() {
return System.currentPageReference().getParameters().get('frm');
}

public string getTextBox() {
return System.currentPageReference().getParameters().get('txt');
}    
}

1 个答案:

答案 0 :(得分:0)

您可以在包装器“等于”和“ hashCode”中定义两个方法,并使用要保持唯一性的包装器数据成员。在下面的代码中,我在您的代码中有“姓名”,您可以使用“ accountid”

public class TestWrapper{
    public String name {get; set;}
    public Integer val {get; set;}
    public TestWrapper(String name, Integer val) {
        this.name = name;     
        this.val = val;
    }
    public boolean equals(Object obj) {
        TestWrapper other=(TestWrapper) obj;
        return this.name==other.name;
    }

    public Integer hashCode() {
        return name.hashCode();
    }
}

代替列表使用包装器集:

public TestData(){
    Set<TestWrapper> tlist = new Set<TestWrapper>();
    tlist.add(new TestWrapper('A',1));
    tlist.add(new TestWrapper('B',1));
    tlist.add(new TestWrapper('C',1));
    tlist.add(new TestWrapper('A',1));
    System.debug(tlist);
}

希望它能起作用!!!!