+ Start a Discussion
mac adminmac admin 

Update method with in the same controller

Hi all,
I want to write the update method with in the same controller. For four vfpage i have written a single controller in the save method i have written a redirect method to second page with saved record ID. Now I want to update the second page values and redirect to third page.

Can anyone help me over here.

Thanks inadvance.

Regards,
mac.
LBKLBK
You can do this by passing an additional parameter in your URL.

For example, calling URL for Page1 would be CURRENT_URL+ '&Page=Page1'

When you set the PageReference in APEX class, read this parameter and set thew further redirect accordingly.

Here is the code sample for that.
 
queryString = ApexPages.currentPage().getParameters().get('Page');
System.debug('queryString ' + queryString );

if (queryString == 'Page1'){

//Setup redirect to Page 2
PageReference MyPage = new PageReference('/apex/Page2?id=' + (String)id + '&Page=Page2');
            MyPage.setRedirect(true);
            return MyPage;
}
else if (queryString == 'Page2'){


//Setup redirect to Page 3
PageReference MyPage = new PageReference('/apex/Page3?id=' + (String)id + '&Page=Page3');
            MyPage.setRedirect(true);
            return MyPage;
}
and so on....

Let me know if this helps.
 
mac adminmac admin
HI LBK,
Thanks for the reply, all the code has to be in the save method right..?
LBKLBK
That is right. All of these lines of code go into Save method.

And, if you are calling first VF page from a custom button or link, check out if you can add Page=Page1 as part of the query string there.

If it doesn't work out, your IF and ELSE IF conditions should check from Page2 onwards. And, the Page1 will be checked as part of the last ELSE condition (because you don't have a query string to check).

Hope this helps.
mac adminmac admin

Hi LBK,
I have written  a redirection method in the save method to redirect to second page after saving the first page. Below is my redirection method:
 public PageReference page2() {
this.ugId = ApexPages.currentPage().getParameters().get('id');
  Pagereference ref = new Pagereference('/apex/Page2?id=' +this.ugId);

              ref.setRedirect(true);

               return ref;

}

The page is redirecting to second page, here I want to update the page 2 data in the db of same record  and redirect to 3rd page. Can you please help me over here.

Thanks in advance.

Regrads,
mac.

LBKLBK
Hi Mac,

Are you not using a single Save method for all the pages?

If you are using different save method for different pages, within the same APEX class, you don't even need the querystring to identify the page.

Can you please post your APEX class and VF pages (may be Page 1 and Page 2 will suffice), so that I can help you better?
mac adminmac admin
Hi LBK,
Below is my controller
public class Ugcontroller {
   
  public String StringDate {get; set;}
  public String StringDateDOW {get; set;}
  public String BirthdateDOW {get; set;}

    public String getBirthdateDOW() {
        return null;
    }

 public String selectedyear{get;set;}
 public Date dat{get;set;}
 public ID ugId{get;set;}
 

  public Under__c ugform{
    get {
      if (ugform== null)
       ugform = new Under__c();
       
      return ugform;
    }
    set;
  }
    
 
  
  
    public PageReference Underpage4() {
      Pagereference fourthpage = new Pagereference('/apex/page4');
       return fourthpage;
    }
    
    
    public PageReference Undergpage3() {
      Pagereference thirdpage = new Pagereference('/apex/page3');
       return thirdpage;
    }
    
    
    public PageReference Undergpage2() {
      Pagereference secondpage = new Pagereference('/apex/Page2');
       return secondpage;
    }


    public PageReference Undergpage1() {  
       
     Pagereference firstpage = new Pagereference('/apex/Page1');
       return firstpage;
    }



    
     public Ugcontroller () {
     
  
  }
  
  
    public class Sampledate {
            
            public Sampledate() {
        
            }
       }
       
 
public List<SelectOption> getyearOptions() {
        List<SelectOption> countryOptions = new List<SelectOption>();
        countryOptions.add(new SelectOption('','-Select-'));
        
        for(integer i=2017;i<=2022;i++)
        {
        string s = string.valueof(i);
        countryOptions.add(new SelectOption(s,s));
        }
        
        return countryOptions;
    } 
    
    public PageReference cancel() {
      Pagereference ref = new Pagereference('http://mca.edu/admissions/apply-now/');

              ref.setRedirect(true);
            
         // if successfully inserted new lform, then displays the thank you page.
           return ref;
        
    }
  Public Boolean Agreement{get;set;}  
    


    public PageReference save() {
    
     If(ugform.Approve__c!=true)
    {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please check the check box.');
    ApexPages.addMessage(myMsg);
    return null;
     }
      
 
    system.debug(this.ugform);
    system.debug('-----entered in to save method----');
           try {
            system.debug('-----entered in to try block----');
      ugform.Year_Enrollment__c = String.valueOf(selectedyear);
      //ugform.Date_of_Birth_ug__c = Date.valueof(dat);
      system.debug('---ugform is ---'+ugform);
      upsert ugform;
      system.debug('----ugfrom id is ----'+ugform.id);
     
    } catch (DMLException e) {
     system.debug('-----entered in to catch block----');
     system.debug('---- error is --'+e);
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new Under.'));
      ApexPages.addMessages(e);
      return null;
    }
     Pagereference ref = new Pagereference('/apex/page?id=' +ugform.id); 
               ref.setRedirect(true);
                      return ref;
  
        
    }
    
               public PageReference getpdfpage() {
  this.ugId = ApexPages.currentPage().getParameters().get('id');
  Pagereference ref = new Pagereference('/apex/PDF?id=' +this.ugId);

              ref.setRedirect(true);
            
         // if successfully inserted new lform, then displays the thank you page.
           return ref;      
    }
     
 
  
  // find the day of the week for Birthdate.  Date.valueOf can be used with Date fields
  public PageReference FindBirthdateDOW() {
    try {
      BirthdateDOW = DateTime.newInstance( Date.valueOf(ugform.Date_of_Birth_ug__c), Time.newInstance(0, 0, 0, 0) ).format('EEEE');
    } catch (exception e) {
      BirthdateDOW = '';
    } 
    return null;
  }
  
}

 
LBKLBK
Hi Mac,

Your Save method will look like this.
 
public PageReference save() {
    
	String queryString = ApexPages.currentPage().getParameters().get('Page');
	String sPageReference = '';
	
	//If the Page query string is not set or set to Page1
	if (queryString == 'Page1' || queryString == null){
		
		//LBK: I assume that this block is part of Page 1 only. If it is common across, move it above the IF statement
		If(ugform.Approve__c!=true)
		{
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please check the check box.');
			ApexPages.addMessage(myMsg);
			return null;
		}
		
		//Your form elements in Page 1 goes here.
		ugform.Year_Enrollment__c = String.valueOf(selectedyear);
        //ugform.Date_of_Birth_ug__c = Date.valueof(dat);
 
		//Setup your sPageReference here.
		sPageReference = '/apex/Page2?id=' + (String)ugform.id + '&Page=Page2';
	}
	else if (queryString == 'Page2'){
				
		//Your form elements in Page 2 goes here.
		//ugform.Year_Enrollment__c = String.valueOf(selectedyear);
        //ugform.Date_of_Birth_ug__c = Date.valueof(dat);
 
		//Setup your sPageReference here.
		sPageReference = '/apex/Page3?id=' + (String)ugform.id + '&Page=Page3';
	}
	else if (queryString == 'Page3'){
				
		//Your form elements in Page 3 goes here.
		//ugform.Year_Enrollment__c = String.valueOf(selectedyear);
        //ugform.Date_of_Birth_ug__c = Date.valueof(dat);
 
		//Setup your sPageReference here.
		sPageReference = '/apex/Page4?id=' + (String)ugform.id + '&Page=Page4';
	}
	else if (queryString == 'Page4'){
				
		//Your form elements in Page 4 goes here.
		//ugform.Year_Enrollment__c = String.valueOf(selectedyear);
        //ugform.Date_of_Birth_ug__c = Date.valueof(dat);
 
		//Setup your sPageReference here.
		sPageReference = 'SET THIS TO FINAL LANDING PAGE';
	}	

	try {
		system.debug('-----entered in to try block----');
		system.debug('---ugform is ---'+ugform);
		upsert ugform;
		system.debug('----ugfrom id is ----'+ugform.id);

	} catch (DMLException e) {
		system.debug('-----entered in to catch block----');
		system.debug('---- error is --'+e);
		ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new Under.'));
		ApexPages.addMessages(e);
		return null;
	}
	Pagereference ref = new Pagereference(sPageReference); 
	ref.setRedirect(true);
	return ref;

        
}
Look at the commented lines in the code and add the right elements from your VF page to save the data.
 
mac adminmac admin
Hi LBK,
I have change my code with your code. After saving the first page it's not redirecting to second page it's redirecting to congrats page. Can you help me where I have done mistake.
LBKLBK
Can you post your updated Save method again?

How do you access your Page1?

Can you provide me the URL of the Page1?

You can copy it from the address bar, when you land in the page (you can remove the content till force.com/).
mac adminmac admin
Hi LBK,
My first page is the home page for the Force.com Sites. 
https://developer-XXXXX.XXXXXX.force.com/GraduateApplication/
is my first page URL. After filling the data in this (page1) when we click on Next button then it redirects to page2 but in the URL it will appears as below
https://developer-XXXXXXXX.XXXXX.force.com/GraduateApplication/page1
LBKLBK
So, GraduateApplication is your first page.

Or Page1 is your first page?

Your Next button in your Page1, should have the PageReference like this (if it has to go to Page2).

https://developer-XXXXXXXX.XXXXX.force.com/GraduateApplication/page2?Page=Page2&id=YOUR_UGFORM_ID_HERE

If you find it difficult to interpret this, post your Save method and names (just names) of all the Visualforce pages in Chronological order.