Validate overlap dates before save

  • 2
  • Question
  • Updated 4 years ago
  • Answered
I am trying to validate the database records based on Start and End date of newly created record to check the overlapping of dates and to show the message if its is overlapping and further it has to allow to save the record on confirmation.

I was able to do this check for database records, but how can i check this within newly created rows whether the dates are been overlapping or not. 

Here is my code:


                        var $ = skuid.$,
                            params = arguments[0],
                            list = params.list,
                            item = params.item,
                            model = params.model;
                       
                        
                        //var $ = skuid.$;
            var save1 = false;
                     
                        $(function(){
                        salescallM = skuid.model.getModel('SalesCallModel');
var rowIdsThatChanged = Object.keys(skuid.model.getModel('SalesCallModel').changes);          

                        var currentscRow = salescallM.getFirstRow();
                        var newstartdt = currentscRow.Start__c;
                        var newdt = new Date(skuid.time.parseSFDateTime(newstartdt).setMinutes(skuid.time.parseSFDateTime(newstartdt).getMinutes() + currentscRow.Duration_in_Minutes__c));      
                        var newEndDt = skuid.time.getSFDateTime(newdt);
            var newDurinMin = currentscRow.Duration_in_Minutes__c;
            var owner = currentscRow.OwnerId;
           
            console.log('NEWDur:'+ newDurinMin+ ' NStdt :'+ newstartdt+ ' NEndt:'+newEndDt);
           
           
            var result = '';
            //Database level overlap check
                        salescall = skuid.model.getModel('SalesCall');
           
            var condition1 = salescall.getConditionByName('Start__c');
                            salescall.setCondition(condition1, newstartdt);
                            salescall.activateCondition(condition1);
           
            var condition2 = salescall.getConditionByName('Calculated_End__c');
                            salescall.setCondition(condition2, newstartdt);
                            salescall.activateCondition(condition2);
           
            var condition3 = salescall.getConditionByName('Start__c1');
                            salescall.setCondition(condition3, newstartdt);
                            salescall.activateCondition(condition3);
           
            var pageTitle = $('#SalesCallRecs');
            var editor = pageTitle.data('object').editor;
           
//if (rowIdsThatChanged = '' ) //No changes made on row - to avoid saving on click of save without doing any action
if(rowIdsThatChanged.length > 0 && rowIdsThatChanged != '')
{
$.each(salescall.getRows(),function(){
var startdt = this.Start__c;
var enddt = this.Calculated_End__c;
var name = this.Name;

if((startdt >= newstartdt && (startdt <= newEndDt && startdt < newEndDt)) || ((enddt >= newstartdt && enddt > newstartdt) && enddt <= newEndDt) ||(startdt <= newstartdt && enddt >= newEndDt))
{
save1 = true;            
}                       
});

            if(save1 == true)
            {
           
result = confirm("Sales Call exists with the same time slot. Do you still want to continue?");
            /*editor.handleMessages([{
                                        message: 'WARNING: Sales Call exists with the same time slot. Click Save to Continue',
                                        severity: 'WARNING'
            },]);*/
if(result == true)
{
salescallM.save();
}
else if(result == false)
{
salescallM.cancel();
}
}
            if(save1 == false) //to handle delete rows
            {          
            salescallM.save();
}
            }
                        });
                        
                        
Photo of Pavithra

Pavithra

  • 180 Points 100 badge 2x thumb

Posted 4 years ago

  • 2
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You can do the same thing you did for the database model. Once a user enters new rows, they exist in the model, so you can use the same snippet you used for the "salescall" model. Like this, just change the variables to match the other model :

$.each(salescallM.getRows(),function(){			var startdt = this.Start__c;	
var enddt = this.Calculated_End__c;
var name = this.Name;
if((startdt >= newstartdt && (startdt <= newEndDt && startdt < newEndDt)) || ((enddt >= newstartdt && enddt > newstartdt) && enddt <= newEndDt) ||(startdt <= newstartdt && enddt >= newEndDt)){
save1 = true;        
}                      
});
Photo of Pavithra

Pavithra

  • 180 Points 100 badge 2x thumb
Thank you for your inputs, it is truly valuable. But is there a way to loop through only newly created rows instead of looping through the entire model (salescallM model) records. I don't want to take the entire salescallM model record to loop through for the check, i want to take newly created rows only. How can i identify those newly created rows before saving it to database.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
This is kind of a hack.. but you can check if
row.CreatedDate === (null || undefined)
It should be null if the row hasn't saved yet. You could probably also check if the length of the Id field is 18 characters.