You need to sign in to do that
Don't have an account?
Sylvio Avilla
Prevent DateTime Overlap Trigger
Hello Everyone,
I would like to create a simple trigger and prevent the user to add a new record that will overlap an existing record.
This is the closest I could get to solve it:
"Invalid bind expression type of Schema.SObjectField for column of type Datetime.
Can anyone tell me whats wrong?
Thanks
I would like to create a simple trigger and prevent the user to add a new record that will overlap an existing record.
- The idea is that the same person couldn't do 2 things at the same time!
This is the closest I could get to solve it:
// referene link https://developer.salesforce.com/forums/?id=906F00000008zTcIAI // assuming the candidate is in myRec List<MyRec__c> overlaps=[select Start_Date__c, End_Date__c, Business_Type__c from MyRec__c where ( (Start_Date__c >= :myRec.Start_Date__c AND StartDate__c <= :myRec.Stop_Date__c) OR (Stop_Date__c >= :myRec.Start_Date__c AND StopDate__c <= :myRec.Stop_Date__c) OR (Start_Date__c <= :myRec.Start_Date__c AND StopDate__c >= :myRec.Stop_Date__c) ) and Business_Type__c=:myRec.Business_Type__c]; if (!overlaps.isEmpty()) { // error here }The problem is that when try to use it, receive the following error:
"Invalid bind expression type of Schema.SObjectField for column of type Datetime.
Can anyone tell me whats wrong?
Thanks
So, myRec is a custom object, witch contains the Start_Date__c, End_Date__c, Business_Type__c custom fields.
I just click "new" to create a new myRec.
Thanks again
Maybe I'm being redundant, but here is the trigger with my real custom objects ( the other code was just an example).
Keep receiving the same error
"Invalid bind expression type of Schema.SObjectField for column of type Datetime."
Sorry for the elementay question, but I'm not very good at it
Thanks again
You need to check against the records in the trigger, e.g.
This won't scale beyond 100 records though, as you have to execute the query against a single record in order to validate the date range, which is fine if you are only creating records via the UI.
I tried to simplify the code, so I could see if it would work.
The thing is that I've tried to create a new record that would meet the criteria (
Sa_da_da_Base__c = :trigger.new[0].Sa_da_da_Base__c) and generate the error, informing that is duplicated, but nothing happens!
Here I am again.
Can you help me once again? I tried do simplify the code with just one statement
Data_Hora_Chegada_Base__c =:trigger.new[0].Data_Hora_Chegada_Base__c
The problem is that its always returning something and displaying the error, even with just one record!
Thanks again
so you will only return the error if there are no overlaps, via the else clause:
Just have one last issue. When I update the record, it returns me "Overlap". There is a way to avoid it?
Thanks!