+ Start a Discussion
satyamsatyam 

Please help to resolve unexpected token: '.' Error1

Hi,

 

Please help to resolve following error:

 

unexpected token: '.'

 

Error is coming for below code can anyone please suggest to resolve the following:

 

  List<Monthly_Plant_Hour__c> mph= Database.query('SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,'
                                            +'Reporting_Year__c FROM Monthly_Plant_Hour__c '
                                                +'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c ');
                                                
        for(Integer i=0; i<mph.size();i++){


            finalmph.add(mph[i]);
        }
                                                
        return finalmph;
    }

 

 

Thanks in advance:))))

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

The dyamic query by you is not correct and hence this exception.First construct a static query and then form the dynamic query carefully.Its just a compile time error of query string not being proper .

 

Let me know if you need more explanation

satyamsatyam

Thaks for your reply Mohit.

 

PFA below is my code Can you please do some changes which need to so that i can change it accordingly to work it properly.

 

In debug log its showing the exception in Red coloured line.Please help.

 

public class planthourExtension {
private final Get_Hour_Report__cplant;
public String year;
public String previousYear;
public Integer intYear = 0;
public Integer intPreviousYear = 0;
public String reporttype;
public String reportperiod;
public String reportCategory;
public String facility;
public String country;
public String plantDivision;
List<Decimal> cumulativemanhr=new List<Decimal>();
List<Decimal> cummanhr1 = new List<Decimal>();
List<Decimal> avgemployee = new List<Decimal>();
Decimal **bleep** = 0;
decimal hr = 0;
List<Monthly_home_Hour__c> incidentrec = new list<Monthly_home_Hour__c>(); 
public string firstWhereClause = '';
public string secondWhereClause = '';
public string secondWhereClause1 = '';
public List<string> mon = new List<String>();

public planthourExtension(ApexPages.StandardController controller) {

this.plant = (Plant_Hours_Report__c)controller.getRecord();
Get_Hour_Report__co = [Select Country__c,Division_div__c,Facility__c, Reporting_Category__c,
Report_Year__c,Report_Type__c,Reporting_Period__c
From Get_Hour_Report__cWhere Id =: plant.Id];

year= o.Report_Year__c;
if(year!= null){
intYear = Integer.valueof(year);
intPreviousYear = intYear - 1;
previousYear = String.valueof(intPreviousYear);
}
reporttype=o.Report_Type__c;
reportperiod=o.Reporting_Period__c;
facility = o.Facility__c;
reportCategory = o.Reporting_Category__c;
plantDivision = o.Division_div__c;
country = o.Country__c;
system.debug('reporttype is '+reporttype);

if(reporttype == 'Facility'){
firstWhereClause = 'Facility__c = \''+facility+'\' and Reporting_Category__c =\''+reportCategory+'\'';
}else if(reporttype == 'Sub'){
firstWhereClause = ' Country__c = \''+country+'\' and Reporting_Category__c =\''+reportCategory+'\'';
}else if(reporttype == 'Division'){
firstWhereClause = ' Country__c = \''+country+'\' and Division__c = \''+plantDivision+'\'';
}else {
firstWhereClause = ' Country__c = \''+country+'\'';
}

Date currentDate = date.today();

Integer currentMonth = currentDate.month();
system.debug('Current Month is'+currentMonth );

String[] monthQuery = new String[]{};
String[] monthQuery1 = new String[]{};

if(reportperiod == 'Calendar'){
system.debug('Entered Calendar');
for(Integer i=1;i<=currentMonth;i++ ){
monthQuery.add('\''+String.valueof(i)+'\'');
}
system.debug('monthQuery is '+monthQuery);
secondWhereClause = ' and Reporting_Year__c =\''+year+'\' and Reporting_Month__c in '+monthQuery+'';

}else if(reportperiod == 'Fiscal'){
system.debug('Entered Fiscal');
if(currentMonth >= 10){
for(Integer i=10;i<=currentMonth;i++ ){
monthQuery.add('\''+String.valueof(i)+'\'');
}
}else{
for(Integer i=1;i<=currentMonth;i++ ){
monthQuery.add('\''+String.valueof(i)+'\'');
}
for(Integer i=10;i<=12;i++ ){
monthQuery1.add('\''+String.valueof(i)+'\'');
}
}
secondWhereClause = ' and Reporting_Year__c =\''+year+'\' and Reporting_Month__c in '+monthQuery+'';
secondWhereClause1 = ' and Reporting_Year__c =\''+previousYear+'\' and Reporting_Month__c in '+monthQuery1+'';

}else if(reportperiod == 'Rolling'){
system.debug('Entered Rolling');
for(Integer i=1;i<currentMonth;i++ ){
monthQuery.add('\''+String.valueof(i)+'\'');
}
for(Integer i=currentMonth;i<=12;i++ ){
monthQuery1.add('\''+String.valueof(i)+'\'');
}

secondWhereClause = ' and Reporting_Year__c =\''+year+'\' and Reporting_Month__c in '+monthQuery+' ';
secondWhereClause1 = ' and Reporting_Year__c =\''+previousYear+'\' and Reporting_Month__c in '+monthQuery1+'';
}




public List<Monthly_home_Hour__c> getManhour() {

List<Monthly_home_Hour__c> finalmph = new List<Monthly_home_Hour__c>();


if(!secondWhereClause1.equals('')){

List<Monthly_home_Hour__c> mph1= Database.query('SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause+ ' ' + secondWhereClause1 + ' order by Reporting_Month__c ');

for(Integer i=0; i<mph1.size();i++){
finalmph.add(mph1[i]);


List<Monthly_home_Hour__c> mph= Database.query('SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c ');

for(Integer i=0; i<mph.size();i++){
finalmph.add(mph[i]);
}

return finalmph;
}

public List<Decimal> getCumulativeManhour() {

Decimal cummanhr = 0;
Decimal cummanhrcum = 0;


if(!secondWhereClause1.equals('')){

List<Monthly_home_Hour__c> mph1= Database.query('SELECT Name,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause+ ' ' + secondWhereClause1 + ' order by Reporting_Month__c ');

if(mph1.size()>0){
for(Integer i=0;i<mph1.size();i++)
{
cummanhr =mph1[i].Man_hours__c;
cummanhrcum = cummanhrcum + cummanhr;
cummanhr1.add(cummanhrcum);
}
}

List<Monthly_home_Hour__c> mph= Database.query('SELECT Name,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c ');

if(mph.size()>0){
for(Integer i=0;i<mph.size();i++)
{
cummanhr =mph[i].Man_hours__c;
cummanhrcum = cummanhrcum + cummanhr;
cummanhr1.add(cummanhrcum);
}

return cummanhr1;
}
public List<Decimal> getAverageEmployee() {
Integer i= 0;
Integer j= 0;
Decimal avgemp = 0;
Decimal avgemployeeset = 0;

if(!secondWhereClause1.equals('')){

List<Monthly_home_Hour__c> mph1= Database.query('SELECT Name,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause+ ' ' + secondWhereClause1 + ' order by Reporting_Month__c ');

if(mph1.size()>0){

avgemployeeset = 0;
for(i=0;i<mph1.size();i++)
{
avgemp =mph1[i].Employee__c;

avgemployeeset = ((avgemployeeset*i) + avgemp)/(i+1);

avgemployee.add(avgemployeeset);
j = i+1;
}
}

List<Monthly_home_Hour__c> mph= Database.query('SELECT Name,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c ');

if(mph.size()>0){
for(Integer m=0;m<mph.size();m++)
{
avgemp =mph[m].Employee__c;

avgemployeeset = ((avgemployeeset*j) + avgemp)/(j+1);

avgemployee.add(avgemployeeset);
j++;
}
}

return avgemployee;
}


public List<Monthly_home_Hour__c> getCumulativeRolling() {

List<Monthly_home_Hour__c> finalmph = new List<Monthly_home_Hour__c>();

if(!secondWhereClause1.equals('')){
List<Monthly_home_Hour__c> mph1= Database.query('SELECT Name,Month__c,Reporting_Month__c,Incidents__c,Total_Recordable_Rate__c,'
+' Lost_Work_Incidents__c,Lost_Work_Rate__c,Days_Away_From_work__c,'
+' Days_Away_From_Work_Rate__c,Medical_Treatment__c, '
+' Medical_Treatment_Rate__c,Fatalities__c,'
+' Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause+ ' ' + secondWhereClause1 + ' order by Reporting_Month__c ');

for(Integer i=0; i<mph1.size();i++){
finalmph.add(mph1[i]);
}

}
List<Monthly_home_Hour__c> mph= Database.query('SELECT Name,Month__c,Reporting_Month__c,Incidents__c,Total_Recordable_Rate__c,'
+' Lost_Work_Incidents__c,Lost_Work_Rate__c,Days_Away_From_work__c,'
+' Days_Away_From_Work_Rate__c,Medical_Treatment__c, '
+' Medical_Treatment_Rate__c,Fatalities__c,'
+' Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause+ ' ' + secondWhereClause + ' order by Reporting_Month__c ');

for(Integer i=0; i<mph.size();i++){
finalmph.add(mph[i]);
}
return finalmph;
}
}

 

Thanks in advance:)))

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Debug the query in the red you have marked using system.debug .I doubt on your first where clause and secondwhereclause variables .

satyamsatyam

I tried, but querry is returning nothing.

 

some times its showing List Index Out Of Bound Error.Where can be problem

 

??

 

 

Thanks :)))

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Ok Is list out of bound index is coming when query returning null?

Peter.GPeter.G

The first think you should do is print out the soql statement you are trying to execute.  You could replace

List<Monthly_home_Hour__c> mph= Database.query('SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c
');

 

With:

String query = ('SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,'
+'Reporting_Year__c FROM Monthly_home_Hour__c '
+'where '+ firstWhereClause + ' ' +secondWhereClause + ' order by Reporting_Month__c ';

system.debug('Generated query: ' + query);

List<Monthly_home_Hour__c> mph= Database.query(query);

 

 

That will let you see the actual query you are generating and allow you to find the syntax error. 

 

 

satyamsatyam

Thanks for your reply Peter.

 

Now i able to catch the error PFA below debug log where the error is occured.

 

12:34:55.194 (194661000)|SYSTEM_METHOD_ENTRY|[68]|System.debug(ANY)
12:34:55.194 (194668000)|USER_DEBUG|[68]|DEBUG|monthQuery is ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ...)
12:34:55.194 (194675000)|SYSTEM_METHOD_EXIT|[68]|System.debug(ANY)
12:34:55.194 (194700000)|SYSTEM_METHOD_ENTRY|[69]|String.valueOf(Object)
12:34:55.194 (194722000)|SYSTEM_METHOD_EXIT|[69]|String.valueOf(Object)
12:34:55.194 (194758000)|CODE_UNIT_FINISHED|planthourExtension <init>
12:34:55.197 (197599000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000001IOtQ|planthourExtension get(Manhour)
12:34:55.197 (197614000)|SYSTEM_MODE_ENTER|true
12:34:55.197 (197632000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000001IOtQ|planthourExtension invoke(getManhour)
12:34:55.197 (197728000)|SYSTEM_CONSTRUCTOR_ENTRY|[106]|<init>()
12:34:55.197 (197751000)|SYSTEM_CONSTRUCTOR_EXIT|[106]|<init>()
12:34:55.197 (197807000)|SYSTEM_METHOD_ENTRY|[123]|System.debug(ANY)
12:34:55.197 (197824000)|USER_DEBUG|[123]|DEBUG|Generated query: SELECT Name,Month__c,Reporting_Month__c,Employee__c,Man_hours__c,Reporting_Year__c FROM Monthly_home_Hour__c where Facility__c = 'a4pE00000008OStIAM' and Reporting_Category__c ='Nexeo Solutions-NA Southeast Region-Supply Chain' and Reporting_Year__c ='2012' and Reporting_Month__c in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ...) order by Reporting_Month__c
12:34:55.197 (197836000)|SYSTEM_METHOD_EXIT|[123]|System.debug(ANY)
12:34:55.197 (197850000)|SYSTEM_METHOD_ENTRY|[124]|Database.query(String)
12:34:55.198 (198220000)|EXCEPTION_THROWN|[124]|System.QueryException: unexpected token: '.'
12:34:55.198 (198330000)|SYSTEM_METHOD_EXIT|[124]|Database.query(String)
12:34:55.198 (198426000)|FATAL_ERROR|System.QueryException: unexpected token: '.'

Class.planthourExtension.getManhour: line 124, column 1
12:34:55.198 (198436000)|CODE_UNIT_FINISHED|planthourExtension invoke(getManhour)
12:34:55.198 (198502000)|CODE_UNIT_FINISHED|planthourExtension get(Manhour)

 

The error is because of the dot at the end thats creating problem so please suggest how i can solve this issue  tried but not able to solve this.

 

Thanks:)))))