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
AgilidiRPAgilidiRP 

Javascript button to create records and populate lookup field

Non-developer needs help!  I am trying to create a custom button with javascript to create records on a list view.  The new records are Invoices that are details of master-detail relationships to Programs and Accounts.

 

The Create New Invoice button is on the Programs list view.  The Account is a lookup field within the Program and it is called Billing_Account__c.  The button will work if I hard code a Account Id into "newInvoice.Billing_Account__c = "xxxx";" but otherwise I can't get it to work.  Any ideas?

 

Also, are there any other best practices that I should incorporate in the button, such as "8 Programs were selected, but only 4 Invoices were created."?

 

Thanks in advance.

 

{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Program__c)};
var newRecords = [];

if (records[0] == null) {
alert("Please select at least one record to update.");
} else {
for (var a=0; a<records.length; a++) {
var newInvoice = new sforce.SObject("Invoice__c");
newInvoice.Program__c = records[a];
newInvoice.Billing_Account__c = "{!Program__c.Billing_AccountId__c}"; //This line is the problem
newRecords.push(newInvoice);
}
result = sforce.connection.create(newRecords);
parent.location.href = url; //refresh the page
}

Ashish_SFDCAshish_SFDC
Hi Agilidi, 


Thsi is not a best practice for Salesforce, its recommended to use a Standard Validation Rule on that custom object. 

https://help.salesforce.com/HTViewHelpDoc?id=fields_about_field_validation.htm&language=en_US


Regards,
Ashish
blucasblucas
Is your list view of Programs showing on an individual Account page? If so, then the Billing Account for all the listed Programs should be the same. You should be able to use
newInvoice.Billing_Account__c = "{!Account.Id}";
instead of 
newInvoice.Billing_Account__c = "{!Program__c.Billing_AccountId__c}";