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
snyhansnyhan 

Account is not defined error

I'm extremely new in attempting to use AJAX and Javascript and am having quite a bit of trouble creating a button that will allow for the conversion of a business account to a person account. I found some code on the developer board got an error after copying and pasting the original script. Since then, I have continued to modify the code but am still getting error messages that vary depending on the browser I'm using.

 

If I'm using Firefox and click the new button, I get the following error message:

 

"A problem with the OnClick JavaScript for this button or link was encountered:

Account is not defined"

 

However, if I'm using Internet Explorer, I get a different error message:

 

"A problem with the OnClick JavaScript for this button or link was encountered:

 

Syntax error"

 

I'm not sure why I'm getting a different error message from the different browsers and I cannot figure out where I went wrong in the code. If someone could please help me out I'd really appreciate it as I've been working on this for the past 3 days and have gotten nowhere. The script I am trying to use can be found below:

 

<script src="/soap/ajax/22.0/connection.js" type="text/javascript"></script>
var ready = true;
if(ready && confirm("Are you sure you want to convert this Account and Contact to a single Person Account? This is NOT reversible.")) {
var account = new sforce.SObject('Account');
Account.Id = "{!Account.Id}";
Account.RecordTypeId = "012A00000004E7C";
result = sforce.connection.update([Account]);
alert(result);
location.reload(true);
}

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

I modified the code where I'm Dynamically querying the RecordtypeId of the Person Acount and assigning it to Account:

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

var AccId = "{!Account.Id}";
alert('Fetched directly - '+AccId);

PersonAccountRecordtypeId = sforce.connection.query("SELECT Name, SobjectType, IsPersonType FROM RecordType WHERE SobjectType='Account' AND IsPersonType=True limit 1").getArray("records")[0].Id;
alert('Getting Id from Query - '+PersonAccountRecordtypeId);

var AccountObj = new sforce.SObject('Account');
AccountObj.Id = AccId;
AccountObj.Phone = 12345;
AccountObj.RecordtypeId = PersonAccountRecordtypeId;
result = sforce.connection.update([AccountObj]);
alert(result);
location.reload(true);

 As you said you weren't able to fetch the AccountId, try this then:

 

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

var AccId = "{!Account.Id}";
AccountId = sforce.connection.query("Select Name, Id from Account Where Id = '"+ AccId +"' ").getArray("records")[0].Id;
alert('Getting Id from Query - '+AccountId);

var AccountObj = new sforce.SObject('Account');
AccountObj.Id = AccountId;
AccountObj.Phone = 12345;
AccountObj.RecordtypeId = PersonAccountRecordtypeId;
result = sforce.connection.update([AccountObj]);
alert(result);
location.reload(true);

 Hope it helps.

All Answers

Rahul SharmaRahul Sharma

Are you sure that you have also changed the recordtype of PersonAccount in below code?:

 

Account.RecordTypeId = "012A00000004E7C";

nyhansanyhansa

Hi Rahul,

 

I'm not sure why I'm signed in with a different username than the one I used to post, however I am the same person as snyhan...

 

The record type ID I listed in my original post (Account.RecordTypeId = "012A00000004E7C";) is an update I made to the script I found on one of the boards. I found the Person Account Record Type Id by going to Setup -> Customize -> Accounts -> Person Accounts -> Record Types -> [Click on desired record type] -> copy ID from URL (was this not the right place to look?). After some changes, I am no longer receiving the "Account is not defined..." error. I have been individually copying and pasting the Account ID into the button script and converting my accounts to person accounts one by one. I've included the updated script below. For some reason the {!Account.Id} merge field from the original script will not pull the account IDs so instead I have to edit the button each time I want to use it and paste the specific Account ID into the script.

 

<script src="/soap/ajax/22.0/connection.js" type="text/javascript"></script>
var ready = true;
if(ready && confirm("Are you sure you want to convert this Account and Contact to a single Person Account? This is NOT reversible.")) {
var AccountObj = new sforce.SObject('Account');
AccountObj.Id = '001A000000Pwjl4';
AccountObj.RecordTypeId = '012A00000004E7C';
result = sforce.connection.update([AccountObj]);
alert(result);
location.reload(true);
}

 

Thank you for any help you can provide!

Sam

Rahul SharmaRahul Sharma

I modified the code where I'm Dynamically querying the RecordtypeId of the Person Acount and assigning it to Account:

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

var AccId = "{!Account.Id}";
alert('Fetched directly - '+AccId);

PersonAccountRecordtypeId = sforce.connection.query("SELECT Name, SobjectType, IsPersonType FROM RecordType WHERE SobjectType='Account' AND IsPersonType=True limit 1").getArray("records")[0].Id;
alert('Getting Id from Query - '+PersonAccountRecordtypeId);

var AccountObj = new sforce.SObject('Account');
AccountObj.Id = AccId;
AccountObj.Phone = 12345;
AccountObj.RecordtypeId = PersonAccountRecordtypeId;
result = sforce.connection.update([AccountObj]);
alert(result);
location.reload(true);

 As you said you weren't able to fetch the AccountId, try this then:

 

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

var AccId = "{!Account.Id}";
AccountId = sforce.connection.query("Select Name, Id from Account Where Id = '"+ AccId +"' ").getArray("records")[0].Id;
alert('Getting Id from Query - '+AccountId);

var AccountObj = new sforce.SObject('Account');
AccountObj.Id = AccountId;
AccountObj.Phone = 12345;
AccountObj.RecordtypeId = PersonAccountRecordtypeId;
result = sforce.connection.update([AccountObj]);
alert(result);
location.reload(true);

 Hope it helps.

This was selected as the best answer
krish4ukrish4u

Hi,

 

This is Krishna how to get product id directly.

 

I am using {!product2.id} ,but i am getting null .

 

pls suggest on this.

 

 

Regards,

Krishna

Rahul SharmaRahul Sharma

Can you elaborate more on What are you trying to do.

krish4ukrish4u

Hi,

 

Actually i am getting current page Account id and product id.

 

var r="{!Account.Id}" ;     ---> using this we can get current page account id. same way i want to get product id. For that  I used var x="{!Product2.id}".

 

I am getting account id and unable to getting Product id. Product id showing null.

 

For suppose I have two Account Lookups in current page how can i get particular account id.

 

Give suggestions..............

 

thanks,

Krish

forcelearnerforcelearner
Thanks, It helped me too!!