Error:
Developer script exception from . : ConsumerBookingCreate : ConsumerBookingCreate: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: 0011600001oyhaS: [] Class.ConsumerBookingCreateHelper.createBooking: line 57, column 1 Trigger.ConsumerBookingCreate: line 2, column 1
Getting the error at these lines:
if ( insertAccountList != null )
{
System.debug('About to insert accounts. ' + insertAccountList);
insert insertAccountList;
System.debug('AFTER insert of person accounts');
}
Trigger:
trigger ConsumerBookingCreate on Case (after insert) {
ConsumerBookingCreateHelper.createBooking(trigger.new);
}
Apex Class:
public with sharing class ConsumerBookingCreateHelper {
public static void createBooking(List<case> cases){
List<account> insertAccountList = new List<account>();
List<booking__c> insertBookingList = new List<booking__c>();
Map<Id, Account> caseToAccountMap = new Map<Id, Account>() ;
Set<String> facilityIds = new Set<String>();
Map<String, Facility__c> facilityMap = new Map<String, Facility__c>();
Set<Decimal> userIds = new Set<Decimal>();
Map<Decimal, User> userMap = new Map<Decimal, User>();
RecordType persRecordType;
List<RecordType> persRecordTypes = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Account' and IsPersonType = true ];
if ( persRecordTypes != null && persRecordTypes.size() > 0 )
{
persRecordType = persRecordTypes[0];
}
else
{
persRecordType = new RecordType(id='012G00000012BAJ');
}
for(Case c : cases) {
if (c.Phantom_Case__c == true && c.Origin == 'Web') {
Account newA = new Account();
if (c.Last_Name_WTC__c != null) {
newA.LastName = c.Last_Name_WTC__c;
} else {
newA.LastName='No Last Name';
}
newA.RecordTypeId = persRecordType.Id;
newA.RecordTypeId = '012G00000012BAJ';
newA.FirstName = c.First_Name_WTC__c;
newA.LastName = c.Last_Name_WTC__c;
newA.Phone = c.SuppliedPhone;
newA.Alternate_Phone__c = c.Alternate_Phone_WTC__c;
newA.PersonEmail = c.SuppliedEmail;
newA.Partner_ID__c = c.Partner_WTC__c;
newA.Partner_Website__c = c.Partner_Website__c;
newA.Original_Booking_Case__c = c.Id;
newA.Consumer_Id__c = c.Consumer_Id_WTC__c;
insertAccountList.add(newA);
// set up the facility Ids
facilityIds.add( c.facility_external_id_WTC__c );
// set up the user Ids
userIds.add( c.SpareFoot_Booking_Agent_Id_WTC__c );
}
}
System.debug('Testing for accounts.') ;
if ( insertAccountList != null )
{
System.debug('About to insert accounts. ' + insertAccountList);
insert insertAccountList;
System.debug('AFTER insert of person accounts');
}
// get the facility Ids
if ( facilityIds != null && facilityIds.size() > 0 )
{
List<Facility__c> facilities = [SELECT id, name, Facility_Id__c, Account__r.Id FROM Facility__c WHERE Facility_Id__c IN : facilityIds ];
if ( facilities != null && facilities.size() > 0 )
{
for ( Facility__c f : facilities )
{
facilityMap.put( f.Facility_Id__c, f );
}
}
}
// get the user Ids
if ( userIds != null && userIds.size() > 0 )
{
List<User> users = [SELECT id, name, Booking_Agent_Id__c FROM User WHERE Booking_Agent_Id__c IN : userIds ];
if ( users != null && users.size() > 0 )
{
for ( User u : users )
{
userMap.put( u.Booking_Agent_Id__c, u );
}
}
}