You need to sign in to do that
Don't have an account?
EntitlementContact Inserts
Hi,
I'm stuck (real stuck) .. as to working on the following challenge. We have many accounts and each account can have several entitlements. Some even have 20 !. Now, these accounts also have multiple contacts. We have performed import of accounts, contacts and entitlements but now I want to insert all (account) contactId's with all (account) entitlementid's into the entitlementcontact object. Like for example acount Y has 2 contacts and 3 entitlements. This should give 6 rows inside Entitlement contacts. However ... some records are already there as we are onboarding another team
So I managed to create some code ... however I keep running into the following error: Before Insert or Upsert list must not have two identically equal elements
Below my complete code ... maybe i'm on the wrong trail here but ive tried several other ways. Joining data within Excel is not really within my conform zone. All support is much appreciated.
I'm stuck (real stuck) .. as to working on the following challenge. We have many accounts and each account can have several entitlements. Some even have 20 !. Now, these accounts also have multiple contacts. We have performed import of accounts, contacts and entitlements but now I want to insert all (account) contactId's with all (account) entitlementid's into the entitlementcontact object. Like for example acount Y has 2 contacts and 3 entitlements. This should give 6 rows inside Entitlement contacts. However ... some records are already there as we are onboarding another team
So I managed to create some code ... however I keep running into the following error: Before Insert or Upsert list must not have two identically equal elements
Below my complete code ... maybe i'm on the wrong trail here but ive tried several other ways. Joining data within Excel is not really within my conform zone. All support is much appreciated.
List<Id> contactIds = new List<Id>(); List<Id> EntIds = new List<Id>(); List<EntitlementContact> NewEnTlCont = new List<EntitlementContact>(); List<Account> listAccount = [select id,Name ,(select id, Product__c from Entitlements), (select id, email from contacts) from account where IsService__c = true and id in (Select Accountid from entitlement where Asset.Config_ID__c != null and TierSupportLookup__c = 'a0T25000003e0cX' and status='Active' and Status__c = 'No' and CreatedById='0052X000007vbYw') limit 500]; system.debug(LoggingLevel.Info,'*** Numb of Accounts ' + listAccount.size()); For(Account acc : listAccount) { EntitlementContact newEntCnt = new EntitlementContact(); List<Contact> lstContact = acc.contacts; for(Contact cont :lstContact) { //System.debug(LoggingLevel.Info,cont.id+'('+cont.email+')'); //** contactIds.add(cont.id); List<Entitlement> lstEntitlement = acc.Entitlements; for(entitlement entl:lstentitlement){ // System.debug(LoggingLevel.Info,'*** ContactId:'+Cont.id+', EntitlementId:'+ entl.id); contactIds.add(cont.id); EntIds.add(entl.id); newEntCnt.ContactId = cont.id; newEntCnt.EntitlementId = Entl.id; NewEnTlCont.add(NewEntCnt); } } } system.debug(LoggingLevel.Info,'*** contactIds:' + contactIds.size()); system.debug(LoggingLevel.Info,'*** EntIds:' + EntIds.size()); System.debug(logginglevel.info,'*** NewEnTlCont:'+NewEnTlCont.size()); Insert NewEnTlCont;
If you think that String key = cont.id + ':' + Entl.id ; already exists into EntitlementContact.
Set<String> setEntitlementIds = new Set<String>(); ( or Set<Id> setEntitlementIds = new Set<Id>(); ) Outside the loops :
All Answers
You should use a Map moreover :
Map<String,EntitlementContact> MapNewEnTlCont = new Map<String,EntitlementContact>();
In the loop :
newEntCnt.ContactId = cont.id;
newEntCnt.EntitlementId = Entl.id;
String key = cont.id + ':' + Entl.id ;
if ( ! MapNewEnTlCont.containsKey( key )) {
MapNewEnTlCont.put( key , newEntCnt );
}
Outside the loops :
If "Before Insert or Upsert list must not have two identically equal elements" is related to Insert NewEnTlCont; (and not a trigger elsewhere).
Jos/
Hi Alain,
I changed the code to however i'm facing the same error and I guess .. it's due to the fact that the combination ContactId and EntitlementId already exists.
EXCEPTION: System.ListException: Before Insert or Upsert list must not have two identically equal elements
STACKTRACE:AnonymousBlock: line 35, column 1
LINE: 35 COLUMN: 1....
EXCEPTION: System.ListException: Before Insert or Upsert list must not have two identically equal elements = there is trigger elsewhere probably and you need to watch at your log.
It is not the list above because all the items are different ( different keys ).
The below code works .. however ... only with small batches
So I want this to be capable to handle large amounts of contacts and not limited in batches like for example accounts like 'ABCDE%' etc.
The alternative I have is to include this inside the trigger on entitlement insert but I rather not as that would cascade DML and cause DML .. if not CPU timeout issues.....
If you think that String key = cont.id + ':' + Entl.id ; already exists into EntitlementContact.
Set<String> setEntitlementIds = new Set<String>(); ( or Set<Id> setEntitlementIds = new Set<Id>(); ) Outside the loops :
Just for anyone using this script, on the top, there's a nVar string you can use to manipulate batches like all accounts starting with 'abc%' or accounts with '%abc%'in their name. It might also be useful to limit the account selection when applicable..
By the way, I forgot Entl.id in setEntitlementIds.add( Entl.id ); // wrong cut/paste instead of copy/paste (sorry)
You have fixed it by yourself and improved the proposed solution for your need.
You just needed a little extra help or an experience feedback for the same kind of problem but I don't use EntitlementContact myself so I leant something.
setEntitlementIds.add( Entl.id );