6

Full .js button code:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT('/resource/' & LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) & '000/JS')}

sforce.connection.sessionId = "{!$Api.Session_ID}";

var accId = "{!Opportunity.Account}";

runURL(accId);

Static resource:

function runURL(accId){
    function errorMsg(accId) {
        var accInfo = sforce.connection.query("SELECT Id, Company_Legal_Name__c FROM Account WHERE Id = '" + accId + "' AND Company_Legal_Name__c != ''");
        console.log("##### Acc ID " + accId);
        console.log("##### Acc Query " + accInfo);
        if (accInfo.size != 1) {
            return "\u2022 Please enter a Company Legal Name";
        } else return "";
    }
}

When I run the above code I can't seem to pass the Account Id to a function in a static resource... however the console.log() actually outputs the correct Account Id, although I get the below error:

Error Screenshot

The query is not actually picking up the Account Id, also this function in inside of another function.

3
  • 1
    There can be order of exceution issue of way your DOM loads .Keep all the code outside static resource and try . – Mohith Shrivastava Aug 18 '14 at 5:15
  • I assume the onclick button code is defined outside of the static resource? Can you post the code that you use to set up the onclick handler and the console log output? – Bob Buzzard Aug 18 '14 at 8:04
  • Updated code so everything that I am testing with at the moment is there. – Tim Aug 18 '14 at 9:06
2

If you want to pass in the Id of the Opportunity’s Account you must actually pass in the AccountId field and not the Account field.

var accId = "{!Opportunity.AccountId}";

It is legal to declare functions inside of functions in JavaScript, but you still have to call them. I’m not sure how the errorMsg function is executing without being called (left that code out?). You should need another line that actually invokes it. I’m guessing you have something like the following:

function runURL(accId){
    // 
    function errorMsg(accId) {
        var accInfo = sforce.connection.query("SELECT Id, Company_Legal_Name__c FROM Account WHERE Id = '" + accId + "' AND Company_Legal_Name__c != ''");
        console.log("##### Acc ID " + accId);
        console.log("##### Acc Query " + accInfo);
        if (accInfo.size != 1) {
            return "\u2022 Please enter a Company Legal Name";
        } else return "";
    }
    // possibly some other stuff…
    // add the actual invocation of errorMsg function
    errorMsg();
} 

The issue with that is that the accId parameter declared in errorMsg is not the same as the one declared in runURL and when you call errorMsg with no argument the accId in errorMsg is undefined. If errorMsg is actually inside of runURL, you don’t need to pass accId to errorMsg. It has access automatically, so you can just remove the parameter in the function declaration. Alternatively, you can pass it explicitly in the method call.

errorMsg(accId); with function errorMsg(accId) will work.

errorMsg(); with function errorMsg() will work.

But errorMsg(); with function errorMsg(accId) will not work.

0
function runURL(accId){
var accInfo = sforce.connection.query("SELECT Id, Company_Legal_Name__c FROM Account WHERE Id = '" + accId + "' AND Company_Legal_Name__c != ''");
    function errorMsg() {
        if (accInfo.size != 1) {
            return "\u2022 Please enter a Company Legal Name";
        } else return "";
    }
}

Externalising the query and removing the parameter worked... I can't believe I didn't try this before... I have sat here for 2 days trying to work this out... what a plonker :')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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