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
DoubleADoubleA 

Association question (Syntax problem)

I had a question regarding apex syntax:

 

I have a trigger that fires on an activity (insert).  I have a field on an Account record that needs updated once the trigger executes.

 

I'm stuck on the part where if I create this task for a contact, how do I update the field on this contact's associated Account record?  I cannot translate this into syntax.


Can anyone help on this?

Thanks

aalbertaalbert

Since the trigger can be associated to different objects, you should do the following logic:

 

a. First loop through all the Task records in the Trigger.new array to find out which ones are associated to Contacts by looking at the WhoId field.

b. Then once you identified the specific Tasks associated to Contacts, in a single query, query the Contact and related Account information. 

c. Then update all the data in a single DML operation.

 

Here is an untested code snippet:

 

trigger taskTest on Task (after insert, after update) { Set<Id> contactIds = new Set<Id>{}; for(Task t : Trigger.new){ String strId = t.WhoId; if(t.WhoId!=null && strId.substring(0,3)=='003') contactIds.add(t.WhoId); } List<Contact> recordsToUpdate = [select id, firstname, lastname , account.name from Contact where Id IN :contactIds]; for(Contact c : recordsToUpdate){ c.Account.Name = 'update field value here'; } update recordsToUpdate; }

 

 

 

 

DoubleADoubleA

Thanks aalbert, this makes sense.  I'll let you know how my code goes.

Much Thanks

DoubleADoubleA

My code is a bit different, but not working anyhow:

 

 

No exceptions are being thrown, the field on the Account is just not updating (keep in mind the task is only associated to contact, not related to Account)

 

MAP<ID,Contact> mapContact = new MAP<ID,Contact>([Select Id, AccountID, Account.Last_Call_Date__c, Account.Last_Visit_Date__c From Contact Where Id IN : setID]); LIST<Contact> listContact = new LIST<Contact>(); Contact myContact = new Contact(); for(Task t : Trigger.New){ if(contactID.startsWith('003')){ if(t.subject.startsWith('Call')){ myContact = mapContact.get(t.WhoId); //map contact contents to myAccount myContact.Account.Last_Call_Date__c = t.activitydate; //set value for Task's Due Date to Last Call Date field on Account listContact.add(myContact); }else if(t.subject.startsWith('Visit')){ myContact = mapContact.get(t.WhoId); myContact.Account.Last_Visit_Date__c = t.activitydate; listContact.add(myContact); } } }

 

 

Any idea why it is not updating?

DoubleADoubleA

**

Forgot to mention

 

contactID = t.whoid (if whoid is not NULL on the task)

 

 

kerwintangkerwintang

I think you have to make an explicit call to save the Contact or Account. Because the trigger only updates the Task record and does not automatically save the Contact/Account. Have you tried it?

 

Best Regards,

Kerwin

DoubleADoubleA

Hi aalbert,

I actually tried using your code just to test around and I noticed the account record not updating

 

 

 

trigger AccountUpdateonContactsTask on Task (after update, after insert) { SET<ID> setContactID = new SET<ID>(); string contactID = ''; for(Task t : Trigger.New){ if(t.whoid!=null){ contactID = t.Whoid; if(contactID.startsWith('003')){ //Associated to Contact record setContactID.add(contactID); } } } LIST<Contact> recordsToUpdate = [Select Id, AccountID, Account.Last_Call_Date__c, Account.Last_Visit_Date__c From Contact Where Id IN : setContactID]; for(Contact c : recordsToUpdate){ c.Account.description = 'Hello World'; } update recordsToUpdate; }

 

 

If I change the value in the last for loop  from "c.Account.description" to "c.description" it will update the Contact record's description field with "hello world", however I cannot get it to update the account field.

 

Any suggestions?

aalbertaalbert

Yeah, that is correct (and my error). You need to directly set and update Accounts - not through the relationship via the Contact.

 

 

trigger taskTest on Task (after update, after insert) { SET<ID> setContactID = new SET<ID>(); string contactID = ''; for(Task t : Trigger.New){ if(t.whoid!=null){ contactID = t.Whoid; if(contactID.startsWith('003')){ //Associated to Contact record setContactID.add(contactID); } } } LIST<Contact> recordsToUpdate = [Select Id, AccountID, Account.Id, Account.description From Contact Where Id IN : setContactID]; List<Account> accountsToUpdate = new List<Account>{}; for(Contact c : recordsToUpdate){ Account a = new Account(id=c.Account.Id,description=c.Account.description); //c.Account.description = 'Hello World'; accountsToUpdate.add(a); } update accountsToUpdate; //update recordsToUpdate; }

 

 

 

DoubleADoubleA

Thanks Albert,

 

That worked.  Now i'm understanding how the data structures are working a BIT better.  I'm still in the midst of understanding Apex Collections (lists, sets, maps)  so its all new to me!!

 

 

Now i'll be working on the actually trigger i have to create, which is not complex but will require some changes!! 

Much thanks!!