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
Edwards71Edwards71 

Selecting Task records associated with a particular custom object

Hi,

 

I'm trying to extract a list of Task Records that are related to a custom object. I'm using the getkeyprefix to identify the 3 digits so I can use this with WhatId.

 

I'm getting a ' compile error unexpected token GDIPrefix' on the code below. Any ideas why?

 

Thanks in advance!

 

Public Task [] getNotListing() {  
   String GDIPrefix =  GDI_Action__c.sObjectType.getDescribe().getKeyPrefix();
   
   system.debug(GDIPrefix);
   
       return [Select ActivityDate, subject,Description, Contract_reference__c, OwnerID, whoID, whatID
       FROM Task where AccountId = :System.currentPageReference().getParameters().get('id') 
              and ActivityDate < NEXT_YEAR and WhatID Like GDIPrefix order by ActivityDate ASC];
        
        }  

 

Best Answer chosen by Admin (Salesforce Developers) 
Edwards71Edwards71

Thanks - you're right. You can't use Like or most other operators with an ID field.

 

I got this code to work.  Thanks for your help.

 

 Public Task [] getNotListing() {  
   list<Account> a1 = [Select Name FROM Account where Id = :System.currentPageReference().getParameters().get('id') ];      
       string VCacct1 = a1.get(0).name;
   
   list<GDI_Action__c> actions = [select id FROM GDI_action__c where GNS_Partner__c = :VCacct1 ];
       
   
       return [Select ActivityDate, subject,Description, Contract_reference__c, OwnerID, whoID, whatID 
       FROM Task WHERE 
       ActivityDate < NEXT_YEAR and
       IsClosed=false 
       and WhatID IN :actions
       ORDER BY ActivityDate ASC];
        
        }  
   

 

All Answers

sfdcfoxsfdcfox

Your code here looks correct. I suspect you have a missing closing curly bracket ( "}" ) further up in your code. This would cause the next function after the function missing the curly bracket to throw this sort of red-herring error.

 

What Chris said. I can't believe I missed that.

chris_centrachris_centra

When you reference a class variable from a soql query (inside [brackets]), you have to prefix it with a colon. 

Edwards71Edwards71

Thanks. I've put the colon in and now get a 'compile error: invalid operator on id field'.

 

The code works fine if I delete the ..and WhatId like :GDIPrefix .. bit of the code

Public Task [] getNotListing() {  
   String GDIPrefix =  GDI_Action__c.sObjectType.getDescribe().getKeyPrefix();
   
   system.debug(GDIPrefix);
   
       return [Select ActivityDate, subject,Description, Contract_reference__c, OwnerID, whoID, whatID
       FROM Task where AccountId = :System.currentPageReference().getParameters().get('id') 
              and ActivityDate < NEXT_YEAR and WhatID like :GDIPrefix order by ActivityDate ASC];
        
        }  

 

chris_centrachris_centra

Hmmm, right, probably can't use LIKE on an ID field...not sure how to get around that one in SOQL...maybe create a custom field (text) that contains the WhatId value (set by workflow?  possibly a formula?)...then you can use LIKE on that field...there may be a better way.

Edwards71Edwards71

Thanks - you're right. You can't use Like or most other operators with an ID field.

 

I got this code to work.  Thanks for your help.

 

 Public Task [] getNotListing() {  
   list<Account> a1 = [Select Name FROM Account where Id = :System.currentPageReference().getParameters().get('id') ];      
       string VCacct1 = a1.get(0).name;
   
   list<GDI_Action__c> actions = [select id FROM GDI_action__c where GNS_Partner__c = :VCacct1 ];
       
   
       return [Select ActivityDate, subject,Description, Contract_reference__c, OwnerID, whoID, whatID 
       FROM Task WHERE 
       ActivityDate < NEXT_YEAR and
       IsClosed=false 
       and WhatID IN :actions
       ORDER BY ActivityDate ASC];
        
        }  
   

 

This was selected as the best answer