[SalesForce] OnClick javascript: Getting an error “Cannot set property “Id” of undefined”

Hi I have added a custom button to my custom object "Resource"

Now with the click of the button an account is created with the same name as the name of the "Resource".

The Account object has a lookup field for the Resource. With the click of this button the lookup field is also updated .

But I have another lookup of a custom object "General Ledger Account" on my "Account" object .

My requirement is that the this lookupfield should be auto populated with the value "1200 – Trade Debtors" (This is the name of a "General Ledger Account" ).

I am not able to auto populate this lookup field with javascript.Rest all the fields are getting populated as per requirement

I am posting the code here

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var comAcc = "{!sked__Resource__c.Name}";
var connection = sforce.connection;
var recordtypeid=sforce.connection.query("Select Id, Name From RecordType 
Where SobjectType = 'Account' AND Name = 'Employees'");

var GeneralLedgerAccountReceivable=sforce.connection.query("Select Id,Name 
From c2g__codaGeneralLedgerAccount__c Where Name='"+"1200 - Trade 
Debtors"+"'");
alert(GeneralLedgerAccountReceivable);

var records = recordtypeid.getArray("records");
alert(records);

var records1 = GeneralLedgerAccountReceivable.getArray("records1");
alert(records1);

var newaccount= new sforce.SObject("Account");
newaccount.RecordTypeId=records[0].Id;
newaccount.Name = "{!sked__Resource__c.Name}" ;
newaccount.Resource__c= "{!sked__Resource__c.Id}" ;
newaccount.c2g__CODAAccountsReceivableControl__c=records1[0].Id;
alert(newaccount.c2g__CODAAccountsReceivableControl__c);

alert(result);

I am getting an error"Cannot set property "Id"

I have posted a similar post before but that issue was solved .Now this is a totally different problem I am facing.Please guide me with this .Thanks in advance

Here,

c2g__codaGeneralLedgerAccount__c is the Object "General Ledger Account"

c2g__CODAAccountsReceivableControl__c is the lookup field of the Object "General Ledger Account" in Account

sked__Resource__c is the name of the Resource object

Best Answer

The issue I can see due to copy pasting or typo error.

You are retrieving GeneralLedgerAccountReceivable where is no issues.

var GeneralLedgerAccountReceivable=sforce.connection.query("Select Id,Name 
From c2g__codaGeneralLedgerAccount__c Where Name='"+"1200 - Trade 
Debtors"+"'");
alert(GeneralLedgerAccountReceivable);

In the below line you have written getArray("records1"). Here is the issue.

var records1 = GeneralLedgerAccountReceivable.getArray("records1");
alert(records1);

It should be

var records1 = GeneralLedgerAccountReceivable.getArray("records");

which you are using at this line:

newaccount.c2g__CODAAccountsReceivableControl__c=records1[0].Id;
Related Topic