+ Start a Discussion
Ekin Van Winkle 2Ekin Van Winkle 2 

REQUIRESCRIPT no longer working with Winter 16

As per release winter 16 release notes: http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/salesforce_release_notes.htm
 

REQUIRESCRIPT no longer executes JavaScript on Page Load.

We use the REQUIRESCRIPT function in several custom buttons on our lead page.  When the Winter 16 features were released into our sandbox, our buttons started returning an error Reference $ Undefined.

I reached out to salesforce support, and they told me to use the code in the following article which removed the REQUIRESCRIPT function and instead, creates a custom java function.

https://help.salesforce.com/apex/HTViewSolution?urlname=Receiving-error-sforce-apex-is-null-or-not-an-object-1327108329858&language=en_US

I've been messing with the code above to fit it into my button.  I'm stepping through the code through alerts, it stepped all the way through but at the end I get an Object error.  Any help would be appreciated.

function loadScript(url) 

    var request; 
    if (window.XMLHttpRequest) 
    { 
        request = new XMLHttpRequest(); 
    }     
    else if (window.ActiveXObject) 
    { 
        request = new ActiveXObject("Msxml2.XMLHTTP"); 
        if (!request) 
        { 
            request = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
    
    } 


    var se = document.createElement('script'); 
    se.type = "text/javascript"; 
    //alert("before Request undefined\n");
    request.open("GET", url, false); 
    request.send(null); 
    se.text = request.responseText; 
    document.getElementsByTagName('head')[0].appendChild(se); 
    //alert("end of loadScript\n");


loadScript("/soap/ajax/30.0/connection.js"); 
loadScript("/soap/ajax/30.0/apex.js"); 
loadScript("/resource/jQuery/jquery-1.8.2.min.js"); 
loadScript("/resource/jQuery/ui/jquery-ui-1.9.1.custom.min.js");
loadScript("/resource/jQuery/postmessage/jquery.ba-postmessage.js");
loadScript("/resource/jQuery/bbq/jquery.ba-bbq.min.js");
loadScript("/resource/buttonjs");

try 

    var j$ = jQuery.noConflict(); 
    var newurl= sforce.apex.execute("RegisterMerchant", "getPricingArticleID",{leadID : "{!Lead.Id}"}); 
    newurl1 = String(newurl); 
    alert('Lead ArticleID '+newurl1); 
    if(newurl1.indexOf('error')>-1 ) 

    alert(newurl); 
    document.location.reload(true); 

else 

    alert('Start of Else Statement')
    var strQuery="Select l.of_Pin_Pads__c,l.Id, l.Article_ID__c, l.Processing_Origin__c, l.Company, l.Strategic__c, l.Gift_Only__c, l.Dealer__r.Name, l.Developer__c, l.DLR_Sales__c, l.Name, l.OwnerId from Lead l where Id= '" +'{!Lead.Id}'+"'"; 
    var accFields = sforce.connection.query(strQuery); 
    var records = accFields.getArray("records"); 

    var owner = records[0].OwnerId; 
    var ContactName = records[0].Name; 

    var leadId = records[0].ID; 
    var Dealer = records[0].Dealer__r.Name; 
    var devID = records[0].Developer__c; 
    var Developer = null; 
    var DealerSalesperson = null; 
    if (devID!=null) 
    alert('Developer Record')
    

    var strQuery3 = "SELECT Name from Account where ID='" +devID+"'"; 
    var devField = sforce.connection.query(strQuery3); 
    var devRecord = devField.getArray("records"); 
    Developer = devRecord[0].Name; 
    


var ds = records[0].DLR_Sales__c; 
//if (ds!= null) 
    alert('DLR sales Record1')
//{ 
var strQuery6 = "Select Name from Contact where id = '" +ds+"'"; 
var dlrSalesField = sforce.connection.query(strQuery6); 
var dlrSalesRecord = dlrSalesField.getArray("records"); 
DealerSalesperson = dlrSalesRecord[0].Name; 
//}
alert('Dlr sales Record2') 

var NumberOfPinpads = 0; 
//if(records[0].of_Pin_Pads__c!=null) 
//{ 
NumberOfPinpads = parseInt(records[0].of_Pin_Pads__c); 
//}
alert('Pin Pads') 
var ArticleId = records[0].Article_ID__c; 
var ProcessingOrigin = records[0].Processing_Origin__c; 
var CompanyName = records[0].Company; 
var StrategicAccount= records[0].Strategic__c; 
var GiftOnly = records[0].Gift_Only__c; 
alert('Other stuff') 


// if diff is null, continue, if user clicked cancel, allow them to chg processing origin 
console.log('leadId '+leadId+'--'+Dealer+'--'+Developer+'--'+ArticleId+'--'+DealerSalesperson); 
// next section stolen from Josh, thanks for doing my typing buddy! 
var $articleId = $('<input>', { name: 'ArticleId', type: 'hidden', value: ArticleId }); 
var $processingOrigin = $('<input>', { name: 'ProcessingOrigin', type: 'hidden', value: ProcessingOrigin }); 
var $companyName = $('<input>', { name: 'CompanyName', type: 'hidden', value: CompanyName }); 
var $strategicAccount = $('<input>', { name: 'StrategicAccount', type: 'hidden', value: StrategicAccount }); 
var $giftOnly = $('<input>', { name: 'GiftOnly', type: 'hidden', value: GiftOnly }); 
var $contactName = $('<input>', { name: 'ContactName', type: 'hidden', value: ContactName }); 
var $dealer = $('<input>', { name: 'Dealer', type: 'hidden', value: Dealer }); 
var $developer = $('<input>', { name: 'Developer', type: 'hidden', value: Developer }); 
var $dealerSalesperson = $('<input>', { name: 'DealerSalesperson', type: 'hidden', value: DealerSalesperson }); 
var $numberOfPinpads = $('<input>', { name: 'NumberOfPinpads', type: 'hidden', value: NumberOfPinpads }); 

//alert($articleId.html()); 
var $form = $('<form>', { 
'action': '{!$Label.pricingUI}Invoke', 
'method': 'post', 
'target':'_blank' 
}); 

$form.append($articleId); 
$form.append($processingOrigin); 
$form.append($companyName); 
$form.append($strategicAccount); 
$form.append($giftOnly); 
$form.append($contactName); 
$form.append($dealer); 
$form.append($developer); 
$form.append($dealerSalesperson); 
$form.append($numberOfPinpads); 

$(document.body).append($form); 

$form.submit(); 
// alert('done by me'); 


catch(er) 

alert(er); 
}
 

Best Answer chosen by Ekin Van Winkle 2
Ekin Van Winkle 2Ekin Van Winkle 2
I ended up finding the resolution.  SF is removing some of the jquery references.  When I added this to the button, it worked:

{!REQUIRESCRIPT("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")}

All Answers

NiketNiket

I faced the issue yesterday and with some hit and try approach I was able to make it work .

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")} 

use these 2 lines at the top in the same sequence.


Please mark it as the solution if it answers your question so that others can also take benifit. 
Ekin Van Winkle 2Ekin Van Winkle 2
Thanks for the response but this isn't working.

With the new Winter 16, you can no longer use Requiresript for a button that executes on OnClick Javascript.

So I replaced the Requirescript with a function that is actually working:

function loadScript(url) 

    var request; 
    if (window.XMLHttpRequest) 
    { 
        request = new XMLHttpRequest(); 
    }     
    else if (window.ActiveXObject) 
    { 
        request = new ActiveXObject("Msxml2.XMLHTTP"); 
        if (!request) 
        { 
            request = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
    
    } 


    var se = document.createElement('script'); 
    se.type = "text/javascript"; 
    //alert("before Request undefined\n");
    request.open("GET", url, false); 
    request.send(null); 
    se.text = request.responseText; 
    document.getElementsByTagName('head')[0].appendChild(se); 
    //alert("end of loadScript\n");


loadScript("/soap/ajax/30.0/connection.js"); 
loadScript("/soap/ajax/30.0/apex.js"); 
loadScript("/resource/jQuery/jquery-1.8.2.min.js"); 
loadScript("/resource/jQuery/ui/jquery-ui-1.9.1.custom.min.js");
loadScript("/resource/jQuery/postmessage/jquery.ba-postmessage.js");
loadScript("/resource/jQuery/bbq/jquery.ba-bbq.min.js");
loadScript("/resource/buttonjs");

However, now the button does not like the query below where I'm trying to Select the Name from Account where ID = the developer ID that I declared as a variable.  Basically I have a field named Developer__c that is a lookup from the Lead to the Account object.  If Developer__c is not null, then I want to Select the name from account where the ID = the developer ID in the lookup, but for some reason SF does not like the syntax.

 var leadId = records[0].ID; 
    var Dealer = records[0].Dealer__r.Name; 
    var devID = records[0].Developer__c; 
    var Developer = null; 
    var DealerSalesperson = null; 
    if (devID!=null) 
    alert('Developer Record')
    

    var strQuery3 = "SELECT Name from Account where ID='" +devID+"'"; 
    var devField = sforce.connection.query(strQuery3); 
    var devRecord = devField.getArray("records"); 
    Developer = devRecord[0].Name; 
snookiesnookie
Thanks Ekin.. this worked like a charm.
Ekin Van Winkle 2Ekin Van Winkle 2
I ended up finding the resolution.  SF is removing some of the jquery references.  When I added this to the button, it worked:

{!REQUIRESCRIPT("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")}
This was selected as the best answer
Anil SavaliyaAnil Savaliya
Hi All,

Anybody found any workaround for onload work ? I changed my logic above way,But work only when i click on link,It's not work page onload

Thanks,
Anil Savaliya
Dipti DDipti D
Can someone provide a Solution for the this. I did not notice any difference between Ekin's previous code and new code.
Here is my scenario.
We have color coded buttons with functionality of displaying them as iframe on our standard page layout. After Winter 16 release in our sandbox, the buttons are broke - they are not rerendering immediately to display like they are supposed to. Our developer has used easyXDM functionality.
How they used to behave - On the standard pagelayout - the buttons are displayed correctly based on the profile & based on the Case queue.
Now - after winter 16 release -
All the buttons are shown. I see the hidden XDM button as well. In IE11, when I click on the XDM button it is redirecting me to URL not available. In Firefox & Chrome, when I click on XDM, it pops me up with a question to run the page again, when I click YES, it opens my standard layout like it should.
Here is my code for XDM button for onclick javascript.

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')}
{!REQUIRESCRIPT('https://rawgit.com/ternarylabs/porthole/master/src/porthole.min.js')}
{!REQUIRESCRIPT('/resource/' & LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) &'000/easyXDMConsumer')}

//Js button onClick code
__ConsumerXDM.post({verb:"refresh"});
Ekin Van Winkle 2Ekin Van Winkle 2
Add this after your requirescript for Rawgit


{!REQUIRESCRIPT("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")}

Let me know if that works.
Dipti DDipti D
Hi Ekin,
Thank you for your quick response. Unfortunately that did not work.
Can you see if any other solution works. Our production release is pretty soon and I have to come up with a solution prior to that .
Thank you!

Here is my changed code.

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')}
{!REQUIRESCRIPT('https://rawgit.com/ternarylabs/porthole/master/src/porthole.min.js')}
{!REQUIRESCRIPT("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")}
{!REQUIRESCRIPT('/resource/' & LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) &'000/easyXDMConsumer')}

//Js button onClick code
__ConsumerXDM.post({verb:"refresh"});
Dipti DDipti D
Hi All, 
Any update or help regarding this issue? Thank you!
m_a_sridharm_a_sridhar
You might try this coding pattern. Suppose you have this code in your button:
 
{!REQUIRESCRIPT ('myFile1.js')}
{!REQUIRESCRIPT ('myFile2.js')}
{!REQUIRESCRIPT ('myFile3.js')}
// Button code is here...

Replace it with this code:
 
Sfdc.Resource.addJavaScripts (['myFile1.js', 'myFile2.js', 'myFile3.js'], function () {
    // Button code is here...
});


This idea uses the fact that the Sfdc.Resource.addJavaScripts function is available on all pages.
Dipti DDipti D

@Sridhar, 
Thank you. But the workaround did not work. 
Thanks!

Doug WeyersDoug Weyers
I am running into a similar issue with our custom "request Approval" button.  I have not been able to fins a work around yet. If anyone has any ideas on how to fix this it would be greatly appreciated.

Note; When we run this the only error we  get is "Choose Approver" followed by "We need to confirm that your request is valid. Please repeat your last action."

Thank you in advance,

Douglas S. Weyers


Here is the code we are running:

{!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/35.0/apex.js")} 


var ItemsReady = "{!Opportunity.Line_Items_Ready_for_Approval__c}"; 

//alert (ItemsReady ); 

if (ItemsReady == "No") 

var URL = "/oppitm/multilineitem.jsp?oppId={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}"; 
alert("Request Not Sent.\n\nPlease review your Line Item fields.\n\n(You will need to press the 'Request Approval' button again to resubmit your request after saving your changes.)"); 

else 

var OppReady = "{!Opportunity.Ready_for_Approval__c}"; 

//alert (OppReady); 

if (OppReady == "No") 

var URL = "/{!Opportunity.Id}/e?retURL=%2F{!Opportunity.Id}"; 
alert("Request Not Sent.\n\nPlease review your Opportunity fields.\nPlease resubmit your request after saving your changes."); 

else 

var URL = "/p/process/Submit?retURL={!Opportunity.Id}&id={!Opportunity.Id}"; 
alert ("Your request has been submitted."); 


//alert (URL); 

window.location.href = (URL);
Clare WestonClare Weston
I'm also experiencing this problem. We have a custom button that launches an apex class.

The button code is:

{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}

try{
window.location = sforce.apex.execute("MassCreationGratisItemsUrlRedirector", "urlRedirector", {campaignId:"{!Campaign.Id}"});
}catch(err){
alert(err);
}


At the moment, it's returning the following errors:
TypeError: Cannot read property 'execute' of undefined
TypeError: sforce.apex is undefined

Can anyone tell me what the workaround is??

Thanks!