3

I'm trying to populate a field whith several others. I know nothing about coding so ... After few research I did that:

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

The line 1, 2 and 3 works well. It populaite my custom lookup field from the WhoId lookup field (Yes it's possible to create custom lookup fiel in event now. but you have to ask to enable it)

My problem come with the line 4. Nom_formate__c is a formula field that take first and last name through the "lookup_contact__c". Because the WhoID lookup is populated after the triger, this part stay empty exept if I update the event.

So, I need to know if it have a way to do the triger after the formula is populated. (I have an error ir I write " after insert"

Thx a lot for your help

3
  • You might get the right values if you use update Trigger.new.clone() in an after insert, then check the formula value in before update trigger. YMMV.
    – sfdcfox
    Commented Apr 12, 2015 at 5:06
  • you can use a workflow rule + field update to update the subject based on those 2 fields. you don't need a trigger for this. Commented Apr 12, 2015 at 12:11
  • Hi. Yes, I know it's easy to populate the field with the workflow. I choosed to use a triger because it didnt work well.
    – David Moi
    Commented Apr 12, 2015 at 12:41

1 Answer 1

1

Wow didn't know you can add a custom lookup on Event - this is new. I think you should pull the Contact Info by code and not with the Formula (Unless it is and update Trigger - and this is non-issue since formula already been calculated). To do this: Create a new Contact Instance and query for the attached contact in your custom field.

/*Make sure you perform the query outside of the loop so you won't hit "Too many SOQL Limit" */

Contact c = [Select Id,FirstName,LastName From Contact Where Id =: lookup_contact__c];

Then just use those fields in your code:

u.subject = c.FirstName +" "+c.LastName;
2
  • Hi. Thx for your help. I just realised that my problem is not because formula are calculated after. Actually, they are calculated before. The problem come from the WhoId lookup field that is populated after the triger. That make that my formula has nothing to calculate and give an empty field. The solution that come in my mind is that, instead of starting the triger when a new event is created, is it possible to start it when the WhoId is populated in all event ?
    – David Moi
    Commented Apr 12, 2015 at 13:28
  • LOL Ok I found a way ;) I just created a stupid workflow to update a field. After reading Triggers and Order of Execution, I saw that the triger is started again (before update) after execution of workflow field update ... :)
    – David Moi
    Commented Apr 12, 2015 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .