1

We have a custom button to update a lookup field if it's blank on the button click. I'm using flow to update the actual value. However, when 2 users click on the button at same time, the 2nd click overrides the value, whereas we would expect the alert to show up. It works as expected if the 2nd user refreshes the page first. How can I make the code work to make sure it works all time?

{!REQUIRESCRIPT('/soap/ajax/54.0/connection.js')}

location.reload();

if('{!Audit__c.Audit_Contact__c}' == ''){
    location.replace(‘—flow URL---')
}
else{
    alert("The OMA Audit Contact is already assigned.");
}

1 Answer 1

0

Scripts like this are rendered when the page loads, not when the button is clicked. As such, you'd want to retrieve the record first:

{!REQUIRESCRIPT('/soap/ajax/54.0/connection.js')}
let auditContact = sforce.connection.retrieve('Audit_Contact__c','Audit__c',['{!Audit.Id}'])[0]
if(auditContract && !auditContract.Audit__c) {
  location.replace('flow-url');
} else {
  alert('The OMA Audit Contact is already assigned');
}
6
  • I took your suggestion and modified the script, but it now gives different errors in different browsers: Chrome: Invalid or unexpected token Safari: Invalid character '\u2018' {!REQUIRESCRIPT('/soap/ajax/54.0/connection.js')} let auditContact = sforce.connection.retrieve(‘Audit_Contact__c’,'Audit__c',[‘{!Audit.Id}'])[0] if(!auditContact) { location.replace(‘flow-url'); } else { alert('The OMA Audit Contact is already assigned'); }
    – Jay
    May 13 at 19:29
  • @Jay you gave a weird quote in your code. Make sure they are all the same. Copy-pasting from Microsoft Word can often cause this problem.
    – sfdcfox
    May 13 at 20:49
  • Sorry, if I'm missing something obvious here. Here's my updated script, and it keeps on showing the alert on the button click, even when the field is blank. ``` {!REQUIRESCRIPT('/soap/ajax/54.0/connection.js')} let auditContact = sforce.connection.retrieve('Audit_Contact__c','Audit__c',['{!Audit__c.Id}'])[0] if(!auditContact) { location.replace('flow-url'); } else { alert('The OMA Audit Contact is already assigned'); } ```
    – Jay
    May 16 at 14:13
  • @Jay I had a mistake in my logic. Should have been: if(auditContract && !auditContract.Audit__c) {
    – sfdcfox
    May 16 at 14:36
  • Thank you for the help. After updating the logic, I see no errors. However, the underlying issue still exists. When the second user clicks on the button, the field is updated with the second click whereas I would expect the alert to show up.
    – Jay
    May 16 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.