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
sebascansecosebascanseco 

Create different custom object's records from custom button

I'd like to have a custom button in Account so when the user clicks it, it can create many new records from a custom object (Invoice) and this new records have to have some fields automatically filled up with data calculated from the account object cutom fields.

 

Account object custom fields:

StartDate

TotalAmount

 

Invoice custom object custom fields

InvoiceStarDate

InvoiceAmount

 

The scenario is the following, a user click on the custom button and the javascript creates 4 Invoices records with the InvoiceStartDate calculated based on the Account.StartDate and the InvoiceAmount calculated from the division of the Account.TotalAmount/4

 

Example

------------

Account.StarDate = 01/01/2012

Account.TotalAmount=12,000

 

Click on "Create invoices" custom button and the code creates the following:

 

Invoice.name = 1

Invoice.StartDate = 01/01/2012

Invoice.Amount=3,000

 

Invoice.name = 2

Invoice.StartDate = 04/01/2012

Invoice.Amount=3,000

 

Invoice.name = 3

Invoice.StartDate = 07/01/2012

Invoice.Amount=3,000

 

Invoice.name = 4

Invoice.StartDate = 10/01/2012

Invoice.Amount=3,000

 

Finally, if it's not too much to ask for, it would be a really nice touch to know how to auto refresh the account page so the invoices related list could get populated. :-)


Shoud I use an apex class for the calculations? How do I do this?

Best Answer chosen by Admin (Salesforce Developers) 
AdrianCCAdrianCC

Hello,

 

An apex class is the way I would go, with a corresponding vf page. Send the Id of the Account as a parameter in the link so you know from which record you get the data. Retrieve the fields that you need via a soql in the constructor. Have a method that creates your 4 Invoices and inserts them. The return type for the method should be PageReference and should link back to the initial account - this should solve your refresh problem as well.

The page will have only the <apex:page> element with an action set to your createInvoices() method.

 

This shouldn't take more than an hour of your time, even with test coverage and deployment... 

:)

 

Happy Friday,

Adi

All Answers

AdrianCCAdrianCC

Hello,

 

An apex class is the way I would go, with a corresponding vf page. Send the Id of the Account as a parameter in the link so you know from which record you get the data. Retrieve the fields that you need via a soql in the constructor. Have a method that creates your 4 Invoices and inserts them. The return type for the method should be PageReference and should link back to the initial account - this should solve your refresh problem as well.

The page will have only the <apex:page> element with an action set to your createInvoices() method.

 

This shouldn't take more than an hour of your time, even with test coverage and deployment... 

:)

 

Happy Friday,

Adi

This was selected as the best answer
sebascansecosebascanseco

Thank you Adrian, I'm really new to SF. Could you be more specific and guide me a little more with the syntax of this solution?