• FARSANA PS
  • NEWBIE
  • 175 Points
  • Member since 2017

  • Chatter
    Feed
  • 6
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 33
    Replies
Hi,
I am new to Salesforce . I need to write a test class for custom controller. 
Please help me
public class UpdateGoogleApiController {

    public string endpoint{get; set;}

    //Insert the new custom setting record in the database
    public void saveRecord(){
        CREDE__GoogleAPIEndPoint__c googleKey  = [SELECT Id, name from CREDE__GoogleAPIEndPoint__c][0];
       CREDE__GoogleAPIEndPoint__c gAPI  = new CREDE__GoogleAPIEndPoint__c();
        gAPI.Id = googleKey.Id; 
        gAPI.CREDE__EndpointURL__c = endpoint;
       
        Update gAPI;
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'API key value is updated Successfully.Thank you!'));
        //Redirect to new record detail page
      // return new PageReference('/' + googleKey.Id);
      // return null;
    }
}

 
hey Guys

i need a small help regarding a test classes. Basically i am trying to design a test class for account and contact, in my case Account is Renamed as (Courses) and Contact is Renamed as (Students), and i have 2 custom objects Classes(Class__c) and Attendancess (Attendancess__c)

how do i design a test class for below apex class
public class UpdateUsingCheckboxC {
    
    @AuraEnabled
    public static List <Contact> fetchContact(string key) {
        system.debug('classname'+key);
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        string classname = cls.Courses__r.name;
        system.debug('classname'+classname);
        return [SELECT Id, Name,Account.Name,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c FROM Contact where (Check_for_Attendance__c=false AND Check_for_Absentee__c=false) AND Account.Name =:classname];
    }
    
    @AuraEnabled
    public static void updateRecord(List <String> lstRecordId,string key) {
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        if(cls.Id !=null){
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : lstRecordId]){
                con.Check_for_Attendance__c = true;
                con.Check_for_Absentee__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = true;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
    @AuraEnabled
    public static void updateSecondRecord(List <String> secondRecordId,string key) {
        system.debug('key1'+key);
        system.debug('secondRecordId '+secondRecordId);
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        if(key !=null){
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : secondRecordId]){
                con.Check_for_Absentee__c = true;
                con.Check_for_Attendance__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = false;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
}

 
I'm new to apex coding and have run into a bit of a problem which I'm hoping someone can help with. 

Basically, I've been tasked with a Salesforce deployment for my company a part of which requires a customer portal for viewing cases. Following some early prototyping we decided to use a piece of apex programming to display the case list in a format that is suited to our needs. The list basically consists of a drop down box that has two options, depending on the option chosen the class will pull data from Salesforce. this is being used on a visualforce page that is embedded into the community site.

I've used this code in Sandox with no issues but when I try to deploy to Production it fails due to only 71% code coverage. 
 
public class CommunityCaseList {

public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}



I've read up on it and believe I need to create a test class to run the code but when I'm trying to create one it displays the code as incorrect. This is the test class:
 
@isTest

private class CommunityCaseListTest {
 
    static testMethod void validateCommunityCaseList() {


public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}

}



I'm not quite sure what I'm doing wrong or if there's an easier way to achieve the code coverage required. The tutorials I've read so far are for creating test classes that manipulate data whereas this is simply used to get data.

If anyone can help point me int he right direction it would be greatly appreciated. 

Many thanks

Andy
 i had created a custom object of emplye data  with feilds  like   name , experience, address, salary ,and created a record and got the recordid ='allxxwe22522' .. i need to create a class with all the details of that id and display it    :  
 note:  with out using the pageblock and pageblock table should have to display it. how is it possible








 }
 }
  • August 10, 2018
  • Like
  • 0
Hi,

I a string and it has 'AND' sting multiple times how to find a number of times sub-string occurs in a string.
He is my code
String str=  AccountName AND AccountPhone;
integer ct=?
Hi,
I have 3 objects:
Opportunity
Pro__c
Unc__c 
(Pro__c and Unc__c has lookup to opportunities)

To get unc__c values from Opportunities i am using this query,
select name from opportunity where unc__r.id='idvalue'


can I query from Unc__c to get opportunities??
If so, How?
  • August 08, 2018
  • Like
  • 0
AND (OR(NOT($Profile.Name = 'Integration User'),NOT( $User.Username ='xyz')) , Owner:Queue.DeveloperName ='abc')
is the validation rule condition .Its working when only profile check is added but not when both profile and user check is done in the condition.Will logged in user based check work in validation rules??
I have two object 
1. Account
2. BCC (related object to acount)
Account has external id field "BCC customer code" and BCC object has field "BCC code" and "status".
Requirement : When BCC code status is active then only that BCC code should show in "BCC customer code" on Account detail page.
how to write trigger for this.
 
Hi,
I am new to Salesforce . I need to write a test class for custom controller. 
Please help me
public class UpdateGoogleApiController {

    public string endpoint{get; set;}

    //Insert the new custom setting record in the database
    public void saveRecord(){
        CREDE__GoogleAPIEndPoint__c googleKey  = [SELECT Id, name from CREDE__GoogleAPIEndPoint__c][0];
       CREDE__GoogleAPIEndPoint__c gAPI  = new CREDE__GoogleAPIEndPoint__c();
        gAPI.Id = googleKey.Id; 
        gAPI.CREDE__EndpointURL__c = endpoint;
       
        Update gAPI;
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'API key value is updated Successfully.Thank you!'));
        //Redirect to new record detail page
      // return new PageReference('/' + googleKey.Id);
      // return null;
    }
}

 
ApexPages.addmessage not displaying when called from Custom Button in vf page, but error is displaying in debug logs . How to acheive this
  • December 12, 2019
  • Like
  • 0

APEXCLASS:

i have got the class and derserilized to the respective Class, now need to append the data to the picklist using the Selectlist  using the Repeat Functionality...!


 

public class test12 {

    
 
     
   
     public  string selectedOption{get;set;} 
     public Wrapper Wrapper1{get;set;}
     public list<wrapper> lstWrapper1{get;set;}
     
  public test12(){
      lstWrapper1 = new list<wrapper>();
    
      
    string  jsonexample1 =  ' { "overalldata": [ {"stateName": "Andrapradesh",  "rating": "5.0" , "active": "yes" ,"Capital":"Amaravathi"}, { "stateName": "Telangana",  "rating": "4.0" ,"active": "no","Capital":"Hyderabad" }, {"stateName": "Karnataka",  "rating": "5.0" ,"active": "no","Capital":"Banglore"} , {"stateName": "Maharastra",  "rating": "4.5" ,"active": "no","Capital":"Mumbai"}] } ';

        Map<String, Object> mapobj1= (Map<String, Object>)JSON.deserializeUntyped(jsonexample1);
        system.debug('the Mapped and deserialized values are:'+ mapobj1); 
        Object objj1 = (object)mapobj1.get('overalldata');
        system.debug('the data value is :::'+objj1);
        string  SerilizeEmpdata1 = system.json.serialize(objj1);
        system.debug(SerilizeEmpdata1);
    lstWrapper1 =(list<wrapper>)system.json.deserialize(SerilizeEmpdata1,list<wrapper>.class);
     system.debug(lstWrapper1);
       
       
    
  
      public class Wrapper{
      public string  stateName{get;set;}
      public string  rating{get;set;}
      public string  active{get;set;}
      public string  Capital{get;set;}
      
      
      
      }
}
 

Need to Append the data using the Repeat functionality to selectoption in salesforce
 

<apex:page controller="test12" >
 
<apex:form >

           
            
    <apex:selectList multiselect="false" size="1">
     <apex:repeat value="{!lstWrapper1}" var="a" >

    <!--showing an error while appending to the select option--->
  </apex:repeat>
     
    </apex:selectList>
    
     
</apex:form>
 
</apex:page>
 


Note:
 <apex:selectoption  itemlabel ="{!lstwrapper.name}"  and itemvalue= "{!lstwrapper.rating}" html-data-capitalname ="{!lstwrapper.capitalname}" />

//how it can be acheived....

Thanks 
Deepika Reddy...

 

Create 3 custom Objects
a --> b -->c
c is grand child
a is grand parent
display the count of c in grandparent A
using trigger
in lookup relation
New to coding and would really appreciate any help that can be provided.

I am trying to write a test class for a function I have written that creates Opportunities for each account in a list.  On compile I am receiving the error 'Method does not exist or incorrect signture: void CreateOpptys(Id, List) from the type multiPicklistCtrl.  Can someone please tell me what I am doing wrong?

This is my test class:

static testmethod void CreateOpptysTest(){
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account pAcct = new Account(Name = 'Test Property account', RecordTypeId=RecordTypeIdPropAccount);
        insert pAcct;
        
        PropertySpace__c pSpace = new PropertySpace__c(Name='Test Property Space', Property__c = pAcct.Id, SquareFootage__c = 2000);
        insert pSpace;

        List<Account> accts = new List<Account>();
        for (Integer i=0; i<20; i++) {
            accts.add(new Account(Name = 'Test Account -' +i, RecordTypeId=RecordTypeIdPropAccount));
        }
        insert accts;
        
        accts = [Select Id from Account];
        
        Test.startTest();
          multiPicklistCtrl.CreateOpptys(pSpace.id,accts);
          Test.stopTest();
    }

Here is the function I am trying to write the test for:

@AuraEnabled
    public static void CreateOpptys(Id propertyId, List<Id> selectedAccts){
        system.debug('From Create Oppty propertyId1>>>'+propertyId);
        system.debug('From Create Oppty selectedAccts>>>'+selectedAccts);
        //Need to search through existing Opportunities and if there is an active Opty created for the Account and Property, do not create another Oppty.
        PropertySpace__c propSpace = new PropertySpace__c();
        Id propSpaceId;
        if(string.valueof(propertyId).startsWith('006')){
            opportunity opp= [SELECT PropertySpace__c FROM Opportunity WHERE Id = :propertyId];
               propSpace= getProp(opp.PropertySpace__c);
            propSpaceId = opp.PropertySpace__c;
        }
        else{
            propSpace = [SELECT Name,SquareFootage__c, Property__c FROM PropertySpace__c WHERE Id = :propertyId];
            propSpaceId = propertyId; 
        }
            
        Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Vacancy Fill').getRecordTypeId();
        
         
        list<Opportunity> lstOpptys = new List<Opportunity>();
        for(integer i=0; i< selectedAccts.size(); i++){
            Opportunity o = new Opportunity(Name = propSpace.Name +i,
                                            CloseDate = date.today().addDays(90),
                                            StageName = 'Prospect',
                                            RecordTypeId = recTypeId,
                                            AccountId = selectedAccts[i]);

        lstOpptys.add(o);
        }    
        insert lstOpptys;
        
            
    }

 
Hello all,

I'm trying to populate owner's name to Enrollment_Rep__c field on Lead Object based on the profile name.  Everything works till I change the Owner from User to Queue.  I'm running to System.NullPointerException on the trigger.
trigger EnrollmentRep on Lead (before insert, before update) {
    
    Set<Id> ownerIds = new Set<Id>();

    for (Lead newLead : Trigger.new) {
        ownerIds.add(newLead.OwnerId); 
    }

    Map<Id,User> mapUsers = new Map<Id, User>([SELECT Id, Profile.Name, Name FROM User Where Id In :ownerIds]);

    for (Lead newLead : Trigger.new) {
        User oOwner = mapUsers.get(newLead.OwnerId);
        
        if (oOwner.Profile.Name == 'CEO'){
            newLead.Enrollment_Rep__c = oOwner.Name;
        }
    }
}

I'm new to the trigger and I'm not sure where to use IF statement Lead Owner != Queue.

Any suggestions will be helpful.

Thanks!
​​​​​​​John
 
Can I have the scenario where I can have two records with the same Salesforce Ids with different cases. 
e.g. 
a124T000000kDCa   -- Ajay
a124t000000KDca   -- Hari 

The above is an example of 15 digit Id. Is this scenario possible ?
Is the same scenario possible with 18 digit Ids as well.

Please help.
hey Guys

i need a small help regarding a test classes. Basically i am trying to design a test class for account and contact, in my case Account is Renamed as (Courses) and Contact is Renamed as (Students), and i have 2 custom objects Classes(Class__c) and Attendancess (Attendancess__c)

how do i design a test class for below apex class
public class UpdateUsingCheckboxC {
    
    @AuraEnabled
    public static List <Contact> fetchContact(string key) {
        system.debug('classname'+key);
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        string classname = cls.Courses__r.name;
        system.debug('classname'+classname);
        return [SELECT Id, Name,Account.Name,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c FROM Contact where (Check_for_Attendance__c=false AND Check_for_Absentee__c=false) AND Account.Name =:classname];
    }
    
    @AuraEnabled
    public static void updateRecord(List <String> lstRecordId,string key) {
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        if(cls.Id !=null){
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : lstRecordId]){
                con.Check_for_Attendance__c = true;
                con.Check_for_Absentee__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = true;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
    @AuraEnabled
    public static void updateSecondRecord(List <String> secondRecordId,string key) {
        system.debug('key1'+key);
        system.debug('secondRecordId '+secondRecordId);
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        if(key !=null){
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : secondRecordId]){
                con.Check_for_Absentee__c = true;
                con.Check_for_Attendance__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = false;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
}

 
I'm new to apex coding and have run into a bit of a problem which I'm hoping someone can help with. 

Basically, I've been tasked with a Salesforce deployment for my company a part of which requires a customer portal for viewing cases. Following some early prototyping we decided to use a piece of apex programming to display the case list in a format that is suited to our needs. The list basically consists of a drop down box that has two options, depending on the option chosen the class will pull data from Salesforce. this is being used on a visualforce page that is embedded into the community site.

I've used this code in Sandox with no issues but when I try to deploy to Production it fails due to only 71% code coverage. 
 
public class CommunityCaseList {

public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}



I've read up on it and believe I need to create a test class to run the code but when I'm trying to create one it displays the code as incorrect. This is the test class:
 
@isTest

private class CommunityCaseListTest {
 
    static testMethod void validateCommunityCaseList() {


public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}

}



I'm not quite sure what I'm doing wrong or if there's an easier way to achieve the code coverage required. The tutorials I've read so far are for creating test classes that manipulate data whereas this is simply used to get data.

If anyone can help point me int he right direction it would be greatly appreciated. 

Many thanks

Andy
I am trying to run this code in Anonymous window:

List<Contact> ConList = New List<Contact>();
ConList = [Select id, name, Account.name, Parent_Account__r.name from Contact where Parent_Account__r.name != Account.Parent.name];
System.debug('Contact records :    ' + Con1);

getting error:

Line: 2, Column: 9
Unexpected token '='.

I need to run below query in Dev console or workbench to get the contact records where Parent Account on Contact is not equal to Parent Account of Account:

select id, name, Account.name, Parent_Account__r.name,(Select id,name, Parent.name from Account) from Contact where Parent_Account__r.name != Account.Parent.name

Please help

 
Hi All,
I have the below requirement. I have one parent and 2 childs. i want to get ids of two child records and update a field with id of child 2 on child1 and vice versa. How can i do this?

Thanks.
  • August 14, 2018
  • Like
  • 0
Hello,
I am creating a simple custom login page with 2 fields username, password and a login button. 
I have created a new custom object - loginUsers to store the username and password. 
When I enter the credentials as per in that object it is not working and it is not showing any error message also. Please help me to fix this issue. 

VF: 
<apex:page controller="login" sidebar="false" showHeader="false" standardStylesheets="false">

<body style="background-image:url('{!$Resource.image}');"></body>
 <apex:form >
 
 <center style="margin-top:250px" >
 <apex:image url="{!$Resource.Nature}" width="250" height="100"/><br/>
 
 <br/>
  <apex:inputText id="Username" value="{!username}" /> <br/><br/>
 <apex:inputSecret id="password" value="{!password}"/>/><br/>

  <apex:commandButton value="login" action="{!login}"/>
  <apex:commandLink value="forgetpassword"/>

 </center>
 </apex:form>
</apex:page>

Apex class: 
public class login {
    
    public string username {get; set;}
    public string password {get; set;}
    public list<loginUsers__C> u =[select name,password__C from loginUsers__c where name = :username and password__c =:password];
   
   
 
    public pagereference login()
        {
        if(u.size()>0){
                 pagereference pgref = new pagereference('/apex/loginsf1');
            pgref.setredirect(true);
                 return pgref ;
        }
       
         else
         apexpages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'please enter the username and password'));

         return null;
        }
  
    
}

Thank you very much in advance. 
Regards, 
Sujendran.