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
RajashriRajashri 

Unexpected Token Error while iterating through for loop

Hi,

 

Below is my code and i am trying to using the for loop to iterarte the element but i am getting an error unexpected token projectDetail.id how can i modify the below code in for loop

 

 

public with sharing class ProjectDetailrecords {
   
    public  Project_Detail__c  projectDetailrecords {get; set;}
   
  
    public ProjectDetailrecords(ApexPages.StandardController pController) {
       projectDetail =(Project_Detail__c)pController.getRecord();
       String projectDetailId = projectDetail.Id;
       projectDetail = [SELECT Id, Name,CoreCompleted__c,Completed__c   FROM Project_Detail__c WHERE ID = :projectDetailId];
    }
   
      
    public PageReference ProjectApprovals() {
                
           for(Project_Detail__c item: [SELECT Id, Name,Record_Type__c,CoreCompleted__c,Completed__c   FROM Project_Detail__c  WHERE ID = projectDetail.Id]){
                  
         if((item.Completed__c != 100) && (item.CoreCompleted__c != 100) ){
         String msg='Must be completed ';
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,msg);
            ApexPages.addMessage(myMsg);
            return null;
        } }
       

Chamil MadusankaChamil Madusanka

Use

 

for(Project_Detail__c item: [SELECT Id, Name,Record_Type__c,CoreCompleted__c,Completed__c   FROM Project_Detail__c  WHERE ID =: projectDetail.Id]){

 instead of

 

for(Project_Detail__c item: [SELECT Id, Name,Record_Type__c,CoreCompleted__c,Completed__c   FROM Project_Detail__c  WHERE ID = projectDetail.Id]){

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

RajashriRajashri

Thanks!But my requirement is iterating over the loop and my Completed__c and coreCompleted__c values are less than 100 so it should display an error message but it is not displaying.i simply changed the code as below but it is giving me an error that loop must itereate over Project_Detail__c  object.

 

How can i modify the below code? so it will iterate over the object?

 

public with sharing class ProjectDetailrecords {
   
    public  Project_Detail__c  projectDetailrecords {get; set;}
   
  
    public ProjectDetailrecords(ApexPages.StandardController pController) {
       projectDetail =(Project_Detail__c)pController.getRecord();
       String projectDetailId = projectDetail.Id;
       projectDetail = [SELECT Id, Name,CoreCompleted__c,Completed__c   FROM Project_Detail__c WHERE ID = :projectDetailId];
    }
   
          public PageReference ProjectApprovals() {
                
           for(Project_Detail__c item:  projectDetail)                  
         if((item.Completed__c != 100) && (item.CoreCompleted__c != 100) ){
         String msg='Must be completed ';
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,msg);
            ApexPages.addMessage(myMsg);
            return null;
        } }
       

Laxman RaoLaxman Rao

Since it is a single instance of object, you does not require to iterate over the loop.

 

Try this :

 

public PageReference ProjectApprovals() {
if((projectDetail.Completed__c != 100) && (projectDetail.CoreCompleted__c != 100) ){
String msg='Must be completed ';
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,msg);
ApexPages.addMessage(myMsg);
return null;
}
}

RajashriRajashri

Thanks but This is also Not working.

 

my datatype is formula percent for both the fields.

 

even though the value is less than 100% it is not giving an error.

Laxman RaoLaxman Rao

1. Can you ensure that both the conditions are true?

i.e projectDetail.Completed__c != 100 AND projectDetail.CoreCompleted__c

If both the conditions are true, then only a message is shown.

 

2. Did u add <apex:pageMessages /> in the visualforce page?

 

3. Put a debug log and check what are the values coming for  projectDetail.Completed__c and projectDetail.CoreCompleted__c