function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KNKKNK 

problem with Onclick java Script

Hi Team,

 

I have below code in Custom button

 

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}
var recordTypeName = '{!Account.RecordType}';
var BPRecordId = '';
var theDate = new Date();
var currentYear = theDate.getFullYear();

if(recordTypeName == 'Channel'){
var result = sforce.connection.query("Select r.Id From RecordType r where r.SobjectType = 'Business_Plan__c' and r.DeveloperName = 'Distributor' limit 1");
BPRecordId = result.getArray("records")[0].Id;
}else if(recordTypeName == 'Grower' ){
var result = sforce.connection.query("Select r.Id From RecordType r where r.SobjectType = 'Business_Plan__c' and r.DeveloperName = 'Grower' limit 1");
BPRecordId = result.getArray("records")[0].Id;
}
else if(recordTypeName == 'Lawn and Garden'){
var result = sforce.connection.query("Select r.Id From RecordType r where r.SobjectType = 'Business_Plan__c' and r.DeveloperName = 'Lawn and Garden' limit 1");
BPRecordId = result.getArray("records")[0].Id;
}
else if(recordTypeName == 'Influencer'){
var result = sforce.connection.query("Select r.Id From RecordType r where r.SobjectType = 'Business_Plan__c' and r.DeveloperName = 'Influencer' limit 1");
BPRecordId = result.getArray("records")[0].Id;
}
else{
alert('Invalid Operation. Please contact System Admin for details!!');
}
var BPDescription = sforce.connection.describeSObject("Business_Plan__c");
var BPUrlForNew = BPDescription.urlNew.split('com')[1];
var CFID = 'CF'+'00N90000004WQVZ'; // this ID is differnect with the production,please chage it after deployment
var AccountName = '{!Account.Name}';
var aid='{!Account.Id}';
result = sforce.connection.query("Select Id, Name  From Business_Plan__c where Name = \'" + currentYear + "\'  and Account__c = \'{!Account.Id}\' limit 1" );
records = result.getArray("records");
if(records.length > 0){
var prevId = records[0].Id;
window.top.location=prevId;
}
else{
 window.top.location=BPUrlForNew+'?'+CFID+'='+AccountName+'&'+CFID+'_lkid='+aid+'&RecordType='+BPRecordId+'&Name='+currentYear;
}

 

 

This is working for All Recrod types Except Lawn and Garden, When I click on the button it is shwoing following error : "A problem with the OnClick JavaScript for this button or link was encountered:  result.getArray(...)[0] is undefined".

 

 

Kindly please advice me where i was wrong..

_Prasu__Prasu_
This may be because you query "Select r.Id From RecordType r where r.SobjectType = 'Business_Plan__c' and r.DeveloperName = 'Influencer' limit 1" is returning null.

That means no matching records where found.
Try running this query standalone. You may be specifying wrong where clause.