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
Nirmal ChristopherNirmal Christopher 

Help with adding validation in a Apex page

  public pagereference save1(){
      if(vac.Advertise__c == true){
         list<RPaaS_Stage_Library__c> lib = [select id from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id and category__c='Qualifying'];
         if(!(lib.size()>0))
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'There are no qualifying stages on this job. Therefore job cannot be advertised.'));
      
      list<RPaaS_Stage_Library__c> lib1 = [select id,StageName__c  from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id ];
system.debug('++++'+lib1.size()+'$$$$'+lib1);
set<string>libstring=new set<string>();
for(RPaaS_Stage_Library__c s:lib1){
libstring.add(s.StageName__c  );
}
if(libstring.size()<4){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One of the qualifying stages are missing please check all 4 qualifying stages are added for this job.'));
}
}
    
     upsert vac;
      pagereference page = new pagereference('/apex/RPaaSVacancyTab');
         page.setRedirect(true);
     return page;

 I have to make the custom error messages appear in the top of the screen if there is records in the object  RPaaS_Stage_Library__c  related to its master record from the object RpaasVacancy__c.. I have to display the second error if the sring set libstring does not have 4 values in it. If it meets these two criteries we have to upsert a master list and do page redirection to a specific page listed in the code.  I am unable to make the error messages appear in the click of the button . and dml upsert also not done aong with page redirection if the criteria meets. I checked the conditions given for error messages it is working fine if i use that in other componets say Apex action support event in a check box...but i dont know how to do this with a button action please help...please note that this entire operation of error checking have to be done if the check box  is true.

if(vac.Advertise__c == true)

If it is not true then i have to just upsert the record.

Best Answer chosen by Admin (Salesforce Developers) 
Nirmal ChristopherNirmal Christopher

I used method chaning concept to solve this posted below the code for reference 

public PageReference checkAdv(){
      Pagereference p= new pagereference('');
      list<RPaaS_Stage_Library__c> lib = [select id from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id and category__c='Qualifying'];
      list<RPaaS_Stage_Library__c> lib1 = [select id,StageName__c  from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id ];
      set<string>libstring=new set<string>();
for(RPaaS_Stage_Library__c s:lib1){
libstring.add(s.StageName__c  );
}
 system.debug('+++++'+libstring.size()+'+++++'+libstring);    
      if(!(lib.size()>0) && vac.Advertise__c != true){
      p=this.save1();
      }
      else if((lib.size()>0 && vac.Advertise__c == true) || (libstring.size()>=4 && vac.Advertise__c == true)){
      p=this.save1();
      }
 else{
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'There are no qualifying stages  or one of the SQIO values ae missing on the criteria library for this job. Therefore job cannot be advertised.'));
      p=Null;
      }
         if(libstring.size()<4 && vac.Advertise__c == true){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One of the qualifying stages are missing please check all 4 qualifying stages are added for this job.'));
p=Null;
}
else{
 p=this.save1();
}
      return p;
      }
          public pagereference save1(){
     try{
upsert vac;
PageReference pg = new PageReference('/apex/RPaaSVacancyTab') ;  
  pg.setRedirect(true);
          return pg ;  
              }    
    catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        } 
    return null;
    }

I forgot to instanciate the page reference variable and later I passed its null value in error statements.Using the combinations of various if else conditions I am able to acheive this. Thank you folks 

All Answers

TwanTwan

Hi,

Check that you have a apex:message in your origin visualforce page. Then try adding something like this after

if(libstring.size()<4){
  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One of the qualifying stages are missing please check all 4   qualifying stages are added for this job.'));
return null; }

return null will display again your visual force and add message to it

Antoine

Bhanu VallabhuBhanu Vallabhu

ur using if loop to throw error but to show them it has to be page reference do like this
if(error){
add page message;
return null;
}
else{
logig
return actual page ref
}

use else if for multiple conditions or errors

Nirmal ChristopherNirmal Christopher

Can we do this use mehod chaining....i  have created three different methods each for displaying error, upserting record and page redirection respectively like this.method name but still nt working. Can u provide me with the working code. thanks in Advance.

Nirmal ChristopherNirmal Christopher

I used method chaning concept to solve this posted below the code for reference 

public PageReference checkAdv(){
      Pagereference p= new pagereference('');
      list<RPaaS_Stage_Library__c> lib = [select id from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id and category__c='Qualifying'];
      list<RPaaS_Stage_Library__c> lib1 = [select id,StageName__c  from RPaaS_Stage_Library__c where RpaasVacancy__c =:vac.id ];
      set<string>libstring=new set<string>();
for(RPaaS_Stage_Library__c s:lib1){
libstring.add(s.StageName__c  );
}
 system.debug('+++++'+libstring.size()+'+++++'+libstring);    
      if(!(lib.size()>0) && vac.Advertise__c != true){
      p=this.save1();
      }
      else if((lib.size()>0 && vac.Advertise__c == true) || (libstring.size()>=4 && vac.Advertise__c == true)){
      p=this.save1();
      }
 else{
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'There are no qualifying stages  or one of the SQIO values ae missing on the criteria library for this job. Therefore job cannot be advertised.'));
      p=Null;
      }
         if(libstring.size()<4 && vac.Advertise__c == true){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One of the qualifying stages are missing please check all 4 qualifying stages are added for this job.'));
p=Null;
}
else{
 p=this.save1();
}
      return p;
      }
          public pagereference save1(){
     try{
upsert vac;
PageReference pg = new PageReference('/apex/RPaaSVacancyTab') ;  
  pg.setRedirect(true);
          return pg ;  
              }    
    catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        } 
    return null;
    }

I forgot to instanciate the page reference variable and later I passed its null value in error statements.Using the combinations of various if else conditions I am able to acheive this. Thank you folks 

This was selected as the best answer