+ Start a Discussion
Michael SalgoMichael Salgo 

Custom Button Advice Regarding RESTful Web Service Call and Local Value Setting

Hi All,

I am looking to implement a custom button which will be visible on the detail page of an object - let's say the Contacts object. When this button is pressed, I want to have data go from the Contacts record through a RESTful web service call and have a unique value returned. The user would not be redirected to a new page and the returned value would be used in setting a custom field on the Contacts screen.

Is something like this possible or do I have to create a middle VisualForce layer which will take information from a referenced Contact ID, pass data along through the web service, set and save the returned value, and then redirect the user back to the referenced Contact ID?

Ideally I would like this process to work no matter the Salesforce object if at all possible.

Thanks,
-Michael
Best Answer chosen by Michael Salgo
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Micheal,

 I would suggest you to use Apex In Ajax concept to accomplish this instead of a page.,

 All you need to do is to write an Apex Class with visibility as Global and a method in it mentioned as webservice which has all the logic you need. Now from a java script based custom button on standard page, you can invoke this method and get response from that method. 
For more info, take a look at below link:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm


Hope it helps.,

Thanks,
balaji

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Micheal,

 I would suggest you to use Apex In Ajax concept to accomplish this instead of a page.,

 All you need to do is to write an Apex Class with visibility as Global and a method in it mentioned as webservice which has all the logic you need. Now from a java script based custom button on standard page, you can invoke this method and get response from that method. 
For more info, take a look at below link:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm


Hope it helps.,

Thanks,
balaji
This was selected as the best answer
Michael SalgoMichael Salgo
That's a very good solution, balaji. I would imagine that via the JavaScript button that I would create in this manner that I can set a value on the page as well?
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@ Michael,

  Thank you.,

 If i understand correctly, you are asking whether you can update a field that belongs to same record from which the button is clicked?
I personally dont prefer to scatter the logic, instead i would do a callout from the method , update the record as you need it as anway we are going to pass the record id and other necessary parameters to this method, after the logic is complete  just return the success or error message and use the  button to refresh the page if it is success so it displays the updated field value or just displays an alert with error message so that the user will be notified about the error

Hope it helps.,

Cheers :).

Thanks,
Balaji 
 
Michael SalgoMichael Salgo
Ah, very wise. Very wise indeed. Thank you, Balaji! You've been a great help!
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Glad you liked the solution! 

Cheers.,

Thanks,
Balaji