Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm trying to call the AJAX toolkit from my VF page, which is placed in the Account layout but I get these 2 errors:

Refused to set unsafe header "User-Agent"

Uncaught TypeError: Cannot read property 'push' of undefined apex.js:48 sforce.Apex.execute
apex.js:48 getBillingDetails

This is my page:

<apex:page standardController="Account" >

    <apex:includeScript value="/soap/ajax/29.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/29.0/apex.js"/>

    <script type="text/javascript"> 
        var __sfdcSessionId = '{!GETSESSIONID()}';
        window.onload = getBillingDetails;

        function getBillingDetails(){
            var user = sforce.connection.getUserInfo(); 
            var result = sforce.apex.execute( 
                            "Webservices", // class 
                            "getBillingInfo", // method
                             {accIds:user.Id, householdIDsOnly:false});

        window.location.reload();            
        }
        return null;
    </script>
</apex:page>
share|improve this question

2 Answers 2

The JavaScript you are including is running into a situation it is not designed to handle.

Where the @RemoteAction method is on a custom controller referenced by the page, there is no need to include any scripts (such as connection.js and apex.js) or set session variables and there is a generated JavaScript function in the page - getBillingInfo in your case - that you can just go ahead and call (without having to use sforce.apex.execute). The same pattern will work if you add extensions="Webservices" to your page. So I think that simplification will help.

If you need the User ID at the server side, just calling UserInfo.getUserId() in your @RemoteAction code will give you that (and that approach is less subject to security risks).

Your assignment to window.onload is replacing JavaScript that Visualforce hooks to that event so is dangerous. Google about this to find out about patterns to fix it (or use jQuery - see below).

Ending your getBillingDetails function with window.location.reload() is likely to leave you in an infinite loop of page loads once you have fixed other things. It would be more normal to update the current page using e.g. jQuery with the results from the remote call. (jQuery has a ready function that saves you having to assign to window.onload too.)

share|improve this answer
    
Thanks, I was really trying to avoid writing a controller for this page. I wanted to use the same method I call from the custom button I have on the detail page, in sort of an attempt to automate the processes (the user will not have to click the button). This is also the reason I wanted the page refreshed after the call returned, so the user can see the new information. By your answer I understand this is not doable, correct? –  Dedo Mar 12 at 5:42
    
I don't think you will need to change your Webservices class, you can just reference it using extensions="Webservices" and then the callable getBillingInfo will just appear automatically in your page. (Perhaps using sforce.apex.execute avoids that but IMHO is a less elegant way to go.) I assumed getBillingInfo returned something you wanted to show in the page ("get" usually implies that). But perhaps it does something else. –  Keith C Mar 12 at 8:39

This is what finally worked for me

<apex:page standardController="Account" >

    <apex:includeScript value="/soap/ajax/29.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/29.0/apex.js"/>

    <script type="text/javascript"> 
        var __sfdcSessionId = '{!GETSESSIONID()}';

        var result = sforce.apex.execute( 
                        "Webservices", // class 
                        "getBillingInfo", // method
                         {accIds:['{!Account.Id}'], householdIDsOnly:false});

    document.location.reload(true)

    </script>
</apex:page>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.