+ Start a Discussion
VeMan1542VeMan1542 

Accessing Opportunity Contact Role information using APEX (for Triggers)

We have a problem with sales people not adding a Contact to the Contact Role in Opportunities after an Opp has significantly progressed. As I am apparently unable to access the Contact Role section of an Opportunity using a Workflow or a Validation Rule (please correct me if I am wrong), it made sense to use an Apex trigger to update the Opportunity StageName to the term used for a 20% Probability.
 
The idea is to check for both newly created opportunities and edited existing opportunities:
              
 
 
 
trigger ContactRoleCheck on Opportunity (after insert, after update) {

 for(Opportunity newOpp : Trigger.New){
 if (newOpp.RecordTypeID == '012500000009EaPAAU'){
   if((newOpp.Probability > 20) && (newOpp.ContactID == null)) {
    newOpp.StageName = 'Research';}}
 else if (newOpp.RecordTypeID == '012500000009EbhAAE'){
   if((newOpp.Probability > 20) &&  (newOpp.ContactID == null)) {
    newOpp.StageName = 'Qualified Lead';}}
 
 for(Opportunity oldOpp : Trigger.Old){
 if (oldOpp.RecordTypeID == '012500000009EaPAAU'){
   if((oldOpp.Probability > 20) &&  (oldOpp.ContactID == null)) {
    oldOpp.StageName = 'Research';}}
 else if (oldOpp.RecordTypeID == '012500000009EbhAAE'){
   if((oldOpp.Probability > 20) &&  (oldOpp.ContactID == null)) {
    oldOpp.StageName = 'Qualified Lead';}}
  }
 }
}
VeMan1542VeMan1542

I just found out that hitting the tab button will submit the post early...

As I was saying, in the following code, the system doesn't recognize either newOpp.ContactID, newOpp.Contact, or newOpp.ContactRole.

Interestingly, I have written a URL for a button to send emails from Opportunities which used 'ContactID' to enter the primary contact for an opportunity (as set forth in the Contact Role section) which worked quite nicely. However, APEX doesn't seem to recognize the same field. Can someone help me out with the correct APEX reference to a primary Opportunity contact so I can get this to work. And. please, any feedback on the approach I'm taking with the APEX code would be appreciated. Again, my attempted APEX trigger script is:

 

trigger ContactRoleCheck on Opportunity (after insert, after update) {


 for(Opportunity newOpp : Trigger.New){
 if (newOpp.RecordTypeID == '012500000009EaPAAU'){
   if((newOpp.Probability > 20) && (newOpp.ContactID == null)) {
    newOpp.StageName = 'Research';}}

 else if (newOpp.RecordTypeID == '012500000009EbhAAE'){
   if((newOpp.Probability > 20) &&  (newOpp.ContactID == null)) {
    newOpp.StageName = 'Qualified Lead';}}
 
 for(Opportunity oldOpp : Trigger.Old){
 if (oldOpp.RecordTypeID == '012500000009EaPAAU'){
   if((oldOpp.Probability > 20) &&  (oldOpp.ContactID == null)) {
    oldOpp.StageName = 'Research';}}

 else if (oldOpp.RecordTypeID == '012500000009EbhAAE'){
   if((oldOpp.Probability > 20) &&  (oldOpp.ContactID == null)) {
    oldOpp.StageName = 'Qualified Lead';}}

  }
 }
}