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
Mdex Centre-VilleMdex Centre-Ville 

Update field after insert

Hi.
I did a very simple Trigger to update 2 custom field
First is a custom lookup field that lead to contact: lookup_contact__c
Second is the subject field that is supposed to be populated from 2 custom field


trigger Updatelookup on Event (before insert, before update){
    for (event u : Trigger.new){
        u.lookup_contact__c = u.WhoId;
        u.Subject = u.Nom_formate__c + u.Raison_visite_formate__c;
        }
 }

The trigger by himself is working well exept that I'll need to run it after insert because WhoID is populated after the trigger run so .. this informations is always missing exept if i make a manual update.

the problem is if I say after insert, I have an error message because I con only use before insert.
Can you help me with that ?   (Process builder is not working at all for that because it also run .. befaore whoID is populated :( )
Leslie  KismartoniLeslie Kismartoni
I think you might check if you can alter your Shared Activities - if enabled, the WhoId isn't populated in the before case of the trigger.
https://help.salesforce.com/HTViewHelpDoc?id=activities_shared_enabling.htm

Searching around also found this: https://developer.salesforce.com/forums/ForumsMain?id=906F000000095HTIAY
I guess someone had a future call kick off on an after insert which handled the update?
Mdex Centre-VilleMdex Centre-Ville
Hi

"Allow Users to Relate Multiple Contacts to Tasks and Events" is enable yes so, You say that I should ask the support tean to desable it ?


Yes, the popic explain very well my problem.

the answer seems to be:
-------------------------
Hello Ben,
The workaround I implemented was to have the trigger check to see if WhoId and WhatId were both NULL.  If so, the trigger would pass the task Ids to an @future method that would then check to see if WhoId or WhatId was populated.
Let me know if I was not clear in the explanation.
Thanks,
Eric
--------------------

But  Ihave no ideas about how to do it :(


thx for your help (Now, I know that even if I have rule after insert, I'll have the same problem)
Leslie  KismartoniLeslie Kismartoni
This is jsut pseudo code... Maybe a starting point?

Trigger:
trigger updateContactField on Event (before insert, before update){
	List<Id> futureEventUpdateIdList = new List<Id>();
    for (event u : Trigger.new){
		u.Subject = u.Nom_formate__c + u.Raison_visite_formate__c;

        // if it is null, the future call will call the trigger again.
        if(u.WhoId == null) {
	        futureEventUpdateIdList.add(u.id);            
        } else { // else the field is available and we can call it again.
	        u.lookup_contact__c = u.WhoId;
	    }
    }
    
    // Call the future method
	if (futureEventUpdateIdList.size() > 0)
	    futureUpdate.updateContactField(futureEventUpdateIdList);
}

Class that has the future call:
 
public class futureUpdate {

    @future
    public static void updateContactField(List<Id> eventIdList) {
		List<Event> eventList = [SELECT Id, lookup_contact__c, WhoId FROM Event WHERE ID in :eventIdList];

        for (Event eve : eventList) {
            eve.lookup_contact__c = eve.WhoId;
        }
    	upsert eventList;
    }
    
}

if this helps, consider marking it the valid answer?
Mdex Centre-VilleMdex Centre-Ville
Thx :) Ill test that as soon as I can :) Envoyé depuis mon appareil Android.
Leslie  KismartoniLeslie Kismartoni
Did it work?