1

Could anyone help on this issue.

In the below apex class I have method called getWrapAccounts().In this method for loop allows duplicate values.I have a Login__c field,It is emailID based on thid id how can I avoid duplicate emails entering into loop.

public with sharing class WebinarDetailsExtension {

    public String pageId = Apexpages.currentPage().getParameters().get('id'); 
    public List<Webinar__c> ws = new List<Webinar__c>();
    public List<WebinarAttendeesWrapper> WebinarAttendeesWrapperList = new List<WebinarAttendeesWrapper>(); 
    public String popupvalue{get; set;}  
    list<Webinar_Attendees__c> selectedContacts = new list<Webinar_Attendees__c>();


    public WebinarDetailsExtension(Apexpages.StandardController stdcController){        
        Webinar__c w = [select id,Name,Start_Time__c,Finish_Time__c,Registration_URL__c,Expired__c from Webinar__c where id = : pageId];        
        eventid = w.SCO_ID__c;
        eventName = w.Id;       
        ws.add(w);                        

    }

    //Get list of Attendees of the Webinar
    public List<WebinarAttendeesWrapper> getWrapAccounts() {                     
        List<Webinar_Attendees__c> ld =  [select Id,Name,principal_id__c,Login__c,isLead__c,Attandance_Status__c,Duration_Spend__c,Company_Name__c,City__c,
                                          State__c,Country__c,Opportunity__c,Mobile_Phone__c,WebinarContact__c,Salesforce_Lead__c,Email_Sent__c,Voucher_Number__c,
                                          Voucher_Number__r.Name from Webinar_Attendees__c where Webinar__c = : pageId AND CreatedDate <= TODAY 
                                          ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT : limitsize OFFSET : count ];          

        // this prints two records          
        system.debug('list of attendees:: '+ ld);                   


        //Here "Login__c" is an email address.only unic email id's enterig into loop.


        for (Webinar_Attendees__c aa : ld) { 

            //how to set a condition for to avoid duplicates
            If(Login__c){

                WebinarAttendeesWrapperList.add(new WebinarAttendeesWrapper(aa));                                                                 
            }
        }  
        //This prints 6 records ,each record prints 2 times.duplicates are added
        system.debug('WebinarAttendeesWrapperList 2:: '+  WebinarAttendeesWrapperList);                         
        return WebinarAttendeesWrapperList;
    } 

    //Wrapper class
    public class WebinarAttendeesWrapper {            
        public Webinar_Attendees__c acc {get; set;}
        public Boolean selected {get; set;}

        public WebinarAttendeesWrapper(Webinar_Attendees__c a){
            acc = a;
            selected = false;
        }      
    }    

}

2 Answers 2

1

just a change in above answer

List<String> uniqueEmailIdList = new List<String>();

for (Webinar_Attendees__c aa : ld) { 

    If(!uniqueEmailIdList.contains(aa.Login__c)){
    uniqueEmailIdList.add(aa.Login__c );
    WebinarAttendeesWrapperList.add(new WebinarAttendeesWrapper(aa));                                                                 

}
2
  • Welcome to SFSE! Please take the tour and read How to Answer. What you have right here is what we call a code dump. It's a block of code with no explanation. Code dumps tend to be poor answers because they don't help people understand what the issue is, or why the fix works. They also tend to generate a lot of back-and-forth in the comments like "that didn't work" and "try this instead" which makes it hard for other people with similar questions to follow. If you can explain why this works, or at the very least how it's different than the other answer, that would help improve your answer.
    – Derek F
    Aug 27, 2019 at 19:17
  • You can make changes to your answer by making an edit.
    – Derek F
    Aug 27, 2019 at 19:17
0

You should maintain a list containing unique e-mail ids and before adding items in WebinarAttendeesWrapperList. Check if that list contains e-mail id or not before populating WebinarAttendeesWrapperList.

Something like this

List<String> uniqueEmailIdList = new List<String>();
for (Webinar_Attendees__c aa : ld) { 

        //how to set a condition for to avoid duplicates
        If( !uniqueEmailIdList.contains(Login__c)){//If uniqueEmailIdList does not contain the e-amil id then udpate WebinarAttendeesWrapperList
             uniqueEmailIdList.add( Login__c );//Add the new e-mail id to uniqueEmailIdList so WebinarAttendeesWrapperList is not added with object having the duplicate e-mail id
             WebinarAttendeesWrapperList.add(new WebinarAttendeesWrapper(aa));                                                                 
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .