Update record if some conditions are met

  • 1
  • Question
  • Updated 4 years ago
  • Answered
Hello All,
I have an issue where users are starting to change reservation dates for existing equipment reservations, this has the potential to cause double booking.

I would like to implement an equipment conflict field in the resvmodel that is updated if there is a reservation conflict after a change of the datetime fields. I believe it is possible to do this before saving the record.  Can someone confirm?  

Conditions below are evaluated after a change in the start or end datetime fields. If true I would like the conflict field to be updated with the reservation ID of the conflicting reservation, a popup notice and maybe even change the table row color.

(resvmodel1 record id != resvmodel2 record id) & (resvmodel1 eqip_id == resvmodel2 eqip_id)
     resvmodel1 reservation start < resvmodel2 reservation end
     resvmodel1 reservation end > resvmodel2 reservation start

What is the best practice for implementing this.  Thanks
Photo of Denya Lloyd

Denya Lloyd

  • 570 Points 500 badge 2x thumb

Posted 4 years ago

  • 1
Photo of Denya Lloyd

Denya Lloyd

  • 570 Points 500 badge 2x thumb
Any chance that I can get a lil help with this today guys?  I would be more than happy to provide access if you need to see the models.   Thanks
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You can have a snippet that looks something like this that runs as an action after every model update. I didn't totally get your logic but it should be a headstart.

var params = arguments[0],	$ = skuid.$;
var model1 = skuid.$M('YourModelName1');
var row1 = model1.getFirstRow();
var model2 = skuid.$M('YourModelName2');
var row2 = model2.getFirstRow();
if(row1.Id != row2.Id) && (row1.eqip_id__c == row2.eqip_id__c){
if(row1.Start__c < row2.End__c){
// note that model1 & row1 are getting updated, technically you can update any model you want
model1.updateRow(row1,{
Conflict_Field__c: row2.Id // or however you want to do it 
});
}else if(row1.End__c > row2.Start__c){
// note that model1 & row1 are getting updated, technically you can update any model you want
model1.updateRow(row1,{
Conflict_Field__c: row2.Id // or however you want to do it 
});
}
}
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,026 Points 20k badge 2x thumb
Denya I'm not exactly sure what you are trying to do. 
If you are trying to evaluate for equipment conflicts before save actions occur -  that's going to require you to have all other events for that equipment in memory for you to evauate.  That might start getting difficult.   Moshe is laying out an initial strategy 

Another alternative would be to do this evaluation server side using validation rules in Salesforce.  Save would be prevented if there were equipment conflicts.  This would keep your page size more highly managable. 
Photo of Denya Lloyd

Denya Lloyd

  • 570 Points 500 badge 2x thumb
Thank you both for the suggestions.  Rob, I just need a script that listens for changes to the date fields, checks for conflicts based on some conditions and updates a field in the row if it finds one.  I am inclined to try what Moshe suggested, not too familiar with saleforce serverside rules.  Can any of you explain how i could use $.each function to iterate through the rows of both model1 & Model2 to compare date fields. (Both models have identical data).  Thanks
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You can use a nested for each loop to compare the data from the 2 models. Row1 and row2 will refer to each row in the loop:

var params = arguments[0],	
$ = skuid.$;
var model1 = skuid.$M('YourModelName1');
var model2 = skuid.$M('YourModelName2');
$.each(model1.data, function(i, row1){
	$.each(model2.data, function(j, row2){
		if(row1.Start__c < row2.End__c){
			//do update stuff here
		}else if(row1.End__c > row2.Start__c){
			//do other update stuff here
		}
	});
});;
Photo of Denya Lloyd

Denya Lloyd

  • 570 Points 500 badge 2x thumb
Thanks Moshe.  I'm going to give it a go and update you guys on how it works out