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
Scott PhillipsScott Phillips 

Simple Apex trigger reverse lookup of object

I am new to attempting apex triggers and just starting out.  I need a VERY SIMPLE, BASIC apex and I've spent hours reading through complex examples and not finding anything for what I need.  Here's my case, could anyone help?

2 Objects:  Contact (standard) and Offer (custom).

The Offer object has a lookup field to the Contact which is selected manually by the user.  
I created a custom lookup relationship field on the Contact called "Offer record."

What I want is an apex that when an Offer is created, the apex will lookup that Contact and populate the Offer record onto the field "Offer record."  

Using abbreviated examples:  So I create record OFR-2468 and using the lookup field choose Contact Joe Smith. I click save on the Offer record.  The apex should go to Joe Smith's Contact record and populate his field "Offer record" with the same offer just created, "OFR-2468."  So you could click back and forth between the two and I could write formula fields on the Contact if I need to.  

I am not concerned if there are multiple records created for the same contact or anything special.  I don't need special criteria or conditions.   Just a reverse lookup to populate that object record and if that is successful I can think about getting more complex.

I appreciate any help you could give.  Thanks in advance.
 
PuneetsfdcPuneetsfdc
There are few questions which need to be answered before approaching for any implementation

1. You have contact lookup field on Offer and lookup field "Offer Record" on contact,
=>so Why you want to create lookup field on Contact when you can access the offer record from related list.

2. What will happen when you will have multiple more that two offers for same Contact,
=> contact field on offer will have correct contact but what about "Offer Record " field on Contact
   are you going to overwrite the value or create multiple lookup field on Contact ?
 
Scott PhillipsScott Phillips

Hi Puneet,

Thanks for the response.  As I indicated in the OP, I'm not concerned about multiple records.  If the contact received a 2nd offer in the future, most likely it is an internal promotion.  For the purposes of using this apex the multiple records won't affect what I need.  I need a one time update to a formula field to indicate the contact ever received an offer (once or multiple times doesn't matter), which will feed/trigger some other things I want to do including update fields on a 3rd object that I can't do straight from offer.  I have to connect them.

All I need is an apex that links the related offer record onto the contact record.  I literally don't need any other concern, I just need to know how to do that only.  I don't care if it overwrites with future offers or any of that.  Every example I've spent hours reviewing is very complex and all I want is an apex that puts the offer record on the contact when the contact is put on the offer.  

Would anyone be able to help me with that?

Thanks in advance.

PuneetsfdcPuneetsfdc
try out the code below, hope it help you to solve your problem. Can change it according to your requirement.

TRIGGER:

trigger offerTrigger on Offer__c (after insert, after update) {
    TriggerHandler handler = new TriggerHandler();   
    if(trigger.isAfter) {
        if(trigger.isInsert || trigger.isUpdate) {
            // Single Record
            for(Offer__c o : Trigger.new) {
                handler.updateContactOfferRec(o);
            }
           
            /*  FOR Bulk Handle  
                handler.updateContactRecords(trigger.newMap);
             */      
        }
    }   
}


CLASS:
public class TriggerHandler {
   
    @TestVisible Contact contact;
    @TestVisible List<Contact> contactList;
   
    public void updateContactOfferRec(Offer__c offer) {
        contact = new Contact();
        if(offer.Contact__c != null) {
            contact = [SELECT Id, Offer__c FROM Contact WHERE Id =: offer.Contact__c];
            contact.Offer__c = offer.Id;
        }
        update contact;
    }
   
    public void updateContactRecords(Map<Id, Offer__c> offerMap) {
        Map<Id, Offer__c> newMap = new Map<Id, Offer__c>();
        contactList = new List<Contact>();
        // assuming Contact on Offer will never be blank otherwise place a check
        for(Offer__c o : offerMap.values()){
            newMap.put(o.Contact__c, o);
        }
        contactList = [Select Id, Offer__c From Contact WHERE Id IN: newMap.keySet()];
        for(Contact contact : contactList ) {
            if(newMap.containsKey(contact.Id)){
                contact.Offer__c = newMap.get(contact.Id).Id;
            }
        }
        update contactList;
    }
}
 
Scott PhillipsScott Phillips
Thank you so much! I will try this. Scott Phillips