1

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

2 Answers 2

1

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;
1
  • Hii Sir I have tried ur technique and it is working perfectly .I also tried one more way and with that technique also I was able to achieve the requirement.I am posting my updated code below Commented May 25, 2017 at 4:59
1

I am posting my updated code below: The functionality is working perfectly now:

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var comAcc = "{!sked__Resource__c.Name}";
var account=sforce.connection.query("Select Id, Name,Resource__c From 
Account Where  Resource__c='"+"{!sked__Resource__c.Id}"+"'"); 
try{
if( account.size>0) { 
alert('An Account  already exists corresponding to this resource.You cannot 
create a new account'); 
} else {
var connection = sforce.connection;
var recordtypeid=connection.query("Select Id, Name From RecordType Where 
SobjectType = 'Account' AND Name = 'Employees'");
var GeneralLedgerAccountReceivable=connection.query("Select Id,Name From 
c2g__codaGeneralLedgerAccount__c Where Name='"+"1200 - Trade Debtors"+"'");
var records = recordtypeid.getArray("records");
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__CODAAccountTradingCurrency__c="AUD";


newaccount.c2g__CODAAccountsReceivableControl__c=GeneralLedgerAccountReceivable.records.Id;
     result = sforce.connection.create([newaccount]);
     alert('A new account is created corresponding to this resource' );
      }
      }
      catch(err)
     {alert(err.message); 
      }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .