Monday, May 18, 2015

Taking control of Custom Action in Salesforce1

I have been involved with a few projects that incorporated Visualforce pages into Salesforce1's custom action since the launch of Salesforce1, and one thing I had found annoying was that there was no documentation on how one can take over the action associated with "Submit" and "Cancel" button on top of the custom action page.



However, Salesforce has recently updated their documentations on how you can add your own action to these two buttons using Publisher SDK.  I first saw this on Visualforce Mobile Trailhead, but they have updated Salesforce1 Mobile App Developer Guide to include it as well.

To add your own Javascript action to the "Save" button, you would first include Publisher SDK:


<!-- Publisher SDK -->

<script type='text/javascript' src='/canvas/sdk/js/publisher.js'></script>

Then, you would modify following Javascript function inside your Visualforce page.

// When the panel is displayed, enable the submit button
Sfdc.canvas.publisher.subscribe({name: "publisher.showPanel", onData:function(e) {
    Sfdc.canvas.publisher.publish(
        {name: "publisher.setValidForSubmit", payload: "true"});
}});

// Action to be performed when submit button is pressed
Sfdc.canvas.publisher.subscribe({ name: "publisher.post", onData: function(e) {

    // ***************************************************
    // Add your own action here
    /* myOwnAction([params], 
          // Callback
          function(error, records) {
             if (error) {
               // Do something on error
             }
             else {
               // Close the window
               Sfdc.canvas.publisher.publish(
                { name: "publisher.close", payload:{refresh:"true"}});
             }
       }); */
}});

No comments:

Post a Comment