+ Start a Discussion
jason.bradleyjason.bradley 

"Update failed. First exception on row 1 with id ***; first error: DUPLICATE_VALUE"

Hello,

 

I'm trying to write a batch Apex class that will generate a new record for each day between two dates that are retreived from the user with information on the day already filled in in different ways. The object, Time_Dimension__c has quite a few fields, either dates or strings for the most part, and one unique field, Date_Key_Unique__c. 

 

On the test page I've written, I have been inputting the start date as 2/28/2012 and the end date as 2/26/2013. 

The biggest problem is that every field on these records except for Name is blank. I looked at the Apex Jobs page to see what the issue was, and this is what it displays:

First error: Update failed. First exception on row 1 with id a1eg0000000CuwOAAS; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: a1eg0000000Cus3: []

 

 Here is the Apex class I've written to accomplish this:

global class TimeDimensionGenerator implements Database.Batchable<sObject>, Database.Stateful {
	
	Integer calendarStartYear {get; set;}
	Integer calendarEndYear {get; set;}
	String fiscalStartMonth {get; set;}
	String fiscalStartDay {get; set; }
	String actuarialStartMonth {get; set;}
	String actuarialStartDay {get; set;}
	String policyStartMonth {get; set;}
	String policyStartDay {get; set;}
	
	Date startDate {get; set;}
	Date endDate {get; set;}
	
	global final string query;
	
	Time_Dimension__c lastTimeDimensionOfLastScope {get; set;}
	
	List<String> monthNames = new List<String>();
	
	//Date counters
	Date currentCalendarDate {get; set;}
	
	global TimeDimensionGenerator(Integer calendarStartYear, Integer calendarEndYear, String fiscalStartMonth, String fiscalStartDay, String actuarialStartMonth, String actuarialStartDay, String policyStartMonth, String policyStartDay)
	{
		this.calendarStartYear = calendarStartYear;
		this.calendarEndYear = calendarEndYear;
		this.fiscalStartMonth = fiscalStartMonth;
		this.fiscalStartDay = fiscalStartDay;
		this.actuarialStartMonth = actuarialStartMonth;
		this.actuarialStartDay = actuarialStartDay;
		this.policyStartMonth = policyStartMonth;
		this.policyStartDay = policyStartDay;
		
		monthNames.add('January');
		monthNames.add('February');
		monthNames.add('March');
		monthNames.add('April');
		monthNames.add('May');
		monthNames.add('June');
		monthNames.add('July');
		monthNames.add('August');
		monthNames.add('September');
		monthNames.add('October');
		monthNames.add('November');
		monthNames.add('December');
		
		startDate = Date.newInstance(calendarStartYear, 1, 1);
		endDate = Date.newInstance(calendarEndYear, 12, 31);
		
		List<Time_Dimension__c> timeDims = new List<Time_Dimension__c>();
		for (Integer i = 0; i < startDate.daysBetween(endDate); i++)
		{
			Time_Dimension__c t = new Time_Dimension__c();
			timeDims.add(t);
		}
		insert timeDims;
		
		currentCalendarDate = startDate;
		
		query = selectAll('Time_Dimension__c');
	}
	
	global Database.queryLocator start(Database.BatchableContext context)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext context, List<SObject> scope)
	{
		
		List<Time_Dimension__c> timeDims = (List<Time_Dimension__c>)scope;
		if (lastTimeDimensionOfLastScope != null)
		{
			
		}
		else
		{
			
		}
		
		for (Integer i = 0; i < timeDims.size(); i++)
		{
			timeDims[i].Date__c = currentCalendarDate;
			timeDims[i].Year__c = String.valueOf(currentCalendarDate.year());
			timeDims[i].Day_Number_in_Month__c = currentCalendarDate.day();
			timeDims[i].Day_of_Week_Long__c = dayOfWeek(currentCalendarDate);
			timeDims[i].Day_of_Week_Short__c = timeDims[i].Day_of_Week_Long__c.substring(0, 3);
			timeDims[i].Long_Month_Description__c = String.valueOf(monthNames.get(currentCalendarDate.month() - 1));
			timeDims[i].Day_Full_Description__c = String.valueOf(timeDims[i].Day_of_Week_Long__c + ', ' + timeDims[i].Long_Month_Description__c + ' ' + currentCalendarDate.day() + ', ' + currentCalendarDate.year());
			timeDims[i].Date_Key_Unique__c = String.valueOf(currentCalendarDate.year() + currentCalendarDate.month() + currentCalendarDate.day());
			//timeDims[i].Month_Description__c = timeDims[i].Long_Month_Description__c.substring(0, 3);
			timeDims[i].Month_Key__c = String.valueOf(currentCalendarDate.year() + currentCalendarDate.month());
			timeDims[i].Month_Number__c = currentCalendarDate.month();
			timeDims[i].Calendar_End_Date__c = Date.newInstance(currentCalendarDate.year(), 12, 31);
			timeDims[i].Calendar_Start_Date__c = Date.newInstance(currentCalendarDate.year(), 1, 1);
			timeDims[i].Calendar_Month_Start_Date__c = Date.newInstance(currentCalendarDate.year(), currentCalendarDate.month(), 1);
			timeDims[i].Calendar_Month_End_Date__c = Date.newInstance(currentCalendarDate.year(), currentCalendarDate.month(), Date.daysInMonth(currentCalendarDate.year(), currentCalendarDate.month()));
			timeDims[i].Quarter_Number__c = getQuarterNumber(currentCalendarDate);			
			timeDims[i].Quarter_Description__c = 'Q' + timeDims[i].Quarter_Number__c;
			timeDims[i].Quarter_Key__c = currentCalendarDate.year() + timeDims[i].Quarter_Description__c;
			timeDims[i].Short_Month_Description__c = timeDims[i].Long_Month_Description__c.substring(0, 3);
			timeDims[i].Short_Month_and_Year__c = timeDims[i].Short_Month_Description__c + '0' + timeDims[i].Year__c.substring(2);
			
			
			//Last Things
			currentCalendarDate = currentCalendarDate.addDays(1);
			System.debug(currentCalendarDate);
			
			//update timeDims[i];
		}
		
		update timeDims;
		
		//Last Things
		lastTimeDimensionOfLastScope = timeDims[timeDims.size() - 1];
	}
	
	global void finish(Database.BatchableContext context)
	{
		
	}
	
	global String selectAll(string sfo){
	    String SOQL;
        map<String, schema.sobjecttype> allSObjects = schema.getglobaldescribe();
        schema.sobjecttype q = allsobjects.get(sfo);
        schema.describesobjectresult d = q.getdescribe();
        map<String, schema.sobjectfield> m = d.fields.getmap();
        set<String> s = m.keyset();
        string k = '';
        for(String f : s){
            k = k+f+', ';
        }
        k = k.substring(0,k.length()-2);
        SOQL = 'SELECT ' + k + ' FROM ' + sfo;
        
        return SOQL;
    }
    
    global String dayOfWeek(Date dt)
    {
    	
    	DateTime d = DateTime.newInstance(dt.year(), dt.month(), dt.day(), 0, 0, 0);
    	return d.format('EEEE');
    }
    
    global Integer getQuarterNumber(Date d)
    {
    	Integer qNum;
    	if (d.month() <= 3)
		{
			qNum = 1;
		}
		if (d.month() >= 4 && d.month() <= 6)
		{
			qNum = 2;
		}
		if (d.month() >= 7 && d.month() <= 9)
		{
			qNum = 3;
		}
		if (d.month() >= 10 && d.month() <= 12)
		{
			qNum = 4;
		}
		
		return qNum;
    }
}

 

Here is how I am calling the batch:

TimeDimensionGenerator gen = new TimeDimensionGenerator(dummy.Calendar_Start_Year__c.year(), dummy.Calendar_End_Year__c.year(), '', '', '', '', '', '');
		ID batchprocessid = Database.executeBatch(gen);

 

The dummy variable that is reference here is just an extra object I created to allow me to use the inputFields with the date picker and store the inputted data in one place. It's only used on the page I've created to test the generation of the TimeDimensions.

 

I've already wasted at least half a day trying to figure out what's going wrong and have yet to get anywhere, so any help from some more experienced Salesforce users would be amazing right now.

Best Answer chosen by Admin (Salesforce Developers) 
jason.bradleyjason.bradley

I turns out that I had a unique field whose value I was generating was not as unique as I had initally thought. It was unfortunate that it wasn't able to tell me which field this was from the error description, but I fixed it nontheless.