+ Start a Discussion
Matthew HamptonMatthew Hampton 

Simple Lookup Trigger Help



I need help writing a basic trigger (at least I think it is basic, I am still new):


I have two Custom Objects:





What I would like to do is:


1. From the field Name on GPON_Address__C, lookup to see if there is a matching record in Household_Address__C where the name is the same.


2. If there is a match, return the value from Customer_Name__c that is in Household_Address__C and put it in the Customer__c field on GPON_Address__C.


Long story short, I just want to do the equivelant of a VLOOKUP using GPON_Address__C.name as the reference and using Household_Address__C.name as the match and return Household_Address__C.customer_name__c.


Is this possible?


Hope this was clear, any help writing the code would be greatly appreciated.







Try something like the following --


String GPONName = GPON_Address.name; // Assumption is that GPON_Address is the name of the variable that we are processing.

Household_Address__C householdAddress = [select Household_address__C.Customer_Name__c from Household_Address__C where Household_address__C.name = :GPONName];

if(householdAddress != null)
   GPON_Address.Customer_Name__c =  householdAddress.Customer_Name__c;

Imran MohammedImran Mohammed

As you will be doing this in a trigger, you should be careful else the trigger will be recursive one and you may end up hitting the governor limits.

Use Trigger.Old and Trigger New while doing this to make a check whether Customer_Name__c in both the objects are not same. Then copy the value and update the object.


Hope this helps.



Matthew HamptonMatthew Hampton

Got it working, thanks for the help!