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
NakataNakata 

What's wrong with this simple SOQL ? - How to get event/task related to Lead object ?

Good day, 

 

Any expert please point out what's wrong with my query below and get error "invalid query filter operator"

 

SELECT Id FROM Event WHERE WhoId like 'ABC%'

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

I am just trying to give you an structure of your trigger ,  you can try it

 

 

trigger CountTotalEventsOnLead on Event (after insert) {

    String leadKey = '00Q';
    Integer eventCount = 0;
    Id leadId;
    for(Event e : trigger.new)
        {
            String strWhoId = String.valueOf(e.whoId).substring(0 , 3);
            if(strWhoId == leadKey)
                {
                    leadId = e.whoId;
                }
        }
    
    if(leadId != null)
        {    
            List<Lead> listLead = [Select id , (SELECT Id FROM Events ) from Lead where id =: leadId];
            if(listLead.size() > 0)
                eventCount = listLead.get(0).Events.size();
                
            //update lead field with new count 
            listLead.get(0).APINameOfEvencountField__C = eventCount;
            update listLead.get(0);          
   
        }
}

 

 

I have written this trigger for only single event insert, you should write it for bulk data also using collections set,map, list if you need to. Please ask me if you have any problem.

 

All Answers

Shashikant SharmaShashikant Sharma

Let me explain you how do a SOQL works in Apex Compiler

 

Lets take your example 

 

SELECT Id FROM Event WHERE WhoId like '00Q%'

 

When you write this and compile , first Compiler checks sytax from left to right

 

1) Syntax of Query SELECT Id FROM Event that is correct in your case till

2)systax of where is also correct till    :   WHERE WhoId 

3) in where condition then compiler checks whther the operator on the field condition is allowed or not, in your case you are using like with field id and that is not allowed thats why you see this compiler error "invalid operator on id field"

 

even if you remove like and use = operator then you will see "invalid ID field: 00Q%" because compiler will find incompatible value to compare for id value

 

I hope it is clear to you.

NakataNakata

Thanks Shashikant Sharma !

 

I also doubt on why "whoId" can't be query here ? how can i refer whether the event is coming from Lead object or not , what is the best way to check that ?

Shashikant SharmaShashikant Sharma

You can use this query 

List<Event> l = [SELECT Id FROM Event WHERE WhoId in (Select id from Lead)];

 

NakataNakata

Thanks  and here my challenge :-

 

Assumption :-  this is after insert trigger from Event object

 

1. After event records inserted, i need to find out the Lead object id that linking it

2. After get the lead object id, i need to know how many records in Event object currently link to that Lead object

3. Find out the total of event records that linked to related Lead object

4. update to Lead object's field with the total number

 

 Any chance to use single query to accomplish the above ?

Shashikant SharmaShashikant Sharma

I am just trying to give you an structure of your trigger ,  you can try it

 

 

trigger CountTotalEventsOnLead on Event (after insert) {

    String leadKey = '00Q';
    Integer eventCount = 0;
    Id leadId;
    for(Event e : trigger.new)
        {
            String strWhoId = String.valueOf(e.whoId).substring(0 , 3);
            if(strWhoId == leadKey)
                {
                    leadId = e.whoId;
                }
        }
    
    if(leadId != null)
        {    
            List<Lead> listLead = [Select id , (SELECT Id FROM Events ) from Lead where id =: leadId];
            if(listLead.size() > 0)
                eventCount = listLead.get(0).Events.size();
                
            //update lead field with new count 
            listLead.get(0).APINameOfEvencountField__C = eventCount;
            update listLead.get(0);          
   
        }
}

 

 

I have written this trigger for only single event insert, you should write it for bulk data also using collections set,map, list if you need to. Please ask me if you have any problem.

 

This was selected as the best answer
NakataNakata

Awesome  !

 

Thanks a lot for your warm input !