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
Matthew HamptonMatthew Hampton 

Duplicate ID in List - HELP PLEASE

Can anyone take a look at the code written below and help me re-write it so that I avoid getting the error message "Duplicate ID in list at Line 23, Column 69".

 

 

trigger FiberUpdate on Fiber_Qualified_Address__c (after insert, after update) {
Set<String> AddressSet = new Set<String>();
Set<String> LocSet = new Set<String>();
Set<String> ZipSet = new Set<String>();
                    for(Fiber_Qualified_Address__c s: Trigger.New)
                             {
                                      AddressSet.add(s.street_address__c);
                                      LocSet.add(s.loc__c);
                                      ZipSet.add(s.zip_code__c);
                              }
                              
list<Subscriber__c> gpon = [select id, name, fiber_address__c, street_address__c, loc__c, zip_code__c from Subscriber__c where street_address__c IN: AddressSet
and loc__c IN: LocSet
and zip_code__c IN: ZipSet];

        if(gpon.size()>0) 
        { List<Subscriber__c> toUpdate=new List<Subscriber__c>(); 
        Map<ID, String> FQAID = new Map<ID, String>(); 
        for(Subscriber__c FQAID2: gpon) { Map<String, Subscriber__c> FQAMap = new Map<String, Subscriber__c>(); 
        for(Subscriber__c foa: gpon) { FQAMAP.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_code__c, foa); } 
        for(Fiber_Qualified_Address__c s: Trigger.new) { if(FQAMAP.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_code__c)) 
        { Subscriber__c cand = FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c); 
        cand.fiber_address__c = s.id; toUpdate.add(cand); } } update toUpdate; }
}
}

 

Any help you can provide is appreciated.

 

Thanks in advance,

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Are you just doing:

 

 

update toUpdate;

 

If so, change to:

 

 

update toUpdate.values();

 

 

 

 

 

All Answers

bob_buzzardbob_buzzard

According to the docs, Lists cannot contain two instances of an sobject with the same id. It looks like you are adding the same subscriber to the toUpdate list.

 

There are a number of ways to solve this - one is to change toUpdate to a Map<Id, Subscriber__c> and put each matching subscriber into the map based on its id. That way, if the subscriber already exists, it will just be overwritten.  You can then execute the update on toUpdate.values().

Matthew HamptonMatthew Hampton

Bob:

 

I made the change on toUpdate to a Map<ID, Subscriber__c>. I understand why it is necessary for this, thanks for your help.

 

Now...when I save the code, I am getting an error:

 

 "Description Resource Path Location Type Save error: Method does not exist or incorrect signature: [MAP<Id,Subscriber__c>].add(SOBJECT:Subscriber__c) FiberUpdate.trigger /TrigUp/src/triggers line 23"

 

Looks like this is on the part of the code toUpdate.add(cand).

 

Thoughts? Help?

 

Thanks again,

 

Matt

bob_buzzardbob_buzzard

Rather than doing :

 

 

toUpdate.add(cand);

 

 

you'll need:

 

 

toUpdate.put(cand.id, cand);

 

 

Matthew HamptonMatthew Hampton

Made that change...now new problem...

 

"Description Resource Path Location Type Save error: DML requires SObject or SObject list type: MAP<Id,Subscriber__c> FiberUpdate.trigger /TrigUp/src/triggers line 23"

I beleive this is on the cand.fiber_address__c = s.id portion.

 

I really do appreciate your help. I am taking a class from SF in June on code writing. Not quite proficient yet.

 

Thanks,

 

Matt

bob_buzzardbob_buzzard

Are you just doing:

 

 

update toUpdate;

 

If so, change to:

 

 

update toUpdate.values();

 

 

 

 

 

This was selected as the best answer
Matthew HamptonMatthew Hampton

I cannot thank you enough for the help. Much appreciated.