Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Custom button executing Javascript
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Custom button executing Javascript
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 01:00 PM
So I'm trying to execute Javascript from a custom button on the Event object. The alert(result) is coming back with nothing even though it should be returning my oppResult object with the oppId and errormessage. Any ideas what I'm missing, or what needs to change?
The custom button:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}
var meetingID = '{!Event.Id}';
var agree = confirm("Are you sure you want to create an opportunity?");
if(agree){
var result = sforce.apex.execute("MeetingOpportunity","createOp portunity",{meetingID: meetingID});
alert(result);
}
Class:
global class MeetingOpportunity {
webservice static OppResult createOpportunity(ID meetingID){
OppResult res = new OppResult();
//based on logic set variables in the result object
res.errorMsg = 'oh no something broke';
res.oppId = '01pW00000000000';
system.debug(res);
return res;
}
global class OppResult{
global String errorMsg;
global Id oppId;
}
}
Solved! Go to Solution.
Re: Custom button executing Javascript
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 02:09 PM - edited 12-14-2012 02:12 PM
Using http://teachmesalesforce.wordpress.com/2011/05/02/
Two thoughts - It might be that your returning a full sObject to JS doesn't work. I would try a method that returns a number or string to confirm things are working.
The other thought is looking at your Apex Class you're not inserting the Opportunity. So you're returning a non inserted sobject. That might also be causing the issue. **Edit I really should read things more carefully. Brain automatically adding words not there.**
What are you hoping to do after the button is clicked?
Re: Custom button executing Javascript
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 02:38 PM
Just change the below to use webservice keyword. It should fix your issue
global class OppResult{
webservice String errorMsg;
webservice Id oppId;
}
Kartik Viswanadha | Force.com Developer | @logontokartik | http://peregrinusforce.com
Re: Custom button executing Javascript
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 04:08 PM
Thanks Kartik, that worked. I have another question though, the alert(result) works but I tried pulling out a single field from the object by doing (result.errorMsg) but I'm getting back "undefined". Any ideas?
alert(result); alert(result.errorMsg);
Re: Custom button executing Javascript
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 06:51 PM
hmm. the sforce.apex.execute generally returns the types as arrays. So try using result[0].errMsg. this should work
Kartik Viswanadha | Force.com Developer | @logontokartik | http://peregrinusforce.com
Re: Custom button executing Javascript
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-15-2012 01:39 AM
I personnally have never liked this solution, calling a webservice from JavaScript in a button.
If you are adding a button to Salesforce (as opposed to a web page), you are already on the platform.
I suggest creating a 1-line Visualforce page with a page statement which defines an "action" which is called in the controller extensio; I do this all the time :
- if you need to collect input from the user, displays messages, etc then do it in this VF page
- reuse the controller method you have already written to do whatever processing needs to be done
- if you need to display stuff after the call, return a new PageReference to another VF page to do that stuff.
I hope this helps. Sorry, I'm on my iPad so don't have sample code at hand : search "action" attribute of the <apex:page> marker.
Rup

