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
sunny@99-chgsunny@99-chg 

page redirection...

hai all,Thanks in advance..

 

I have a small problem,but i could nt understand how to do it.please help me.

I want to redirect to google page only when the record is inserted.here in this code always it is redirecting after clicking on save.if the error message will come means it wont redirect...

 

public class loginfortestController {
public LoginDetailsforTest__c opp{get;set;}
public loginfortestController() {
opp=new LoginDetailsforTest__c();
}

public pagereference save()
{

for(list<logindetailsfortest__C> lt:[select email__C from logindetailsfortest__C where email__c=:opp.email__c limit 1])
{
system.debug(lt);
if(opp.email__c==null||!lt.isempty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'enter another email id');
ApexPages.addMessage(myMsg);
PageReference pageRef = ApexPages.currentPage();
}else{
LoginDetailsforTest__c ldt=new LoginDetailsforTest__c(user_name__c=opp.user_name__c,email__c=opp.email__c);
insert ldt;
}
}
pagereference ref=new pagereference('https://www.google.com');
return ref;
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
rohitsfdcrohitsfdc

CHange your code to this

public class loginfortestController {
public LoginDetailsforTest__c opp{get;set;}
public loginfortestController() {
opp=new LoginDetailsforTest__c();
}

public pagereference save()
{

for(list<logindetailsfortest__C> lt:[select email__C from logindetailsfortest__C where email__c=:opp.email__c limit 1])
{
system.debug(lt);
if(opp.email__c==null||!lt.isempty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'enter another email id');
ApexPages.addMessage(myMsg);
PageReference pageRef = ApexPages.currentPage();
return pageRef;
}else{
LoginDetailsforTest__c ldt=new LoginDetailsforTest__c(user_name__c=opp.user_name_​_c,email__c=opp.email__c);
insert ldt;
}
}
pagereference ref=new pagereference('https://www.google.com');
return ref;
}

}

 Hope this will work now

All Answers

rohitsfdcrohitsfdc

CHange your code to this

public class loginfortestController {
public LoginDetailsforTest__c opp{get;set;}
public loginfortestController() {
opp=new LoginDetailsforTest__c();
}

public pagereference save()
{

for(list<logindetailsfortest__C> lt:[select email__C from logindetailsfortest__C where email__c=:opp.email__c limit 1])
{
system.debug(lt);
if(opp.email__c==null||!lt.isempty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'enter another email id');
ApexPages.addMessage(myMsg);
PageReference pageRef = ApexPages.currentPage();
return pageRef;
}else{
LoginDetailsforTest__c ldt=new LoginDetailsforTest__c(user_name__c=opp.user_name_​_c,email__c=opp.email__c);
insert ldt;
}
}
pagereference ref=new pagereference('https://www.google.com');
return ref;
}

}

 Hope this will work now

This was selected as the best answer
Alex.AcostaAlex.Acosta

Simpliest way without getting into much complexity is having your page ref be inside your else statement with the google redirect. This should handle any validation rules as well and display it on the page incase the insert fails. This has not been tested but I'm pretty sure it should work just fine. Just my personal disclamer.

 

public pagereference save()
	{
		pagereference ref = null;
		for(list<logindetailsfortest__C> lt:[select email__C from logindetailsfortest__C where email__c=:opp.email__c limit 1]){
			system.debug(lt);
			if(opp.email__c==null||!lt.isempty())
			{
				ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'enter another email id');
				ApexPages.addMessage(myMsg);
				PageReference pageRef = ApexPages.currentPage();
			}else{
				LoginDetailsforTest__c ldt=new LoginDetailsforTest__c(user_name__c=opp.user_name__c,email__c=opp.email__c);
				try{
					insert ldt;
					ref = new pagereference('https://www.google.com');
					ref.setRedirect(true);
				}
				catch(DmlException e){
					ApexPages.addMessages(e);
				}
			}
		}
		return ref;
	}

 

Suresh RaghuramSuresh Raghuram

first thing  you need write setredirect = true

 

second thing is any thing will go through salesforce server so pan your url to meet that condition.

pagereference ref=new pagereference('https://www.google.com');

ref.setredirect(true);
return ref;

 

if this answers your question, Make this as solution

sunny@99-chgsunny@99-chg

Thanks Rohit.

sunny@99-chgsunny@99-chg

Thanks Alex.