1

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>(); 
        try{

            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();
                        ListOfIds.add(Id.valueOf(recordId));
                        system.debug('objectName======>'+objectName);
                        DateLastUpdateUtil.dateDefine(objectName,ListOfIds);
                        break;
                    }
                }

            } 

        }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;

            system.debug('seconds====>'+Seconds);
            system.debug('Minutes====>'+Minutes);
            system.debug('Hours======>'+Hours);
            system.debug('Days======>'+days);

            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);
            }else{
                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 '17 at 6:32
  • @Reshma I check this query and it is valid – Shantanu Mahajan May 18 '17 at 6:39
  • can you check by giving space before where? – Safiya PV May 18 '17 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 '17 at 6:45
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

To

 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

Listrecordidprefix

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) – Caspar Harmer May 18 '17 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 '17 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. – Caspar Harmer May 18 '17 at 6:58
0

** EDIT YOU DO NOT NEED THE RECORD ID PREFIX IF YOU HAVE THE OBJECT **

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 – Shantanu Mahajan May 18 '17 at 6:38
  • Edited again. Change your query in exactly this way and it should work. – Caspar Harmer May 18 '17 at 6:43
  • Oops I see it's a list. So it's the where part. Amended my answer. – Caspar Harmer May 18 '17 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.