You need to sign in to do that
Don't have an account?

Ajax Tookit on Custom Button from detail page to update that custom object
I'm want to write some JS in a custom button to update the status field of the current object. I found some example code that seems to do what I want, but I'm getting an error when performing the update.
The error I'm getting is: Refused to set unsafe header "User-Agent"
I get this error when I try to update SF with result = sforce.connection.update(updateRecords);
Any tips?
I solved the problem.
I guess there were two bugs in this issue:
1) This bug in the Ajax Toolkit. It was an incidental discovery because it was the only error that was reported to me.
2) I was attempting to clear the field value by doing: (a.Taken_By__c = ""; //an empty string, no spaces), This does nothing. By adding a space to that field value, (a.Taken_By__c = " ";), the intent is understood when received by the SF service, and the field will be cleared, thereby solving my initial problem.
The spec-offending code still resides in the Ajax Toolkit, however, and this is a bad thing. I would guess that everyone that uses the Ajax Toolkit DOES receive this error, but because the request is sent anyways (though it probably should not), thereby delivering the expected results, so nobody bothers to investigate the error.
When the request does have the User-Agent a header, the request is violating the specification, so why does the browser still send the request? This is what confuses me now.
All Answers
Hi Alex,
This code is to update a field of current record.
Hope it helps.
No, this gives the same result as my own code. I'm pretty sure the problem lies in the headers on the HttpRequest. There is a header (User-Agent) that the browser sees, so the browser won't send it.
Does anyone know how to set the Request headers in the Ajax Toolkit? I'd like to clear out the User-Agent header.
Post your code which gives you the same error.
The error happens here: sforce.connection.update([a]);
Remove below line and check again:
Same error:
Refused to set unsafe header "User-Agent"
I'm in the Chrome browser, by the way.
Read this:
http://dev.w3.org/2006/webapi/XMLHttpRequest-2/#dom-xmlhttprequest-setrequestheader
The spec for XmlHttpRequest says that the User-Agent can not be set.
Now check out the source code from the Ajax Toolkit:
Line 599: they set the User-Agent. (-_-)
This is a bug. I found the problem. The creator needs to fix this. I tried contacting SF through support, but they tried to tell me that they don't support the Ajax Toolkit.
I would clear the header before sending it, but I'm pretty sure it is not possible. (or can you remove headers from an XmlHttpRequest?)
Can anyone tell me who I can talk to to get this fixed?
I thought I'd paste this here for the assistance of others.
You can eliminate the error by overriding the sforce.Transport function at runtime ('monkey patching'). See below:
I solved the problem.
I guess there were two bugs in this issue:
1) This bug in the Ajax Toolkit. It was an incidental discovery because it was the only error that was reported to me.
2) I was attempting to clear the field value by doing: (a.Taken_By__c = ""; //an empty string, no spaces), This does nothing. By adding a space to that field value, (a.Taken_By__c = " ";), the intent is understood when received by the SF service, and the field will be cleared, thereby solving my initial problem.
The spec-offending code still resides in the Ajax Toolkit, however, and this is a bad thing. I would guess that everyone that uses the Ajax Toolkit DOES receive this error, but because the request is sent anyways (though it probably should not), thereby delivering the expected results, so nobody bothers to investigate the error.
When the request does have the User-Agent a header, the request is violating the specification, so why does the browser still send the request? This is what confuses me now.
Alex - I have also been running into this exact issue on Chrome.
For me however, the request returns a 500 error from the serverUrl.
from monitoring the requests, I see that
serverUrl = "https://na12-api.salesforce.com/services/Soap/m/22.0/00DU0000000IEvm" as given by login(), which looks okay to me
The odd thing is that the response to this is a 500 error! I thought it might have to do with that user-agent error, but commenting it out makes no difference.
Any insight you can still remember from this issue? Any clue why the url returned by login() would give a 500 error?
Thank you very much in advance.
Can you post the JS code that you are using? It's probably that the problem lies there.
I can imagine that you are using a Database.update(someObject), but 'someObject' doesn't have a value in the Id property. Another issue could be that you are trying to update 'someObject', but the Id doesn't match that object type.
My code is running in a non-salesforce context that allows for XHR (it's a chrome extension, running on the background page where XHRs are allowed by the browser)
var conn = new sforce.Connection(); //this works
conn.login("tal@ecquire.com", "password+token"); //this works and returns session id, serverUrl etc etc
var result = sforce.connection.query("Select Name,Id from User", {onSuccess : success, onFailure : failure});
// This is where the problem is caused - the failure callback prints the error below starting with "an error has occurred"
I just realized that this is probably a session id issue. In the chrome console these two errors go together (the first is chrome's error reporting):
and my failure callback prints out this error object:
An error has occurred {faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', detail:{UnexpectedErrorFault:{exceptionCode:'INVALID_SESSION_ID', exceptionMessage:'Invalid Session ID found in SessionHeader: Illegal Session', }, }, }
So it appears that the 500 error is caused by the invalid session id. It's very odd - i'm trying to figure out why that is. Any clue so far? I will continue to investigate and post any new developments.
Okay - solved the problem with a "monkey patch"
The issue was that for some reason, even though login() returned a sessionId, it was not storing it correctly. Inside sforce.Connection.prototype.login() the line of code this.sessionId = result.sessionId; was not enough to do it.
Basically had to copy version 22.0 of the salesforce ajax toolkit, use it locally, and add this more explicit line
sforce.connection.sessionId = result.sessionId
to this function
sforce.Connection.prototype.login()
after this line
var result = this.invoke("login", [arg1, arg2], false, null);
I know it's not the best way to do things since it assumes the instance of the class sforce.Connection, sforce.connection, will always be there, and I hope there's a better solution using the this keyword, but it works for now.
Hi Rahul,
Can you please give an example of code how to set this field value dynamically?
Sachin
Hi Steven,
The monkey patching script you have provided works like a charm.. Thanks!!
Could you please explain as to what the script does??
Regards
Sam
Good job Steven, it worked just fine!! Thanks a lot!! :)
--
Best Regards,
Anand Agrawal
Salesforce Consultant | Horicent Systems | Pune - India
M +91.997.028.2098 Skype: horicent.systems Twitter @horicentweets
W www.horicent.com
I want the code for Javascript that is used to connect salesforce i am making chrome extension. Please give me code for retrive information from salesforce.
sforce.connection.sessionId = "{!$Api.Session_ID}";