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
Christianna GrinterChristianna Grinter 

Trigger to update a contact look up field on Opportunity based on Account

Hi,

I am trying to write my first trigger and I am running into major roadblocks!  I have done some testing but I think the problem is a little too much for a novice.

Summary:
I am the admin for a group that does mobile advertising.  In mobile advertising there is often an agency that will buy ads on behalf of the client.  I am trying to pull in the billing information from the account page into the opportunity automaticaly for the sales and billing teams.  We will often have multiple opportunities for each client (ex.  Summer campaign, Back to School campaign, Holiday campaign, etc.) and it does not make sense to enter the billing information on the opportunity each time.  It should be entered on the account page once and then pulled into each opportunity automatically.  

On the Account page, in the related contact list, a new contact is created and the billing information is entered.  We have 2 fields on the opportunity page that are both account look up fields (see scenarios below).

Goal:  On the opportunity, have the billing contact look-up field automatically grab the related billing contact from the account page


Oh yea, some other items that make this kind of a pain:
  • I am on the enterprise edition but I am part of a larger org (I am 1 of 50 or so business units)
    • This means we have to share fields and it is easy to reach the limits - also means it is sometimes hard to get new fiels on popular objects
  • Account page
    • Will not be able to get any additional fields to enter billing contact information, I was told to enter it in the related contact list
  • Contact page
    • I was able to get custom picklist values added to a field to flag that is it a billing contact for our business unit
  • Reporting
    • Since there are two account look up fields, anytime you try to run an opportunity and account report it will only pull in information for the client account, not the agency account.  I need to pull in the billing contact on the opportunity to create the look up for reporting to work (see scenarios below)
  • Record Types
    • You basically need to include a record type for everything.  This rule should only fire for our record type and should not be applied to any other business unit.


Scenario 1:  Client is buying ads direct and will be billed directly
Account Field:  (look up client)
Partner Field:  (would be blank since there is no agency)

Scenario 2:  Agency is buying ads for the client - agency will be billed by us, they will then in turn bill the client on their own
Account Field:  (look up client)
Partner Field:  (look up agency)


Opportunity Fields:
Account Name (Account)
Partner (Partner__c)
Billing Contact (Billing_Contact__c)
Opportunity Record Type:  Ad Sales

Contact Fields:
Name (First Name and Last Name - not sure how Salesforce combines them into one field)
Contact Type (Contact_Type__c) (picklist value of "Billing")



If there is a partner, grab the billing contact from the partner account page.  If there is no partner, grab the billing contact from the client account page.  The trigger should fire every time the opportunity changes stages.  Sometimes we do not have the billing information when the sales rep is first working with the client (50% or less opportunity probability).  By the time the opportunity gets to 75% or higher though we are getting a contract in place and then is when the billing information is generally listed on the contract.  

I was going to create a separate validation rule that would not allow the opportunity to go to 100% unless the billing contact look-up field had a value entered - i.e. the sales rep entered the billing information on either the client account or the agency account page.


I think that is everything but let me know if I missed something that helps to expalin this more!

Best Answer chosen by Christianna Grinter
Abhi_TripathiAbhi_Tripathi
Hi Christianna,

Go through this post, it will help writing your first trigger after that post your code, I'll be happy to help you with that 
http://abhithetechknight.blogspot.in/2014/06/basics-of-apex-trigger-for-beginners.html

Regards,
Abhi Tripathi
SFDC Certified Developer

All Answers

Abhi_TripathiAbhi_Tripathi
Hi Christianna,

Go through this post, it will help writing your first trigger after that post your code, I'll be happy to help you with that 
http://abhithetechknight.blogspot.in/2014/06/basics-of-apex-trigger-for-beginners.html

Regards,
Abhi Tripathi
SFDC Certified Developer
This was selected as the best answer
Christianna GrinterChristianna Grinter
thanks for the info - I will definitely use that post, it looks like a great resource.