+ Start a Discussion
Paras JainParas Jain 

when i click on Related list Button error comes "On click Java Script SForce not defined"

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


var myquery = "SELECT Id,recordType.Name FROM Account WHERE Id = '{!Account.Id}' limit 1";
var result = sforce.connection.query(myquery);
var records = result.getArray("records")[0];
var accountrecordtype= ''+records.RecordType.Name;

var result2 = sforce.connection.query("select Id,Name from RecordType where name='"+accountrecordtype+"' and SObjectType='Contact'");
var conRecordType = result2.getArray("records")[0];
var recordType = ''+conRecordType.Id;

window.location = 'https://cs20.salesforce.com/003/e?retURL=%2F{!Account.Id}&accid={!Account.Id}&RecordType='+recordType;
KaranrajKaranraj
Paras - Your code works fine in my developer org. Its seems like an problem with user permission not in the javascript code. Try check the functioanlity using Admin user and let us know, if you are still facing the problem.
Paras JainParas Jain
It working fine in Related List which comes below in Account page layuot but in mini console view or mini page layout , On top of the page this button shows error
KaranrajKaranraj
To avoid that error, open the window in a new page, instead of loading in a same page. Change window.location into window.open which will open the page in a new window by this you can able to resolve this issue. Try the below code
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var myquery = "SELECT Id,recordType.Name FROM Account WHERE Id = '{!Account.Id}' limit 1";
var result = sforce.connection.query(myquery);
var records = result.getArray("records")[0];
var accountrecordtype= ''+records.RecordType.Name;

var result2 = sforce.connection.query("select Id,Name from RecordType where name='"+accountrecordtype+"' and SObjectType='Contact'");
var conRecordType = result2.getArray("records")[0];
var recordType = ''+conRecordType.Id;

window.open( '/003/e?retURL=%2F{!Account.Id}&accid={!Account.Id}&RecordType='+recordType);



 
Paras JainParas Jain
changed ajax/30.0 to ajax/29.0  now works fine...but i have to open in in new window.... but window.open not works.. pls tell if u knw ..thnks