Salesforce 如果对象已存在,则不要将对象添加到对象列表中

Salesforce 如果对象已存在,则不要将对象添加到对象列表中,salesforce,apex-code,Salesforce,Apex Code,我正在使用以下方法,使用动态soql对联系人记录进行相当简单的查询: public PageReference contactSearch() { contactResultSetSize = 0; if(!String.isEmpty(firstname) || !String.isEmpty(lastname) || !String.isEmpty(company)) { string soql = 'Select firstname, lastname, a

我正在使用以下方法,使用动态soql对联系人记录进行相当简单的查询:

public PageReference contactSearch() {
    contactResultSetSize = 0;
    if(!String.isEmpty(firstname) || !String.isEmpty(lastname) || !String.isEmpty(company)) {
        string soql = 'Select firstname, lastname, account.Name, account.BillingStreet, account.BillingCity, account.BillingState, account.BillingPostalCode From Contact';
        String whereClause = ''; 

        if(!String.isEmpty(firstname)) {
            whereClause = ' Where firstname like \'%' + firstname + '%\'';
        }
        if(!String.isEmpty(lastname)) {
            if(!String.isEmpty(firstname)) {
                whereClause += ' AND lastname like \'%' + lastname + '%\'';
            }
            else {
                whereClause = ' Where lastname like \'%' + lastname + '%\'';
            }
        }
        if(!String.isEmpty(company)) {
            if(!String.isEmpty(firstname) || !String.isEmpty(lastname)) {
                whereClause += ' AND account.Name like \'%' + company + '%\'';
            }
            else {
                whereClause = ' Where account.Name like \'%' + company + '%\'';
            }
        }
        soql = soql + whereClause;

        List<Contact> searchResults = Database.query(soql);
        contactResultSetSize = searchResults.size();
        if(contactLinesForPage == null) {
            contactLinesForPage = new List<ContactWrapper>();
        }

        for(Contact c : searchResults) {
            contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
        }
    }
    return null;    
}
publicpagereference contactSearch(){
contactResultSetSize=0;
如果(!String.isEmpty(firstname)| |!String.isEmpty(lastname)| |!String.isEmpty(company)){
字符串soql='从联系人中选择firstname、lastname、account.Name、account.BillingStreet、account.BillingCity、account.BillingState、account.BillingPostalCode';
字符串where子句=“”;
如果(!String.isEmpty(firstname)){
whereClause='Where firstname like\'%'+firstname+'%\';
}
如果(!String.isEmpty(lastname)){
如果(!String.isEmpty(firstname)){
whereClause+=”和lastname,如\'%'+lastname+'%\';
}
否则{
whereClause='Where lastname like\'%'+lastname+'%\';
}
}
如果(!String.isEmpty(公司)){
如果(!String.isEmpty(firstname)| |!String.isEmpty(lastname)){
whereClause+=”和account.Name,如\'%'+公司+'%\'';
}
否则{
whereClause='Where account.Name,如\'%'+company+'%\';
}
}
soql=soql+WHERE子句;
List searchResults=Database.query(soql);
contactResultSetSize=searchResults.size();
if(contactLinesForPage==null){
contactLinesForPage=新列表();
}
对于(联系c:searchResults){
添加(新的ContactWrapper(contactLinesForPage.size(),c');
}
}
返回null;
}
我正在使用包装器类,contactLinesForPage是我的包装器对象的列表:

public List<ContactWrapper> contactLinesForPage {get; set;}
public List contactLinesForPage{get;set;}
由于用户进行多次搜索,我不想将记录重新添加到搜索结果列表中。如何检查对象中是否已存在记录,以便在搜索中不会返回重复的记录


感谢您的帮助。

只需添加一项检查,检查contactLinesForPage allready是否包含此联系人。大概是这样的:

  for(Contact c : searchResults) {
        Boolean toInsert = true;
        for(ContactWrapper cw : contactLinesForPage){
             if(cw.contact.Id == c.Id){
                 toInsert=false;
             }
        }
        if(toInsert){
           contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
        }
    }

只需添加一个检查contactLinesForPage allready是否包含此联系人。大概是这样的:

  for(Contact c : searchResults) {
        Boolean toInsert = true;
        for(ContactWrapper cw : contactLinesForPage){
             if(cw.contact.Id == c.Id){
                 toInsert=false;
             }
        }
        if(toInsert){
           contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
        }
    }

只需添加一个检查contactLinesForPage allready是否包含此联系人。大概是这样的:

  for(Contact c : searchResults) {
        Boolean toInsert = true;
        for(ContactWrapper cw : contactLinesForPage){
             if(cw.contact.Id == c.Id){
                 toInsert=false;
             }
        }
        if(toInsert){
           contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
        }
    }

只需添加一个检查contactLinesForPage allready是否包含此联系人。大概是这样的:

  for(Contact c : searchResults) {
        Boolean toInsert = true;
        for(ContactWrapper cw : contactLinesForPage){
             if(cw.contact.Id == c.Id){
                 toInsert=false;
             }
        }
        if(toInsert){
           contactLinesForPage.add(new ContactWrapper(contactLinesForPage.size(), c, ''));
        }
    }

或者你可以用地图。将ContactWrapper对象添加到地图。地图的关键是一个id。如果他们添加了一个重复的联系人,它将覆盖已经存在的联系人。你的代码就是

aMap.put(cw.id, cw);  // one line eliminates duplicates.
如果需要ContactWrappers列表,只需返回
aMap.values()


如果要抽象维护联系人集合的行为,请创建一个ContactCollection类并将实现隐藏在其中。这将为类似情况提供更可重用的模式。

或者您可以使用地图。将ContactWrapper对象添加到地图。地图的关键是一个id。如果他们添加了一个重复的联系人,它将覆盖已经存在的联系人。你的代码就是

aMap.put(cw.id, cw);  // one line eliminates duplicates.
如果需要ContactWrappers列表,只需返回
aMap.values()


如果要抽象维护联系人集合的行为,请创建一个ContactCollection类并将实现隐藏在其中。这将为类似情况提供更可重用的模式。

或者您可以使用地图。将ContactWrapper对象添加到地图。地图的关键是一个id。如果他们添加了一个重复的联系人,它将覆盖已经存在的联系人。你的代码就是

aMap.put(cw.id, cw);  // one line eliminates duplicates.
如果需要ContactWrappers列表,只需返回
aMap.values()


如果要抽象维护联系人集合的行为,请创建一个ContactCollection类并将实现隐藏在其中。这将为类似情况提供更可重用的模式。

或者您可以使用地图。将ContactWrapper对象添加到地图。地图的关键是一个id。如果他们添加了一个重复的联系人,它将覆盖已经存在的联系人。你的代码就是

aMap.put(cw.id, cw);  // one line eliminates duplicates.
如果需要ContactWrappers列表,只需返回
aMap.values()

如果要抽象维护联系人集合的行为,请创建一个ContactCollection类并将实现隐藏在其中。这将提供更可重用的东西,以及用于类似情况的良好模式