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
hemmhemm 

Apex code example request: copy values from Account to Contact when the Contact is created

I have tried reading through the APEX developer guide and, at this time, most of it is flying high over my head.  I don't have a Java background, so it will take me a bit to ramp up.  I have a need now and am hoping that the development community can help me out.

We are using custom address fields on our Account, Contact and Lead objects so we can have picklists on Country.  We need this design to aid in Lead Assignment rules, Territory Manager rules and Validation Rules to enhance address quality.  One of the downsides of doing this is losing the feature where the Account address automatically copies to the Contact when clicking the New button on the Contact Related List from an Account.

I am wondering if anyone can get me pointed in the right direction (or even write the code for me and post it here :smileyvery-happy:).  I am thinking one of the 2 following solutions would be best:

  1. (preferred) Override the new button on a Contact with an S-Control that calls an Apex code package.  The package should copy the Account address and have it already populated on the contact form when it appears.  This would allow a user to still change the address before saving the contact.
  2. Trigger on the save event of a contact - check to see if the address fields are null.  If so, copy the account address.


michaelforcemichaelforce

hemm,

I have not yet gotten my hands dirty with Apex code, but I can offer this...

Since the 'save' button cannot be overridden BUT the 'new' button can...  For my company, I have built my own creation form to spoof the existing form.  This form is exposed when you click 'new'... then you put in whatever code you want behind the save button (because you built the form).  In your example, that code would be a call to an Apex function.

 

mtbclimbermtbclimber
Apex Code can not populate values in a record before a user has access to the form and there is no UI component to Apex Code today. All Apex Code magic is behind the scenes.

You can certainly use Apex Code in triggers to copy the values from your custom address fields to the standard ones but defaulting your custom fields on contact from either the custom or standard address fields on account will require an override of the new button, as michaelforce suggests.
The_FoxThe_Fox
Hi Hemm,

It is definitely possible to make Country Picklist I have Made It through Ajax and for any Country field on any object you want, just a declaration at the beginning of the js code and that's it.

Regards