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
CloudboxCloudbox 

Apex class and trigger help - On creating a new record

I am test designing a custom application and have it all configured nicely, but need some help with the code as I'm a spring newbie in this area.

 

I am very familiar with HTML etc and have recently been researching apex/java style syntax and structure and it's a very slow process.

 

I understand you need to have classes defined and then you can create trigger scripts and visualforce pages that interact with the classes.

 

I understand the class types and the basic concept of methods but the details here-in are where I am finding it hard to self teach myself - Let alone achieve my goal.

 

Should I be using IDE? How does one find the correct code operators and how to use them? Anyway I find myself typing in lines of code just guessing and realising I'm like a blind man feeling around in a room of buttons hoping I press the right one. I have done the fundamentals course and are trying to get my head around the cook book. But I'm getting into the hard code stuff now.

 

I would be grateful if someone could shine a torch around for me in my current goal before I go for a personal educational course on this. 

 

My Goal:

 

In a force.com custom app I have an Object (tab - arbitrarily called "Items") that creates an item as an auto number record (on clicking new button)

 

Then takes me to the create page where I specify the related contact / account and other info

 

I have a number field in this object and which also appears on the create page which is "Number of items"

 

Now the idea is that the information entered on the create page will look at the "Number of items" and then create identical records from the entered information, but with unique record entries in the database.

 

For example: I fill in the item details in the fields on the create page and specify "Number of items = 5" (type '5' in the field)

 

I want it to create 5 new item records using the Auto Number record sequence


ITEM-001

ITEM-002

ITEM-003

ITEM-004

ITEM-005

 

Seems logical and simple enough, but making it happen is proving difficult for me.

 

I figure, I need to use a trigger to replicate the record, so use 'before insert' to kick in when the save button is pressed to create the record(s) and some how have it look at the field with the amount of iterations and have some code that makes it 'stamp' that many iterations. And that I need to define this as a class.

 

Either that or I could create a visualforce wizard page that looks more familiar to me from my html coding experience to replace the standard record create page (on clicking new) where I need to define the class as well.

 

 

What I am wondering - Is this a simple coding task that I am just not good enough yet to understand? Considering I have been researching for a day now and have not come much closer. Is this something that someone out here could think "Oh yes you use this and that and there you go" or does this kind of thing take many days/hours of development?

 

If it is a straight forward hand full of lines could someone show me some code so I can attempt to reverse engineer it for my understanding. Or even the way I should structure the class and what code operators I should be looking at.

 

I feel like I have the idea in the logic but I can't translate it into code.

 

Any help is much appreciated as I really want to learn how to do this rather than pay to have it done for me. (I'm trying to learn to be a developer even though I'm totally newb). Even advise on how to learn for someone at my level

 

TIA

 

 

Best Answer chosen by Admin (Salesforce Developers) 
IanRIanR

Hi,

 

Just to get gonig, start at http://developer.force.com/ This will point you to documentation for Apex and Visualforce. I have both these links bookmarked and refer to them regularly; particularly when you start building pages, you'll find yourself referring to the Standard Component Reference in the VF guide all the time...

 

 

Now, to answer your question:

 

Typically, if you were building a maintenence screen for a custom object, you would set a standard controller for your page and name it as per the name of your custom object; then you would be binding to properties on your object, e.g.

 

 

 

<!-- Page: --><apex:page standardController="Account"> <apex:form> <apex:pageBlock title="My Content" mode="edit"> <apex:pageBlockButtons> <apex:commandButton action="{!save}" value="Save"/> </apex:pageBlockButtons> <apex:pageBlockSection title="My Content Section" columns="2"> <apex:inputField value="{!account.name}"/> <apex:inputField value="{!account.site}"/> <apex:inputField value="{!account.type}"/> <apex:inputField value="{!account.accountNumber}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>

 

 

 

 (which is pasted straight out the documentation - but 'Account' in this example could easily be 'Item__c' )

 

 

 

However - If you want to create multiple items in one go, of identical data, you'll need to approach it a little differently.

 

Firstly, you'll need to set the Name field on your custom object to be 'Auto Number' and specify the Display Format. I inferred from your question that you have already done this.

 

Next, you'll need to add a property to your controller to set the number of items you wish to create.

 

Finally, you'll need to write the method that will clone the current in-memory object and place it in a collection, then insert that collection to the database.

 

 

e.g.

 

 

public class MyController { private MyObject__c myObject; public integer NumberOfItems { get; set; } // Constructor public MyController(ApexPages.StandardController stdController) { myObject = (MyObject__c) stdController.getRecord(); } public PageReference CreateItemCollection() { List<MyObject__c> items = new List<MyObject__c>(); items.add(myObject); for (integer i=0; i< NumberOfItems-1; i++) { // Clone the item: // param1 - preserve Id; set false as we want to create a new instance in the db // param2 - deep Clone; set true to preserve relationships to other SObjects, or false to discard. MyObject__c itemClone = myObject.clone(false, false); items.add(itemClone); } insert items; return null; } }

 

 and in your page:

 

  

<!-- Page: --><apex:page standardController="MyObject__c" extensions="MyController"> <apex:form> <apex:pageBlock title="My Content" mode="edit"> <apex:pageBlockButtons> <apex:commandButton action="{!CreateItemCollection}" value="Create Items"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Specify number of items to create" columns="1"> <apex:pageBlockSectionItem> <apex:outputLabel value="Number of Items" /> <apex:inputText value="{!NumberOfItems}" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection title="My Content Section" columns="2"> <apex:inputField value="{!myObject__c.Field1__c}"/> <apex:inputField value="{!myObject__c.Field2__c}"/> <apex:inputField value="{!myObject__c.Field3__c}"/> <apex:inputField value="{!myObject__c.Field4__c}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>

 

 

 

 

 Where field1,2,3,4 would be the fields you have defined against your custom object...

 

Anyway - that's one way to skin this particular cat... 

 

HTH, Ian Randall

 

:) 

All Answers

IanRIanR

Hi,

 

Just to get gonig, start at http://developer.force.com/ This will point you to documentation for Apex and Visualforce. I have both these links bookmarked and refer to them regularly; particularly when you start building pages, you'll find yourself referring to the Standard Component Reference in the VF guide all the time...

 

 

Now, to answer your question:

 

Typically, if you were building a maintenence screen for a custom object, you would set a standard controller for your page and name it as per the name of your custom object; then you would be binding to properties on your object, e.g.

 

 

 

<!-- Page: --><apex:page standardController="Account"> <apex:form> <apex:pageBlock title="My Content" mode="edit"> <apex:pageBlockButtons> <apex:commandButton action="{!save}" value="Save"/> </apex:pageBlockButtons> <apex:pageBlockSection title="My Content Section" columns="2"> <apex:inputField value="{!account.name}"/> <apex:inputField value="{!account.site}"/> <apex:inputField value="{!account.type}"/> <apex:inputField value="{!account.accountNumber}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>

 

 

 

 (which is pasted straight out the documentation - but 'Account' in this example could easily be 'Item__c' )

 

 

 

However - If you want to create multiple items in one go, of identical data, you'll need to approach it a little differently.

 

Firstly, you'll need to set the Name field on your custom object to be 'Auto Number' and specify the Display Format. I inferred from your question that you have already done this.

 

Next, you'll need to add a property to your controller to set the number of items you wish to create.

 

Finally, you'll need to write the method that will clone the current in-memory object and place it in a collection, then insert that collection to the database.

 

 

e.g.

 

 

public class MyController { private MyObject__c myObject; public integer NumberOfItems { get; set; } // Constructor public MyController(ApexPages.StandardController stdController) { myObject = (MyObject__c) stdController.getRecord(); } public PageReference CreateItemCollection() { List<MyObject__c> items = new List<MyObject__c>(); items.add(myObject); for (integer i=0; i< NumberOfItems-1; i++) { // Clone the item: // param1 - preserve Id; set false as we want to create a new instance in the db // param2 - deep Clone; set true to preserve relationships to other SObjects, or false to discard. MyObject__c itemClone = myObject.clone(false, false); items.add(itemClone); } insert items; return null; } }

 

 and in your page:

 

  

<!-- Page: --><apex:page standardController="MyObject__c" extensions="MyController"> <apex:form> <apex:pageBlock title="My Content" mode="edit"> <apex:pageBlockButtons> <apex:commandButton action="{!CreateItemCollection}" value="Create Items"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Specify number of items to create" columns="1"> <apex:pageBlockSectionItem> <apex:outputLabel value="Number of Items" /> <apex:inputText value="{!NumberOfItems}" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection title="My Content Section" columns="2"> <apex:inputField value="{!myObject__c.Field1__c}"/> <apex:inputField value="{!myObject__c.Field2__c}"/> <apex:inputField value="{!myObject__c.Field3__c}"/> <apex:inputField value="{!myObject__c.Field4__c}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>

 

 

 

 

 Where field1,2,3,4 would be the fields you have defined against your custom object...

 

Anyway - that's one way to skin this particular cat... 

 

HTH, Ian Randall

 

:) 

This was selected as the best answer
CloudboxCloudbox

IanR, Thanks for showing me this, it works alright, that 'cat is skinned'.

 

I have spent some time looking at how it works and I'm starting to get the picture. I understand the visualforce page code more so but still trying to work out the class code. I'm going to start here and research how to structure it and put things together.

 

Thanks for the start, it's awesome to look at and work with something that works, to get a picture on the way to implement something from and idea.

 

Cheers :)