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
Jonathan Osgood 3Jonathan Osgood 3 

Error: Compile Error: line 43:0 no viable alternative at character ' ' at line 43 column 0

public class MeritusSignupController {


 public Form__c form {get;set;}
 public String formId {get;set;}
 public Account a {get;set;}
 public Contact c {get;set;}
 public String is_new {get;set;}


 public MeritusSignupController()
    {
    
     form= new Form__c();
     form.Applicant_First_Name__c = UserInfo.getFirstName();
     form.Applicant_Last_Name__c = UserInfo.getLastName();
     form.Email__c = UserInfo.getUserEmail();
     a= new account();
     is_new = ApexPages.CurrentPage().getParameters().get('newform');
     formId = ApexPages.CurrentPage().getParameters().get('formid');
     String userId= UserInfo.getUserId();
     c= new contact();
      
     List<Form__c> formList = new List<form__c>();
        if (is_new == 'true')  
        {
            form = new form__c();
            form.Applicant_First_Name__c = UserInfo.getFirstName();
            form.Applicant_Last_Name__c = UserInfo.getLastName();
            form.Email__c = UserInfo.getUserEmail();
        }
    
        else if (formId != null)
        {
            formList = [Select id, Student__r.Name, Student__c, Applicant_First_Name__c,Applicant_Last_Name__c,Email__c from form__c where id =: formid];
        }
        
    }
    
 void savefunc()
     {
     Boolean flag = false;
      if (form.Applicant_First_Name__c == null)  {
           form.Applicant_First_Name__c.addError('Required field.');
           flag = true;
          } 
        }
                             
                                        
     if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        
        upsert form;        
        upsert c;
       }  
}
Best Answer chosen by Jonathan Osgood 3
William TranWilliam Tran
Try this; 

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
public class MeritusSignupController {


 public Form__c form {get;set;}
 public String formId {get;set;}
 public Account a {get;set;}
 public Contact c {get;set;}
 public String is_new {get;set;}


 public MeritusSignupController()
    {
    
     form= new Form__c();
     form.Applicant_First_Name__c = UserInfo.getFirstName();
     form.Applicant_Last_Name__c = UserInfo.getLastName();
     form.Email__c = UserInfo.getUserEmail();
     a= new account();
     is_new = ApexPages.CurrentPage().getParameters().get('newform');
     formId = ApexPages.CurrentPage().getParameters().get('formid');
     String userId= UserInfo.getUserId();
     c= new contact();
      
     List<Form__c> formList = new List<form__c>();
        if (is_new == 'true')  
        {
            form = new form__c();
            form.Applicant_First_Name__c = UserInfo.getFirstName();
            form.Applicant_Last_Name__c = UserInfo.getLastName();
            form.Email__c = UserInfo.getUserEmail();
        }
    
        else if (formId != null)
        {
            formList = [Select id, Student__r.Name, Student__c, Applicant_First_Name__c,Applicant_Last_Name__c,Email__c from form__c where id =: formid];
        }
        
    }
    
 void savefunc()
     {
     Boolean flag = false;
      if (form.Applicant_First_Name__c == null)  {
           form.Applicant_First_Name__c.addError('Required field.');
           flag = true;
          } 
        
                             
                                        
     if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        
        upsert form;        
        upsert c;
       } 
     } 
}

 

All Answers

Anirudh SinghAnirudh Singh
Hi Jonathan,

The error is because the below code is being used outside the scope of the savefunc method:

if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        
        upsert form;        
        upsert c;
       } 

Reason: Boolean flag is defined inside the function and the if loop is outside it.

Please let me know if this helps.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh
Jonathan Osgood 3Jonathan Osgood 3
Thanks Anirudh,

I've tried adding the IF statement inside of the fucntion and the error persists:
 
public class MeritusSignupController {


 public Form__c form {get;set;}
 public String formId {get;set;}
 public Account a {get;set;}
 public Contact c {get;set;}
 public String is_new {get;set;}


 public MeritusSignupController()
    {
    
     form= new Form__c();
     form.Applicant_First_Name__c = UserInfo.getFirstName();
     form.Applicant_Last_Name__c = UserInfo.getLastName();
     form.Email__c = UserInfo.getUserEmail();
     a= new account();
     is_new = ApexPages.CurrentPage().getParameters().get('newform');
     formId = ApexPages.CurrentPage().getParameters().get('formid');
     String userId= UserInfo.getUserId();
     c= new contact();
      
     List<Form__c> formList = new List<form__c>();
        if (is_new == 'true')  
        {
            form = new form__c();
            form.Applicant_First_Name__c = UserInfo.getFirstName();
            form.Applicant_Last_Name__c = UserInfo.getLastName();
            form.Email__c = UserInfo.getUserEmail();
        }
    
        else if (formId != null)
        {
            formList = [Select id, Student__r.Name, Student__c, Applicant_First_Name__c,Applicant_Last_Name__c,Email__c from form__c where id =: formid];
        }
        
    }
  
  void savefunc()
     {
     Boolean flag = false;
     
      if (form.Applicant_First_Name__c == null)  
        {
           form.Applicant_First_Name__c.addError('Required field.');
           flag = true;
          } 

     if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        
        upsert form;        
        upsert c;
       }  


        }
                             
                                        
    

 public PageReference saveForm() {
        
        try
        {
        savefunc();
        }
        catch (Exception e)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, e.getmessage()));
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, String.valueOf(e.getLineNumber())));
        }
}
}

 
William TranWilliam Tran
Try this; 

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
public class MeritusSignupController {


 public Form__c form {get;set;}
 public String formId {get;set;}
 public Account a {get;set;}
 public Contact c {get;set;}
 public String is_new {get;set;}


 public MeritusSignupController()
    {
    
     form= new Form__c();
     form.Applicant_First_Name__c = UserInfo.getFirstName();
     form.Applicant_Last_Name__c = UserInfo.getLastName();
     form.Email__c = UserInfo.getUserEmail();
     a= new account();
     is_new = ApexPages.CurrentPage().getParameters().get('newform');
     formId = ApexPages.CurrentPage().getParameters().get('formid');
     String userId= UserInfo.getUserId();
     c= new contact();
      
     List<Form__c> formList = new List<form__c>();
        if (is_new == 'true')  
        {
            form = new form__c();
            form.Applicant_First_Name__c = UserInfo.getFirstName();
            form.Applicant_Last_Name__c = UserInfo.getLastName();
            form.Email__c = UserInfo.getUserEmail();
        }
    
        else if (formId != null)
        {
            formList = [Select id, Student__r.Name, Student__c, Applicant_First_Name__c,Applicant_Last_Name__c,Email__c from form__c where id =: formid];
        }
        
    }
    
 void savefunc()
     {
     Boolean flag = false;
      if (form.Applicant_First_Name__c == null)  {
           form.Applicant_First_Name__c.addError('Required field.');
           flag = true;
          } 
        
                             
                                        
     if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        
        upsert form;        
        upsert c;
       } 
     } 
}

 
This was selected as the best answer
Jonathan Osgood 3Jonathan Osgood 3
Thank you William, that did it. Much appreciated. Can you tell me what you edited? It's not clear. 
William TranWilliam Tran
In the save function, I moved the bracket down so that everything can compile and run properly.

Thx