0

EDIT:

I have a standard button added to the salesforce standard page, what I want to do when the user clicks on the buttons the following:

1) alert with the message saying " are you sure you want to run "
   a. if the user ok then execute the trigger
   b. if the user cancel then do nothing.

Is there any way to set up an alert message when a user clicks the button on the Contact object, so when the user clicks on the button, user should get an alert with [ok] and [cancel] if the user click on [cancel] nothing happens but if the user clicks on [ok] then I have to fire trigger event.

  • 1
    Could you please be more specific? – sfdcfox Sep 7 '16 at 17:49
  • Updated please have a look at it. – Nick Kahn Sep 7 '16 at 18:00
  • Do you mean a custom button you created (Setup > Customize > Contacts > Custom Buttons, Links, and Actions), or a standard button like New, Edit, Delete, Sharing? – sfdcfox Sep 7 '16 at 18:03
  • Custom Button or Link created with Behavior - Javascript – Nick Kahn Sep 7 '16 at 18:04
  • Do you just want to run an empty update? Are you looking for the window.confirm method? How is this not just a pure JS question? – Adrian Larson Sep 7 '16 at 18:07
3

At a basic level, you can use JavaScript, as follows:

{!RequireScript('/soap/ajax/37.0/connection.js')}
{!RequireScript('/soap/ajax/37.0/apex.js')}

if(confirm('Are you sure?')) {
    var result = sforce.apex.execute('MyClass','MyMethod',{ recordId: '{!Contact.Id}' });
    window.top.location.reload();
}

See the documentation for more information.


You could also perform a direct DML operation:

{!RequireScript('/soap/ajax/37.0/connection.js')}
var record = new sforce.SObject('Contact');
record.Id = '{!Contact.Id}';
if(confirm('Are you sure?')) {
    var result = sforce.connection.update([record]);
    window.top.location.reload();
}
|improve this answer|||||
  • Although this approach requires you to make your methods global, right? Couldn't you just do an empty update instead and avoid that? – Adrian Larson Sep 7 '16 at 19:08
  • @AdrianLarson A decent point. There's several ways to do the same thing, it's mostly a matter of preference. Global/public doesn't make any difference unless we're talking about putting out a managed package. I've included the empty-update method as well. – sfdcfox Sep 7 '16 at 19:17
  • Well, you have to make it webservice too, which I missed. Doesn't have any impact though? Can't your code then be called from a broader array of contexts? I always thought it would weaken your security somewhat to make methods global, depending on what actions they perform. – Adrian Larson Sep 7 '16 at 19:18
  • @AdrianLarson There's really no practical difference to a beginning developer. Once you start dealing with managed packages, then you have to worry about the limitations imposed by global. – sfdcfox Sep 7 '16 at 19:38
  • So basically you just worry about being tied to a specific implementation? Is that it? – Adrian Larson Sep 7 '16 at 19:39
0

There are a couple of ways you could accomplish this depending on what you want to do when the button is pressed. I have used javascript for easy field updates on the record, and creating a javascript window with ok/cancel buttons shouldn't be too hard. Start Here: http://www.cloudsocius.com/salesforce-opportunity-custom-button/

If you want to get more complex, such as submitting a record for approval. I recommend you look at using flowdesigner. A flow can store information, submit records, create new records, etc.

|improve this answer|||||
  • Link only answers are frowned upon here, and often get flagged for deletion. Can you add the relevant content to your post? – Adrian Larson Sep 7 '16 at 19:12

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.