1

Basically this question is answered in many blogs but none of them serves my purpose. I can see contradictory statements as well, whether or not a List Custom setting be accessed from a custom button using JS. Here is my JS of the custom button:-

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 
var userProfile = "{!$Profile.Name}";
var query1 = "SELECT Id,Tax_Exempt__c,FEIN__c,Tax_Exempt_Expiration_Date__c from Account WHERE Id='{!Opportunity.AccountId}'"; 
var rec1 = sforce.connection.query(query1); 
var rec2 = rec1.getArray('records'); 
var taxExempt=rec2[0].Tax_Exempt__c;
var fein=rec2[0].FEIN__c;
var oppStage = "{!Opportunity.StageName}"; 
var user = {!ISPICKVAL( $User.UserType , 'PowerPartner')}
var RollSync = "{!Opportunity.Roll_Up_Sync_Quote__c}"; 
var customersigner = "{!Opportunity.Customer_Signed_by_Contact__c}"; 
var installcontact = "{!Opportunity.Install_Contact__c}";
var lines;
var loaReq = false;
var query2 = "SELECT SyncedQuoteId FROM Opportunity WHERE Id='{!Opportunity.Id}'"; 
var rec3 = sforce.connection.query(query2); 
var rec4 = rec3.getArray('records'); 
/*var query3 = "SELECT Service_Type__c FROM OpportunityLineItem WHERE OpportunityId ='{!Opportunity.Id}' AND Service_Type__c = 'Business Trunking'"; 
var rec5 = sforce.connection.query(query3); 
var rec6 = rec5.getArray('records');*/
var query4 = "SELECT Id FROM OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' AND (Service_Type__c = 'Hosted Voice' OR Service_Type__c = 'Business Phone')"; 
var rec7 = sforce.connection.query(query4); 
var rec8 = rec7.getArray('records'); 
var query5 = "SELECT Id FROM Telephone_Number_Information__c WHERE Opportunity__c ='{!Opportunity.Id}'"; 
var rec9 = sforce.connection.query(query5); 
var rec10 = rec9.getArray('records'); 
var query6 = "SELECT lines__c FROM Quote WHERE OpportunityId = '{!Opportunity.Id}' AND IsSyncing = true";
var rec11 = sforce.connection.query(query6);
var rec12 = rec11.getArray('records');
var query7 = "SELECT Service_Type__c FROM OpportunityLineItem WHERE OpportunityId ='{!Opportunity.Id}' AND Service_Type__c = 'Enterprise Trunking'"; 
var rec13 = sforce.connection.query(query7); 
var rec14 = rec13.getArray('records'); 
var query8 = "Select id, PricebookEntryId, PricebookEntry.Product2Id from OpportunityLineItem WHERE OpportunityId ='{!Opportunity.Id}'";
var rec15 = sforce.connection.query(query8);
var rec16 = rec15.getArray('records');
var query9 = "Select Product_Id__c from Product_List__c"
var rec17 = sforce.connection.query(query9);
var rec18 = rec17.getArray('records');
alert(rec18);
for (var i=0;i<rec16.length;i++)
{
    for (var j=0;j=rec18.length;j++)
    {
        if(rec16.records[i].PricebookEntry.Product2Id == rec18.records[j].Product_Id__c)
            loaReq = true;
    }
}
var OpportunityObj = new sforce.SObject("Opportunity"); 
OpportunityObj.Id = '{!Opportunity.Id}';
var updateOpty = 'false';
if(rec12.length>0)
{
    lines = rec12[0].lines__c;
}
else{
    lines=0;
}


if(taxExempt == '' || taxExempt == null) 
{ 
    alert('Tax Exempt is mandatory on Account in order to generate Service Agreement'); 
} 
if(fein == '' || fein == null) 
{ 
    alert('Federal Tax ID Number is mandatory on Account in order to generate Service Agreement'); 
} 
else if(oppStage == 'New Opportunity') 
{ 
    alert('Sorry! You cannot create Service Agreement when Stage is "New Opportunity"'); 
} 
else if(RollSync == 0) 
{ 
    alert('Please Sync atleast one Quote'); 
}
else if(customersigner == "") 
{ 
    alert('Customer Signer should not be empty'); 
} 
else if(installcontact == "") 
{ 
    alert('Install Contact should not be empty'); 
} 
else if(rec8.length>0 && rec10.length == 0) 
{ 
    alert('Telephone Number is required when Voice Products are included'); 
}
else if((rec8.length>0 && rec10.length != lines) && lines != 0 && lines!=null) 
{ 
    alert('No of TNs records must be the sum of Full Featured Lines Products ,Basic Lines Products,Toll Free Number BP Products and Remote Call Forwarding BP Products.'); 
}
/*else if(rec6.length>0 && user==true) 
{ 
alert('Please contact CSS or Channel Manager to proceed with the order.'); 
}*/
else if(rec10.length > 0 && lines == 0)
{
    alert('Telephone Number not needed');
} 
else if(userProfile == "Senior Partner Community")
{ 
    OpportunityObj.Stage_Flag__c= 'True';
    OpportunityObj.Service_Agreement_Status__c = 'In Process';
    updateOpty = 'True'
}
else if(userProfile == "Channel CSS" || userProfile == "Sales - Alt Channels" || userProfile == "Manager of Channel Sales")
{
    OpportunityObj.StageName="Forecasted to Close this FP";
    OpportunityObj.Service_Agreement_Status__c = 'In Process';
    updateOpty = 'True';
}
if(updateOpty == 'True')
{
    result = sforce.connection.update([OpportunityObj]);
    if (result[0].getBoolean("success"))
    {
        if(loaReq == false)
        {
            location.reload(); 

            window.open("https://composer.congamerge.com?sessionId={!API.Session_ID}&serverUrl={!API.Partner_Server_URL_290}&id={!Opportunity.Id}&TemplateId=a0r54000000696x&LG3=1&LGAttachOption=0&AttachmentParentId={!Opportunity.Id}&DefaultPDF=1&SC0=1&FP0=1&QueryId=[TelephoneNumber]a0q54000000HDiq,[FFBusinessPhone]a0q54000000HDiw,[TelephoneNumberFF]a0q54000000HDj3,[BSCBusinessPhone]a0q54000000HDix,[TelephoneNumberBSC]a0q54000000HDir,[TFBusinessPhone]a0q54000000HDj2,[TelephoneNumberTF]a0q54000000HDj2,[RCFBusinessPhone]a0q54000000HDj1,[TelephoneNumberRCF]a0q54000000HDj5,[AllBusinessPhone]a0q54000000HDj0,[Internet]a0q54000000HDiq,[Video]a0q54000000HDiu,[Phone]a0q54000000HDiv,[BTrunking]a0q54000000HDip,[ETrunking]a0q54000000HDio&FlattenPDFForms=1&DS7=1",'_blank','location=yes,height=570,width=520,scrollbars=yes,status=yes');
        }
        else
        {
            location.reload(); 

            window.open("https://composer.congamerge.com?sessionId={!API.Session_ID}&serverUrl={!API.Partner_Server_URL_290}&id={!Opportunity.Id}&TemplateId=a0r54000000696x,a0r540000006mvH&LG3=1&LGAttachOption=0&AttachmentParentId={!Opportunity.Id}&DefaultPDF=1&SC0=1&FP0=1&QueryId=[TelephoneNumber]a0q54000000HDiq,[FFBusinessPhone]a0q54000000HDiw,[TelephoneNumberFF]a0q54000000HDj3,[BSCBusinessPhone]a0q54000000HDix,[TelephoneNumberBSC]a0q54000000HDir,[TFBusinessPhone]a0q54000000HDj2,[TelephoneNumberTF]a0q54000000HDj2,[RCFBusinessPhone]a0q54000000HDj1,[TelephoneNumberRCF]a0q54000000HDj5,[AllBusinessPhone]a0q54000000HDj0,[Internet]a0q54000000HDiq,[Video]a0q54000000HDiu,[Phone]a0q54000000HDiv,[BTrunking]a0q54000000HDip,[ETrunking]a0q54000000HDio&FlattenPDFForms=1&DS7=1",'_blank','location=yes,height=570,width=520,scrollbars=yes,status=yes');
        }
    } 
    else
    {
        alert("Failed to update Opportunity" + result[0]);
    }
}

The alert(rec18) is giving this output:-

cs4O.salesforce.com says:
{type:Product_List_c, Id:nuII,
Product_Id__c:'O1ti00000057dNnAAI',}

And is immediately followed by this error:-

cs4O.salesforce.com says:
A problem with the OnClick JavaScript for this button or
link was encountered:
Cannot read property '0' of undefined

What is wrong? The alert clearly shows that I'm able to get the values of the Custom Setting. Then whats the error about?

  • It doesn't look like rec18 is the source of your error being that you're not referencing a property of 0 on rec18. What is rec16? I'm assuming rec16.records is undefined – Antonio Manente Aug 18 '16 at 20:13
  • 1
    Apologies for the typo. it should be j=0. btw, its still giving the same error. rec16 is the collection of records from query8, and it is not null. – Abhijeet Mitra Aug 18 '16 at 20:16
  • Gotcha, although the error being thrown indicates that rec18.records would be undefined. Try to alert that instead to narrow down the source of the error. – Antonio Manente Aug 18 '16 at 20:32
  • I've put alert for rec16 and rec18 both. Below are the results:- rec16:- {type:OpportunityLineltem, Id:'00k540000039I6tAAE', PricebookEntryld:'01ui000000FkEs6AAF', PricebookEntry: {type: 'PricebookEntry', Id:nuII, Product2ld :'01ti00000057d NnAAI', ), }, {type:'OpportunityLineltem', Id:'00k540000039I6sAAE', PricebookEntryld:'01ui000000MgpxuAAB', PricebookEntry: {type: 'PricebookEntry', Id:nuII, Product2ld:'01ti0000005HG2dAAG', ), } rec18:- {type:Product_List_c, Id:nuII, Product_Id__c:'O1ti00000057dNnAAI',} The alert for their corresponding length gave 2 and 1 respectively. – Abhijeet Mitra Aug 18 '16 at 20:38
2

The error message is telling you that the property records is undefined.

var query8 = "Select id, PricebookEntryId, PricebookEntry.Product2Id from OpportunityLineItem WHERE OpportunityId ='{!Opportunity.Id}'";
var rec15 = sforce.connection.query(query8);
var rec16 = rec15.getArray('records');
var query9 = "Select Product_Id__c from Product_List__c"
var rec17 = sforce.connection.query(query9);
var rec18 = rec17.getArray('records');

While rec18 and rec16 may not be undefined, as demonstrated by your alert, they do not appear to have a property called records(atleast rec18 doesn't).

Earlier in your code, you instantiate rec12 in the same manner:

var query6 = "SELECT lines__c FROM Quote WHERE OpportunityId = '{!Opportunity.Id}' AND IsSyncing = true";
var rec11 = sforce.connection.query(query6);
var rec12 = rec11.getArray('records');

And then reference it in this manner:

if(rec12.length>0)
{
    lines = rec12[0].lines__c;
}

If I'm not mistaken, changing your nested for loop like such:

for (var i=0;i<rec16.length;i++)
{
    for (var j=0;j<rec18.length;j++)
    {
        if(rec16[i].PricebookEntry.Product2Id == rec18[j].Product_Id__c)
            loaReq = true;
    }
}

should solve your issue.

Hope this helps!

| improve this answer | |
  • 1
    My bad. Advice worked like a charm. Along with the reference, I had to change the nested for loop's syntax as well like this:- for (var j=0;j<rec18.length;j++) Thanks a ton. – Abhijeet Mitra Aug 18 '16 at 21:22
  • @AbhijeetMitra No problem, glad I could help – Antonio Manente Aug 18 '16 at 21:41

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.