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

First exception on row 40; first error: FIELD_INTEGRITY_EXCEPTION, Event duration cannot be negative



i have a class which reads through all appointments from my custom object appointment__c and creates an event in SF. Things work fine on the sandbox and when i move it to production i get the below error.


Failure Message: "System.DmlException: Insert failed. First exception on row 40; first error: FIELD_INTEGRITY_EXCEPTION, Event duration cannot be negative: [DurationInMinutes]", Failure Stack Trace: "Class.SyncWithEvents.transfer: line 31, column 7 Class.TestSyncWithEvents.TestappSyncWithEvents: line 25, column 6 External entry ...


Public Class SyncWithEvents
    List<Appointment__c> app= new List<appointment__c>();
    public Integer no_of_appts {get;set;}
    List <Event> LstEvent= new List<Event>();
    public SyncWithEvents()
    public void transfer()
     app=[select StartDateTime__c,EndDateTime__c,Status__c,Client__c,OwnerId   from Appointment__c 
                    where Sync_with_events__c=false];
                    System.debug('No of records+++++++++++++++++++++'+app.size());
          for (integer i=0;i<app.size();i++)
       // for(List<Appointment__c> app:[select StartDateTime__c,EndDateTime__c,Status__c,Client__c,OwnerId  from Appointment__c 
       //             where Sync_with_events__c=false])
                        Event e = new Event();
                        e.StartDateTime = app[i].StartDateTime__c;
                        e.EndDateTime = app[i].EndDateTime__c;
                        e.Subject = 'Appt - '+app[i].Status__c;
                        e.OwnerId=app[i].OwnerId ;
      insert LstEvent;
      update app;

 Here is the test class


public with sharing class TestSyncWithEvents {
	static testMethod void TestappSyncWithEvents() 
    	/*appointment__c a = new appointment__c();
        string year = '2011';
        string month = '12';
        string day = '27';
        string hour = '09';
        string minute = '30';
        string second = '00';
        string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute +  ':' + second;

        Datetime myDate =    datetime.valueOf(stringDate);
        a.Name='Test Name';
        a.Start_Time__c='09:30 AM';
        insert a;*/
    	SyncWithEvents sw= new SyncWithEvents();


 Any ideas on what could be causing the problem


well it seems the start datetime and the end datetime you are inserting for appointment is getting in conflict and hence end datetime is supposedly earlier than start datetime making the duration negative . so the error


try something like




Sometime the error happens due to the conflict with GMT


hope that helps.



Pragadheeshwari APragadheeshwari A

In your production organization, you may not have the correct appoinment data. Better to have validation rule for both the fields that is start date and time should be less than end date and time.

SFDC coderSFDC coder
hi sam,

i am setting the start and end date as below;

Time strtTime=Time.newInstance(0,0,0,0);
Time endTime=Time.newInstance(0,0,0,0);
as i need to make it a full day event.

Still i get an error:

NUMBER_OUTSIDE_VALID_RANGE, Event duration cannot be negative: [DurationInMinutes]

Please help