function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SeattleSFDCSeattleSFDC 

Custom button executing Javascript

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","createOpportunity",{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;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

Just change the below to use webservice keyword. It should fix your issue

 

 global class OppResult{
        webservice String errorMsg;
        webservice Id oppId;
    }

All Answers

Salesforce WizardSalesforce Wizard

Using http://teachmesalesforce.wordpress.com/2011/05/02/call-apex-code-from-a-custom-button/ it looks like your js code is fine.

 

 

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?

 

logontokartiklogontokartik

Just change the below to use webservice keyword. It should fix your issue

 

 global class OppResult{
        webservice String errorMsg;
        webservice Id oppId;
    }
This was selected as the best answer
SeattleSFDCSeattleSFDC

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); 

 

logontokartiklogontokartik

hmm. the sforce.apex.execute generally returns the types as arrays. So try using result[0].errMsg. this should work

@altius_rup@altius_rup

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

Paddu SmileyPaddu Smiley
is manadatory to use apex in custom buttons to update some fields??
Ravikant Ravikant 10Ravikant Ravikant 10
@Paddu it's not mandatory to use apex in custom buttons rather you can go for javascript custom button to perform update for some fields. Kindly refer this link: http://www.jitendrazaa.com/blog/salesforce/create-and-update-records-using-javascript-button-in-salesforce-ajax-toolkit/.

Hope it clears your doubt.
Paddu SmileyPaddu Smiley
Hello Raviakanth thankyou for your answer
Paddu SmileyPaddu Smiley
Hello sir i have a doubt in custom buttons.I have created a detail page button with java script code and kept in the page layout but it is not appearing in the detail page. What may be the problem?
Ravikant Ravikant 10Ravikant Ravikant 10
Actually when the record is saved that means it shows the button in detail page if it is missed then check once again and do the process clearly in page layout and save it not do quick save do full save, it  will definitely show ...........!
Paddu SmileyPaddu Smiley
i tried it by creating 2 times and i clicked save not quick save..the issue is not resolved.
Paddu SmileyPaddu Smiley
hello i got it i did it in opportunities sales layout previously but now i did it in opportunities layout..my app is in sales but why it is not getting when I am doing it in opportunity(sales ) layout?
Ravikant Ravikant 10Ravikant Ravikant 10
App has nothing to do with the page layout which you are using.
Paddu SmileyPaddu Smiley
ok Thankyou
Paddu SmileyPaddu Smiley
Hello sir I need some help!! (Create a custom date field in an account. When we are inserting an account that date field should take only this week date (from mon to sun ). ) For this i wrote a trigger and for that i need a condition which accepts only the current week. can u please tell
Paddu SmileyPaddu Smiley
1) Map --Field and their values 2) Map-- Field and id How to Compare two Maps to check the both fields are equal .if they are equal need to get the field and the value.
Paddu SmileyPaddu Smiley
1) Map --Field and their values 2) Map-- Field and id How to Compare two Maps to check the both fields are equal .if they are equal need to get the field and the value WITHOUT using for loop.
Paddu SmileyPaddu Smiley
1) Map --Fields and their values 2) Map-- Fields How to Compare two Maps to check the both fields are equal .if they are equal need to get the field and the value WITHOUT using for loop.
Paddu SmileyPaddu Smiley
Hello Sir I have 1) Map --Fields and their values 2) Map-- Fields How to Compare two Maps to check the both fields are equal .if they are equal need to get the field and the value WITHOUT using for loop.
Paddu SmileyPaddu Smiley
Hello Sir I have 1) Map --FieldApi's and their values 2) Map-- Fields and FieldApi's How to Compare two Maps to check the both fieldApi's are equal .if they are equal need to get the field and the value WITHOUT using for loop.
Paddu SmileyPaddu Smiley
Hello Sir I have One query. There is a for loop which iterates all the fields in an object,actually limit is 500 know .Iterating 500 fields slow down the process.So Without iterating how can i get every field to check the below if condition
Paddu SmileyPaddu Smiley
Hello Sir how to get each Value from list without iterations in apex