I have written a code which will give my dynamic soql and using this soql my execution will work. But i am getting this erro

unexpected token: IN41

Below is my code Code

public class DateLastUpdateUtil{

    public static void findObjectName (List<String> ListrecordIdPrefix){

        String objectName = '';
        List<String> Matchingrecord = new List<String>();
        List<Id> ListOfIds = new List<Id>(); 

            for(String recordId : ListrecordIdPrefix){
                //Get prefix from record ID
                //This assumes that you have passed at least 3 characters
                String myIdPrefix = String.valueOf(recordId).substring(0,3);
                Map<String,Schema.sObjectType> MapSObjectType = Schema.getGlobalDescribe();

                for(Schema.sObjectType stype : MapSObjectType.values()){
                    Schema.DescribeSObjectResult type = stype.getDescribe();
                    String prefix = type.getKeyPrefix();

                    if(prefix != null && prefix.equals(myIdPrefix)){
                        objectName = type.getName();


        }Catch(Exception e){
            system.debug('Exception due to==========>'+e.getMessage()+e.getLineNumber());


    public static void dateDefine (String objectName,List<Id> ListrecordIdPrefix){
        // String queryStr='SELECT,id,LastModifiedDate FROM'+objectName+'WHERE ID IN:'+ListrecordIdPrefix;
        List<sObject> records = Database.query('Select Id,LastModifiedDate From ' + objectName +'WHERE id IN : ListrecordIdPrefix'); //getting error on this line
        for(SObject record: records){
            DateTime objDT = DateTime.valueOf(record.get('LastModifiedDate'));
            DateTime currentTime = dateTime.now();
            Long dt1 = objDT.getTime();
            Long dt2 = currentTime.getTime();

            Long milliseconds  = dt2-dt1;

            Long Seconds = milliseconds/1000;
            Long Minutes = seconds/60;
            Long Hours = Minutes/60;
            Long Days = Hours/24;


            if(Seconds <= 60){
                system.debug('Last updated a few seconds ago');
            }else if(Minutes <= 60){
                system.debug('Last updated a few minutes ago');
            }else if(Hours >= 24){
                seconds = Math.Mod(seconds,60);
                minutes = Math.Mod(minutes,60);
                hours = Math.Mod(minutes,60);
                system.debug('Last updated a Yesterday '+hours+':'+Minutes+':'+Seconds);
            }else if(Days <= 6){
                seconds = Math.Mod(seconds,60);
                minutes = Math.Mod(minutes,60);
                hours = Math.Mod(minutes,60);
                Days = Math.mod(Hours,24);
                system.debug('Last Update on a '+days+' '+hours+':'+Minutes+':'+Seconds);
                system.debug('Last Updated on =====>'+objDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''));


Thanks for your help in advance !!!

  • Can you debug the dynamic query and check whether its valid or not.
    – Reshma
    May 18, 2017 at 6:32
  • @Reshma I check this query and it is valid May 18, 2017 at 6:39
  • can you check by giving space before where?
    – Safiya PV
    May 18, 2017 at 6:42
  • Yeah give space before where ... List<sObject> records = Database.query('Select Id,LastModifiedDate From ' + objectName +' WHERE id IN : ListrecordIdPrefix');
    – bharath
    May 18, 2017 at 6:45

2 Answers 2


Your posted error goes with the commented line for the query so it does not match your posted code

In your current code you are Missing a space in your query

 objectName +'WHERE


 objectName +' WHERE

Before the where

In the commented query that matches the error given in the question you found that using the property that way was wrong. But From the naming and the error your the value of the property concerns me as


May be a list of the first 3 characters of the id. You need to be using a list of full ids.

Since you have not shown how you constructed that list it is hard to say what you need to do to fix it.

  • As far as I can see, the prefix id list is pointless - why have an object and a prefix in the query (not that you can even use the prefix) May 18, 2017 at 6:53
  • @CasparHarmer not sure I follow. I said in the answer that a list of full ids needed to be used. I would assume they want to filter by id. Answer was for problem posted. I did not review all the code
    – Eric
    May 18, 2017 at 6:55
  • Right - it depends on what he thinks he's using them for... But yes, you are right, of course you need full ids - I'm just not sure he totally understands what he's trying to do with them. May 18, 2017 at 6:58


They are a 1:1 mapping - Opportunities all have the same prefix, so do Accounts and so on. Drop the prefix.

You have no gap between the objectName and the WHERE

To be more specific change this:

objectName +'WHERE id IN :ListrecordIdPrefix

To this:

objectName +' WHERE id IN :ListrecordIdPrefix
  • I am using List only for getting the data. See at line number 40 May 18, 2017 at 6:38
  • Edited again. Change your query in exactly this way and it should work. May 18, 2017 at 6:43
  • Oops I see it's a list. So it's the where part. Amended my answer. May 18, 2017 at 6:45

You must log in to answer this question.

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