You need to sign in to do that
Don't have an account?
Shruthi GM 4
I have class and test class.
Class:-
public with sharing class MyLoginController{
private ApexPages.StandardController controller;
public Login__c login{get;set;}
public id loginId{get;set;}
//constructor
public MyLoginController(ApexPages.StandardController controller) {
this.controller=controller;
this.login= (Login__c)Controller.getRecord();
System.debug('@@@@@'+login.Id);
}
//Login method when user clicks on login button
public ApexPages.PageReference LoginMethod() {
system.debug('&&&&&&&&'+login);
loginId = [SELECT Id FROM Login__c
WHERE Name =: login.Name].Id;
string password = [SELECT password__c FROM Login__c
WHERE Name =: login.Name].password__c;
system.debug('!!!!!!!!'+login.Name);
system.debug('@@@'+password);
if(login.password__c==password) {
system.debug('%%%%%%');
PageReference redirectPage = new PageReference ('/apex/Nomination_msg');
//redirectPage.setRedirect(true);
//redirectPage.getParameters().put('id',controller.getId());
return redirectPage;
}
else{
login.Name.addError('Incorrect username or password.Please Enter correct value.');
return null;
}
}
//save method when user clicks on save button on nomination msg page
public pagereference save(){
login__c l= [select name,password__c,First_Name__c,Last_Name__c,age__c,Email__c,Country_of_user__c from login__c where id =: loginId ];
l.First_Name__c=login.First_Name__c;
l.Last_Name__c=login.Last_Name__c;
l.age__c=login.age__c;
l.Email__c=login.Email__c;
l.Country_of_user__c=login.Country_of_user__c;
update l;
try{
system.debug('Entered into the Method');
PageReference redirectPage = new PageReference ('/apex/successful_msg');
return redirectPage;
return null;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
//signup method when the user clicks on signup button on login page
public pagereference signup() {
Pagereference pg=new Pagereference('https://ap2.salesforce.com/066280000027Mwq');
try{
system.debug('@@@###$$$$%%%%');
PageReference redirectPage = Page.SignupPage;
redirectPage.setRedirect(true);
return redirectPage;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
}
return null;
}
//submit method when the user clicks on submit button on signup page
public pagereference Submit(){
login__c lp=new login__c();
lp.Name=login.Name;
lp.password__c=login.password__c;
lp.RePassword__c =login.RePassword__c ;
try{
PageReference redirectPage = new PageReference ('/apex/Nomination_msg_a');
system.debug('Entered into the Method');
if(login.Password__c == login.Repassword__c) {
Insert lp;
return redirectPage;
}
else {
login.Repassword__c.adderror('Please enter correct password');
redirectPage.setRedirect(false);
return null;
}
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
//savesignup method when user clicks on save option on Nomination_msg_a page
public pageReference savesignup(){
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name].Id;
login__c log=[select name,password__c,First_Name__c,Last_Name__c,Age__c,Email__c,Country_of_user__c, id from login__c where id =: loginid];
log.First_Name__c=login.First_Name__c;
log.Last_Name__c=login.Last_Name__c;
log.Age__c=login.Age__c;
log.Email__c=login.Email__c;
log.Country_of_user__c=login.Country_of_user__c;
update log;
system.debug('&&&&&&&'+log);
try{
system.debug('Entered into the Method');
PageReference redirectPage = new PageReference ('/apex/successful_msg');
return redirectPage;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
}
Test class:-
@isTest(SeeAllData=false)
public class MyLoginControllerTest{
static List<login__c > Lstlogs = new List<login__c >();
static testMethod void testMyLoginController(){
test.startTest();
login__c login=new login__c();
login.Name='Testing';
login.Password__c='sss';
login.RePassword__c='sss';
login.First_Name__c ='Shruthi GM';
login.Last_Name__c ='GM';
login.age__c =12;
login.Email__c ='shruthi.gm@gmail.com';
login.Status__c='Registration Complete';
login.Country_of_user__c ='India';
insert login;
ApexPages.StandardController sc = new ApexPages.StandardController(login);
MyLoginController mec = new MyLoginController(sc);
ApexPages.currentPage().getParameters().put('id',login.id);
mec.LoginMethod();
mec.signup();
mec.submit();
mec.save();
mec.savesignup();
Test.stopTest();
}
}
Test class is covering 60% with System.QueryException: List has more than 1 row for assignment to SObject error msg near the line "mec.savesignup();".How do I need to rectify this error?Please help.
public with sharing class MyLoginController{
private ApexPages.StandardController controller;
public Login__c login{get;set;}
public id loginId{get;set;}
//constructor
public MyLoginController(ApexPages.StandardController controller) {
this.controller=controller;
this.login= (Login__c)Controller.getRecord();
System.debug('@@@@@'+login.Id);
}
//Login method when user clicks on login button
public ApexPages.PageReference LoginMethod() {
system.debug('&&&&&&&&'+login);
loginId = [SELECT Id FROM Login__c
WHERE Name =: login.Name].Id;
string password = [SELECT password__c FROM Login__c
WHERE Name =: login.Name].password__c;
system.debug('!!!!!!!!'+login.Name);
system.debug('@@@'+password);
if(login.password__c==password) {
system.debug('%%%%%%');
PageReference redirectPage = new PageReference ('/apex/Nomination_msg');
//redirectPage.setRedirect(true);
//redirectPage.getParameters().put('id',controller.getId());
return redirectPage;
}
else{
login.Name.addError('Incorrect username or password.Please Enter correct value.');
return null;
}
}
//save method when user clicks on save button on nomination msg page
public pagereference save(){
login__c l= [select name,password__c,First_Name__c,Last_Name__c,age__c,Email__c,Country_of_user__c from login__c where id =: loginId ];
l.First_Name__c=login.First_Name__c;
l.Last_Name__c=login.Last_Name__c;
l.age__c=login.age__c;
l.Email__c=login.Email__c;
l.Country_of_user__c=login.Country_of_user__c;
update l;
try{
system.debug('Entered into the Method');
PageReference redirectPage = new PageReference ('/apex/successful_msg');
return redirectPage;
return null;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
//signup method when the user clicks on signup button on login page
public pagereference signup() {
Pagereference pg=new Pagereference('https://ap2.salesforce.com/066280000027Mwq');
try{
system.debug('@@@###$$$$%%%%');
PageReference redirectPage = Page.SignupPage;
redirectPage.setRedirect(true);
return redirectPage;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
}
return null;
}
//submit method when the user clicks on submit button on signup page
public pagereference Submit(){
login__c lp=new login__c();
lp.Name=login.Name;
lp.password__c=login.password__c;
lp.RePassword__c =login.RePassword__c ;
try{
PageReference redirectPage = new PageReference ('/apex/Nomination_msg_a');
system.debug('Entered into the Method');
if(login.Password__c == login.Repassword__c) {
Insert lp;
return redirectPage;
}
else {
login.Repassword__c.adderror('Please enter correct password');
redirectPage.setRedirect(false);
return null;
}
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
//savesignup method when user clicks on save option on Nomination_msg_a page
public pageReference savesignup(){
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name].Id;
login__c log=[select name,password__c,First_Name__c,Last_Name__c,Age__c,Email__c,Country_of_user__c, id from login__c where id =: loginid];
log.First_Name__c=login.First_Name__c;
log.Last_Name__c=login.Last_Name__c;
log.Age__c=login.Age__c;
log.Email__c=login.Email__c;
log.Country_of_user__c=login.Country_of_user__c;
update log;
system.debug('&&&&&&&'+log);
try{
system.debug('Entered into the Method');
PageReference redirectPage = new PageReference ('/apex/successful_msg');
return redirectPage;
}
Catch(Exception dmle){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
//ApexPages.addMessages(dmle);
//ApexPages.addMessage(myMsg);
return null;
}
}
}
Test class:-
@isTest(SeeAllData=false)
public class MyLoginControllerTest{
static List<login__c > Lstlogs = new List<login__c >();
static testMethod void testMyLoginController(){
test.startTest();
login__c login=new login__c();
login.Name='Testing';
login.Password__c='sss';
login.RePassword__c='sss';
login.First_Name__c ='Shruthi GM';
login.Last_Name__c ='GM';
login.age__c =12;
login.Email__c ='shruthi.gm@gmail.com';
login.Status__c='Registration Complete';
login.Country_of_user__c ='India';
insert login;
ApexPages.StandardController sc = new ApexPages.StandardController(login);
MyLoginController mec = new MyLoginController(sc);
ApexPages.currentPage().getParameters().put('id',login.id);
mec.LoginMethod();
mec.signup();
mec.submit();
mec.save();
mec.savesignup();
Test.stopTest();
}
}
Test class is covering 60% with System.QueryException: List has more than 1 row for assignment to SObject error msg near the line "mec.savesignup();".How do I need to rectify this error?Please help.
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name].Id;
This line might be returning more than one query rows.
Either be more specific or update is like :
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name LIMIT 1 ].Id;
Also, updating the test class will only ensure that your coverage is increased but in case you face the scenario in your org, you will face the error again. Therefore, it is suggested that you make changes in the main class only.
Hope this helps.
Regards
Medhya Mahajan
All Answers
//savesignup method;
In this method
Line 3:
login__c log=[select name,password__c,First_Name__c,Last_Name__c,Age__c,Email__c,Country_of_user__c, id from login__c where id =: loginid];
Here you are assigning the result of the query to an object and when the query does not return any result, there is nothing to assign to the object.
Try doing this instead:
Store the value of query in a list:
List <login__c> loginList = new List <login__c> ();
loginList = [select name,password__c,First_Name__c,Last_Name__c,Age__c,Email__c,Country_of_user__c, id from login__c where id =: loginid];
Then check the size of the list and add then assign first element of the list to the object:
if (!loginList .IsEmpty()){
login__c log = loginList [0];
Mark as solved if this helps.
Regards
Medhya Mahajan
@Medhya:I implemented your changes too...still facing the same error!
Then try below code. Let us know if this will help you else share the same error which you are getting with screen shot and line number
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name].Id;
This line might be returning more than one query rows.
Either be more specific or update is like :
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name LIMIT 1 ].Id;
Also, updating the test class will only ensure that your coverage is increased but in case you face the scenario in your org, you will face the error again. Therefore, it is suggested that you make changes in the main class only.
Hope this helps.
Regards
Medhya Mahajan