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
cswansoncswanson 

New to APEX and Triggers

Looking for some help on creating a trigger, I'm new to Saleforce.com and haven't used this type of customization before.  Here's what I need to do....

 

On an opporutnity I need to be able to track the opportunity owner role.  You are unable to do this with a basic formula field because it does not drill down on the owner ID to the additional owner fields.  Per some research, I came across a presentation that was given at Dreamforce this year in their "Formula Magic" session where they state it's a 3 step process to pull this field into an opportunity.  (http://www.slideshare.net/Salesforce/formula-magic)

 

Step 1: Create a custom lookup field to the user object
Step 2: Create a simple trigger upon insert of record or update of Owner field to populate a custom user lookup field
Step 3: Create formula fields for any field on user object based on custom user lookup field

 

I've done step 1 and created a custom lookup field in my opporutnity called "OwnerLookup".  I need help with a sample trigger for step 2 that will populate "OwnerLookup" with the opportunity owner name, that way I can easily do step 3 to pull in the role.

 

Can anybody please provide a sample trigger you feel might work?

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
MickleMickle

I'm quite new to writing Apex Triggers as well, but this community helped me develop mine, so I wanted to give back. :)

 

(the implied warning there is since I'm quite new, there may be spots where my code fails, or could be more efficient....)

 

Very very basic trigger. I created a custom field on Opportunity called "User" that is simply a look-up to user. You can modify this to your field name.

 

 

trigger owneridtrig on Opportunity (before update, before insert) {
    for (opportunity o : trigger.New) 
    o.User__c = o.OwnerID;
}

 

 

All Answers

MickleMickle

I'm quite new to writing Apex Triggers as well, but this community helped me develop mine, so I wanted to give back. :)

 

(the implied warning there is since I'm quite new, there may be spots where my code fails, or could be more efficient....)

 

Very very basic trigger. I created a custom field on Opportunity called "User" that is simply a look-up to user. You can modify this to your field name.

 

 

trigger owneridtrig on Opportunity (before update, before insert) {
    for (opportunity o : trigger.New) 
    o.User__c = o.OwnerID;
}

 

 

This was selected as the best answer
cswansoncswanson

Thank you very much, everything worked perfectly!