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
Matthew HamptonMatthew Hampton 

Object Update Help

All:

 

I have the following trigger built to update a field on a custom object based on a field match from a second custom object:

 

trigger HouseholdAddressUpdate on Household_Address__c (after update, after insert) {
list<GPON_Address__c> gpon = [select id,name,customer_name__c from GPON_Address__c where name=:Trigger.new[0].name];

if(gpon.size()>0)
{
gpon[0].customer_name__c = Trigger.new[0].customer_name__c;

update gpon;

}
}

This trigger is currently set to run when a record on object Household_Address__C is inserted or updated. I would like to have the trigger activated if a record on EITHER of the custom objects is inserted or updated.

 

Any help would be appreciated.

 

Thanks,

 

Matt

Imran MohammedImran Mohammed

Then you should have trigger on both the objects.

But you should be little careful while doing this.

In your trigger code, you should make a check using Trigger.old and Trigger.new, else it will become recursive and hit the governor limits.

Matthew HamptonMatthew Hampton

Appreciate the response.

 

I am still a novice when it comes to trigger. Can you give me a brief description of what trigger.old vs trigger new is and where I should use it?

 

Thanks!

 

Matt

Matthew HamptonMatthew Hampton

Re-reading my original post, I am not sure if I was making sense.

 

I would like the trigger to run to update GPON_Address__C with information from Household_Address__C when either Household_Address__C OR GPON_Address__C is updated or inserted.

 

I do not know if this is possible or if so, how to write the trigger.

 

Thanks in advance,

 

Matt

Imran MohammedImran Mohammed

Hi,

 

The trigger code should be able to handle bulk records.

So, i modified the code which is below. Let me know if its working and i will update the comments so that you can understand each line of code.

 

trigger HouseholdAddressUpdate on Household_Address__c (after update, after insert) {

 

String[] HouseholdAddressNameList = new String[]{};

Map<String, Household_Address__c> houseHoldMap = new Map<String, Household_Address__c>();

 

For(Household_Address__c ha: Trigger.new)

{

HouseholdAddressNameList.add(ha.name);

        houseHoldMap.put(ha.name, ha);

}

 

GPON_Address__c[] gponToBeUpdateList = new GPON_Address__c[]{};


list<GPON_Address__c> gpon = [select id,name,customer_name__c from GPON_Address__c where name in :HouseholdAddressNameList];


for(GPON_Address__c ga: gpon)
{

Household_Addess__c hac = houseHoldMap.get(ga.name);

        if(hac != null)

        {

 ga.customer_name__c = hac.customer_name__c;

gponToBeUpdateList.add(ga);

        }

}

update  gponToBeUpdateList;
}