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
TheMaryCTheMaryC 

Copy field from Opportunity to Contact

Ok, I'm trying to write my first Apex Tricker and am not doing it very wel..  When ever a new opportunity is created, I want to copy a date custom field "Membership_Expires" to another date custom filed with the same name on the contact tab.  Sounds like it should be straight forward, but I keep getting errors.... any help would be greatly appreciated.

Thanks 

 

kerwintangkerwintang

I suggest you post your code and errors here so we can check it out. :)

 

Regards,

Kerwin

TheMaryCTheMaryC

Below is the code...  The error I get is Illegal variable declaration: c.Membership_Expires__c at line 4 column 11

 

trigger UpdateMembersDate on Opportunity (before update) {
List<Account> accountsToUpdate = new List<Account>();
for (Opportunity o:System.Trigger.new) {
  c.Membership_Expires__c = o.Membership_Expires__c;
}
}

kerwintangkerwintang

There's no declaration for variable c in c.Membership_Expires__c.

 

I think you have to retrieve the related Contacts to the Opportunity. However, an Opportunity is related to an Account, which in turn may have many Contacts.

 

 

What i'd do is try something like this (haven't tested this code, though):

 

trigger UpdateMembersDate on Opportunity (before update) {
List<Account> accountsToUpdate = new List<Account>();
for (Opportunity o:System.Trigger.new) {

  for(Contact c:o.Account.Contacts){

    c.Membership_Expires__c = o.Membership_Expires__c;

  }
}
}

 

Have you tried this?

 

Best Regards,

Kerwin

TheMaryCTheMaryC

The error I get with this code is:

Error: Compile Error: unexpected token: c.Account.Contacts at line 5 column 15

kerwintangkerwintang

try o.Account.Contacts instead of c.Account.Contacts. thanks :)

 

Regards,

Kerwin