You need to sign in to do that
Don't have an account?
Steve Cairney
Javascript button giving the wrong alert.
Hi, I have a very simple button that sends infomation to a webservice.
What I'm trying to do is have an if / else statement that will display an alert with whether the submission was a success or failure.
The code is really quite simple, but it's displaying the wrong alert on a record that I know is a success and a record that I know fails.
Here's the code (note the first alert is just so I can see the inital response from the webservice
If it's a Success, it will return
If it fails, it will return
What could be wrong in the code?
What I'm trying to do is have an if / else statement that will display an alert with whether the submission was a success or failure.
The code is really quite simple, but it's displaying the wrong alert on a record that I know is a success and a record that I know fails.
Here's the code (note the first alert is just so I can see the inital response from the webservice
If it's a Success, it will return
If it fails, it will return
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} var json = sforce.apex.execute("ItsApprovedWebServices","GetBookingDetails",{id:"{!ItsApproved_Booking__c.Id}"}); var authorizationToken = sforce.apex.execute("ItsApprovedWebServices","Login",{userName: 'xxxx'}); var result = sforce.apex.execute("ItsApprovedWebServices","PostBooking",{json: json, authorizationToken: authorizationToken}); alert(result); //if correct returns "Success", if incorrect, returns a long error message if (result == "Success") { alert("OK, Booking sent to ItsApproved - http:www.google.com"); } else { alert("Error! Call Cestrian and quote '{!ItsApproved_Booking__c.Name}'"); }I'm getting the second alert for both scenarios.
What could be wrong in the code?
Hi Steve,
Based on your first post on this question, I think it's returning an array.
So you need to check like this.
All Answers
I think the problem is in your comparison (result == "Success"), first of all are you sure result is a string? Try to do this
if(string(result) === "Success"")...
Is your Method returing the string value as "Success"? i think in your case the method is retunring different value.please share your Class.
here is my code.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var result = ""+sforce.apex.execute( "MyWSClass", "WSMethod", {param:"{!Account.Id}"});
if (result == 'Success') {
alert("Success............");
//your logic
}
else {
alert("Error!...........");
}
========================
Webservice:
global class MyWSClass{
Webservice static string WSMethod(string prm) {
if(prm != null) {
try {
//your logic here
return 'Success';
}
catch(Exception e){
return 'Error';
}
}
else {
return 'Error';
}
}
}
sforce.apex.execute("ItsApprovedWebServices","Login",{userName:'xxxx'}, {onSuccess: function(result){//alert success}},{onFailure: function(error){//alert error}});
i you abserved the method "PostBooking" is returing res.getBody();....
webservice static String PostBooking(String json, String authorizationToken){
-----------------
---------------
res.getBody();// means the condiation in script is always false
}
So try to return Success /Error string like below
res.getStatus()
if(res.getstatuscode() ==200){
return 'Success';
}
else
{
return 'Error';
}
See below link for more details.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_restresponse.htm%23apex_System_RestResponse_methods
How can I test this with string length?
For example, we know that if there is an error it will return a long string. We tried this but couldn't get the right results.
And now the records are returning the first alert, not the second one!
res.getBoday() method will return more repsopnse(string length).so 1st is always true..
in which condition you want to make success? login is sueecess you want get success page right?
try my code and see it will work..
webservice static String PostBooking(String json, String authorizationToken){
-----------------
------------your code---
res.getBody();// means the condiation in script is always false
if(res.getStatusCode() == 200)// it's for success login
return 'Success';
}
// if any exception
else {
return 'Error';
}
return res.toString();
in the class it will return OK 200 regardless. So we need to look at how to use
return res.getBody();
to give an error, res.getBody(); must return a value that does NOT say "Success". Anything else means that it will have failed (regardless of the 200)
Hi Steve,
Based on your first post on this question, I think it's returning an array.
So you need to check like this.
try below code this will work for you in .
webservice static String PostBooking(String json, String authorizationToken)
{
// Now send the data to Its Approved
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('http://removed');
req.setMethod('POST');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization-Token', authorizationToken);
req.setBody(json);
req.setTimeout(60000);
// try {
res = http.send(req);
// } catch(System.CalloutException e) {
// System.debug('Callout error: '+ e);
// System.debug(res.toString());
// }
//return res.toString();
string isSuccess,isErrors;
JSONParser parser = JSON.createParser(res.getBody());
while(parser.nextToken() != null) {
if((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
if(parser.getText() == 'success') {
parser.nextToken();
isSuccess = parser.getText();
}
if(parser.getText() == 'errors') {
parser.nextToken();
isErrors = parser.getText();
}
}
}
System.debug('JSON Response Success :' + isSuccess);
System.debug('JSON Response Errors :' + isErrors);
return isSuccess;
}
change the code in Script as well like
if (result == "true") {
alert('Ok..')
}
else{
alert('Error');
}