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
Sai GuptaSai Gupta 

Can we throw a validation by checking the existing records values with new values?

Hi Team,

I have a scenario that needs to through a validation using validation rules or flows. Please help me to achieve this.

I have an appointment object, where I have a start date and an end date. If any new appointment dates have already existed in the system. I have to throw an error saying already an appointment got booked on the same slot.
Best Answer chosen by Sai Gupta
Maharajan CMaharajan C
Hi Sai,

Please refer the below articles:

https://www.linkedin.com/pulse/using-before-save-flow-prevent-record-creationupdate-based-gidi/

https://www.accidentalcodersf.com/2020/06/before-save-update-flows.html

Thanks,
Maharajan.C

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sai,

As per my understanding, you would like to check if the date is already present if so then have an error added.

So there could be many ways to have this implemented but one way I could think of is you could follow the below steps:

>>You could have a trigger on that appointment object and then you could have a soql query to get the pre-existing dates and then add them into a set so that there are no duplicates.

>> now you can check if the new date is present in this created set and if it is present then you can show an error using the below line:

Trigger.new[0].addError('bad');

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be useful for others in the future.

Regards,
Anutej
Sai GuptaSai Gupta
Hi Anutej,

Thanks for the reply,

Really I appreciate your suggestion. But, I am looking to achieve this scenario through flow or validation. Is this possible?
Maharajan CMaharajan C
Hi Sai,

Please refer the below articles:

https://www.linkedin.com/pulse/using-before-save-flow-prevent-record-creationupdate-based-gidi/

https://www.accidentalcodersf.com/2020/06/before-save-update-flows.html

Thanks,
Maharajan.C
This was selected as the best answer
Sai GuptaSai Gupta
Hi Maharajan,

Thanks for the reply. It's working perfectly fine.