+ Start a Discussion
viktorVviktorV 

Activity - referenced opportunity's details

I'm creating a trigger in which I'd like to check some Opportunity details. I tried

 

for(Event e: trigger.new){
  if(e.What.CloseDate == ...){
    
  }
}

 but it gives Invalid field CloseDate for SObject Name error.

Is there a way to get these details? I don't want to use SOQL,

 

Thank you,

Viktor

 

Best Answer chosen by Admin (Salesforce Developers) 
HaagenHaagen

Hi Victor,

 

sorry I missread your first post. This error you see is probably because the What-field is a polimorfic key, it can point to different kind of sObjects. You need to cast it to the correct type for it to work. You can check the type by looking at  e.What.Type .

 

Hope that brings you closer to the solution.

 

Cheers!

 

Martin

 

 

All Answers

LakshmanLakshman

I assume that you have created trigger on Opportunity object.

 

You can add something like below in your trigger.

 

for (Opportunity op: trigger.new)
  if(op.CloseDate == ...){

HaagenHaagen

Hi Victor,

 

have you tried removing "What" from your if-statement?

 

if (e.CloseDate == ...)

 

Cheers!

 

Martin

viktorVviktorV

Nope. I've created an Event trigger. It is possible to get details on sObjects of the referenced object.

For example, if I create an Opportunity, I have the ability to work with the given Accounts all details. (e.g. using opp.Account.BillingAddress)

 

The reference name on Activities for Related To field is 'What' but I don't know if its waiting more parameters (beacuse related to field can include not just account, than some other objects)

LakshmanLakshman

Try to  use WhatId instead of what.

viktorVviktorV

The foreign key relationship is What, WhatId only gives the referenced id. In spite of that, I tried WhatId and doesn't work.

HaagenHaagen

Hi Victor,

 

sorry I missread your first post. This error you see is probably because the What-field is a polimorfic key, it can point to different kind of sObjects. You need to cast it to the correct type for it to work. You can check the type by looking at  e.What.Type .

 

Hope that brings you closer to the solution.

 

Cheers!

 

Martin

 

 

This was selected as the best answer
viktorVviktorV

Hi Martin, thank you for the answer!

 

Then how could I get the referenced Opportunity field value? Only with SOQL?

 

Thank you,

Viktor

HaagenHaagen

Hi Viktor,

 

the documentation probably states it somewhere, but I usually just try it since I never can remember how much data is available int the trigger. If the references is all null values, try to query for them with SOQL.

 

Cheers!

 

Martin 

viktorVviktorV

Thanks Martin!