+ Start a Discussion
KNKKNK 

Problem with Onclick JavaScript

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='

 

This is not working for lawn and Garden RecordType.

 It is showing following error : result.getArray(...)[0] is undefined. Kindly please help me.

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu
yep that should be the reason.....good one Cherrlin...

All Answers

kiranmutturukiranmutturu
here i think u need to take care about lower and upper case too define the recordtype name how it was defined...
KNKKNK
Lawn and Garden is the recordType
kiranmutturukiranmutturu
put an alert in the else and check it is coming or not when you are working with Lawn and Garden record type.

else if(recordTypeName == 'Lawn and Garden'){
alert("entered");
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;
}
SharathChandraSharathChandra

DeveloperName  should not consist of spaces i think

here Lawn and Garden record type DeveloperName should be Lawn_and_Garden

kiranmutturukiranmutturu
yep that should be the reason.....good one Cherrlin...
This was selected as the best answer