+ Start a Discussion
JoshTonksJoshTonks 

How do I use WhatId on Events to only look for events

Im trying to do a count in apex on Events that were marked as cancelled for opportunities that were generated from referrals I know how to build the counts but don't know how or if its possible to combine the two. This is what ive got so far. But im not sure if you can use the WhatId for what im needing.
 
Date THIS_MONTH = System.today().toStartOfMonth();
Date NEXT_MONTH = THIS_MONTH.addMonths(1).addDays(0);
String IsRef = '%'+ 'Referral' +'%';

    public Integer getRefOpps(){
    return [SELECT Count()
            FROM Opportunities
            WHERE Opp_Enquiry_Source__c LIKE IsRef AND CloseDate => THIS_MONTH && CloseDate < NEXT_MONTH AND Stage !=: 'Closed Won' OR Stage !=: 'Completed' 
            ];
     }

    public Integer getCanxRefTask(){
    return [SELECT Count()
            FROM Event
            WHERE AND X1st_Appointment_With_Director__c = TRUE AND ActivityDate >= THIS_MONTH AND ActivityDate < NEXT_MONTH Outcome__c =: 'Cancelled by Them'
            ];
     }

Thank you in advance.
 
Best Answer chosen by JoshTonks
MandaryaMandarya
Have one single query on Event.
Build your query on top of below one :--

select Count() from event where X1st_Appointment_With_Director__c = TRUE AND what.Opp_Enquiry_Source__c LIKE IsRef

All Answers

MandaryaMandarya
Have one single query on Event.
Build your query on top of below one :--

select Count() from event where X1st_Appointment_With_Director__c = TRUE AND what.Opp_Enquiry_Source__c LIKE IsRef
This was selected as the best answer
JoshTonksJoshTonks
Thank you so much Mandarya. I have tried this and it isnt working.

Im getting the following message

No such column 'Opp_Enquiry_Source__c' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. Is this because of how the What field works on events. That field defiantly exists ive used it previously but its not working