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
MrHammyMrHammy 

missing something in my if statement

still learning and getting better but why is the if statment  not working in this controller? ( know that data is the same, just need it to work at all . i get Error: MultiRow Compile Error: expecting right curly bracket, found 'if' at line 16 column 2

 

public class MultiRow {
    public List<multiRowContact> Contacts { get; set; }
 
         String chainname = ApexPages.currentPage().getParameters().get('name') ;
         String prog = ApexPages.currentPage().getParameters().get('pg') ;
      
      


    public MultiRow() {
        LoadData();        
    }
     


  if(prog == 'WM Synergy'){
  string   filter = '0128000000020mOAAQ'; //'WM Synergy
  } else {
  string   filter = '0128000000020mOAAQ'; //'WM Synergy
  } 
 
    public PageReference Save() {
        for (multiRowContact MRC : Contacts) {
            MRC.Save();
        }
        LoadData();
        return null;
    }
    private void LoadData() {
    

  
        Contacts = new List<multiRowContact>();
        for (List<erp_data__c> cs : [SELECT name, Account__c ,Acct_RT_Name__c, Chain__c,Id, Program__c, RecordTypeId ,updatechain__c , updatetowhat__c 
               FROM ERP_Data__c  
               WHERE Chain__c = :chainname and RecordTypeId = :filter limit 125]) {
               
                for (erp_data__c c : cs) {
                    multiRowContact MRC = new multiRowContact();
                    MRC.ID = c.ID;
                    MRC.Checked = true;
                    MRC.Name = c.Name;
                    MRC.Program = c.Program__c;
                    MRC.account = c.Account__c ;
                    MRC.updatetowhat = c.updatetowhat__c;
                    Contacts.add(MRC);
            }
        }
    }
 
    private class multiRowContact {
    String prog = ApexPages.currentPage().getParameters().get('pg') ;
    String idx = ApexPages.currentPage().getParameters().get('idx') ;
        public String ID { get; set; }
        public String Name { get; set; }
        public String Account { get; set; }
        public String updatetowhat{ get; set; }
        public String Program { get; set; }
        public Boolean Checked { get; set; }
        public void Save() {
            if (Checked) {
                System.debug('Saving...ID: ' + ID);
                erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                c.updatetowhat__c = prog;
                update c;
                
                Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                try {
                   insert newpbl;
                } catch (DmlException e) {
                // Process exception here
                }
            }
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Now you're shadowing the class scoped variable "filter" with a local scoped variable of the same name in the constructor. Do not re-declare the data type, because the goal is to assign the value to the class scoped variable do that it will be available in the other functions. The rest of the code appears fine at a glance.

All Answers

sfdcfoxsfdcfox

Your IF statement isn't in a function, so it will not compile. Move the declaration to the class level, and the initialization to the constructor. In general, you should always be initializing within the constructor, not in the declaration area. Here's a basic restructuring that will work:

 

public class MultiRow {
    public List<multiRowContact> Contacts { get; set; }
 
         String chainname, prog, filter;
 
	public MultiRow() {
		chainname = ApexPages.currentPage().getParameters().get('name') ;
		prog = ApexPages.currentPage().getParameters().get('pg') ;
		 if(prog == 'WM Synergy'){
			filter = '0128000000020mOAAQ'; //'WM Synergy
		} else {
		filter = '0128000000020mOAAQ'; //'WM Synergy
		} 
        LoadData();        
    }
     

    public PageReference Save() {
        for (multiRowContact MRC : Contacts) {
            MRC.Save();
        }
        LoadData();
        return null;
    }
	
    private void LoadData() {
        Contacts = new List<multiRowContact>();
        for (List<erp_data__c> cs : [SELECT name, Account__c ,Acct_RT_Name__c, Chain__c,Id, Program__c, RecordTypeId ,updatechain__c , updatetowhat__c 
               FROM ERP_Data__c  
               WHERE Chain__c = :chainname and RecordTypeId = :filter limit 125]) {
               
                for (erp_data__c c : cs) {
                    multiRowContact MRC = new multiRowContact();
                    MRC.ID = c.ID;
                    MRC.Checked = true;
                    MRC.Name = c.Name;
                    MRC.Program = c.Program__c;
                    MRC.account = c.Account__c ;
                    MRC.updatetowhat = c.updatetowhat__c;
                    Contacts.add(MRC);
            }
        }
    }
 
    private class multiRowContact {
		String prod, idx;
    
		multiRowContact() {
			prog = ApexPages.currentPage().getParameters().get('pg') ;
			idx = ApexPages.currentPage().getParameters().get('idx') ;
		}
        public String ID { get; set; }
        public String Name { get; set; }
        public String Account { get; set; }
        public String updatetowhat{ get; set; }
        public String Program { get; set; }
        public Boolean Checked { get; set; }
        public void Save() {
            if (Checked) {
                System.debug('Saving...ID: ' + ID);
                erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                c.updatetowhat__c = prog;
                update c;
                
                Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                try {
                   insert newpbl;
                } catch (DmlException e) {
                // Process exception here
                }
            }
        }
    }
}

 

MrHammyMrHammy

now the variable is just empty, i have updated to this filling out some of the other else if items but  its just returning nothing. if i declare the filter variable it works fin but no luck with the if 

 

public class MultiRow {
        public List<multiRowContact> Contacts { get; set; }

      String chainname, prog, filter;

     
//string filter = '0128000000020mOAAQ'; //WM Synergy

 
    public MultiRow() {
        String chainname = ApexPages.currentPage().getParameters().get('name') ;
        String prog = ApexPages.currentPage().getParameters().get('pg') ;
        if(prog == 'WM Synergy'){
        string filter = '0128000000020mOAAQ'; //WM Synergy
        } else if (prog == 'PS Cornerstone') {
        string filter = '0128000000020mTAAQ'; //PS Cornerstone
          } else if (prog == 'CB Ascend') {
        string filter = '0128000000020mVAAQ'; //CB Ascend
        } else {
        string filter = '0128000000020mOAAQ'; //WM Synergy as default
        }
        LoadData();        
    }
     

 
 
    public PageReference Save() {
        for (multiRowContact MRC : Contacts) {
            MRC.Save();
        }
        LoadData();
        return null;
    }
    private void LoadData() {
    

  
        Contacts = new List<multiRowContact>();
        for (List<erp_data__c> cs : [SELECT name, Account__c ,Acct_RT_Name__c, Chain__c,Id, Program__c, RecordTypeId ,updatechain__c , updatetowhat__c 
               FROM ERP_Data__c  
               WHERE Chain__c = :chainname and RecordTypeId = :filter limit 125]) {
               
                for (erp_data__c c : cs) {
                    multiRowContact MRC = new multiRowContact();
                    MRC.ID = c.ID;
                    MRC.Checked = true;
                    MRC.Name = c.Name;
                    MRC.Program = c.Program__c;
                    MRC.account = c.Account__c ;
                    MRC.updatetowhat = c.updatetowhat__c;
                    Contacts.add(MRC);
            }
        }
    }
 
    private class multiRowContact {
    String prog = ApexPages.currentPage().getParameters().get('pg') ;
    String idx = ApexPages.currentPage().getParameters().get('idx') ;
        public String ID { get; set; }
        public String Name { get; set; }
        public String Account { get; set; }
        public String updatetowhat{ get; set; }
        public String Program { get; set; }
        public Boolean Checked { get; set; }
        public void Save() {
            if (Checked) {
                System.debug('Saving...ID: ' + ID);
                erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                c.updatetowhat__c = prog;
                update c;
                
                Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                try {
                   insert newpbl;
                } catch (DmlException e) {
                // Process exception here
                }
            }
        }
    }
}

 

sfdcfoxsfdcfox
Now you're shadowing the class scoped variable "filter" with a local scoped variable of the same name in the constructor. Do not re-declare the data type, because the goal is to assign the value to the class scoped variable do that it will be available in the other functions. The rest of the code appears fine at a glance.
This was selected as the best answer