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
Janno RipJanno Rip 

Custom Button that checks through every entry beforce execute

Hello Community,

I need to create a onClick JavaScript Button that switches a custom field on my custom object to a specific value. Now here’s the trick part: It should only execute if no other related objects have already that value.

Example:

User-added image

My custom object is leadevent__c and has a lookup to account. Therefore it is a related list. I have a custom field Phase__c with a value called "in Bearbeitung". I only want to allow 1 leadevent to be in the Phase__ "in Bearbeitung" at a time. Now on the page of the leadevent I already have a button called "Akzeptieren" (Accept) that switches my Phase__c to "in Bearbeitung". However before that happens the button should query through all other leadevents who relate to that account and check if any of them is already in the Phase__c "in Bearbeitung". If so, a simple alert would be enough. If not, the Phase__c should change.

User-added image
 
Is that possible? Im a just in at the beginning of my travel in figuring out what is possible in Salesforce. Hope someone can help.

Greetings from Germany
NagendraNagendra (Salesforce Developers) 
Hi Janno,

What you could do here is use a Flow instead of a Javascript button. Flows have the ability to query the database for any additional records where the Phase is In Bearbeitung and count how many results you have. If the number of results is 0, the flow can change the phase to Offen - otherwise, it could display a warning. The flow will need to have one input variable called record Id - this is so it knows which record to update.

Once you build the flow, you can create a quick action or a button to launch it from the page layout.

Hope this helps.

Thanks,
Nagendra