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
Mohan Raj 33Mohan Raj 33 

I have received the DATE type input on the controller in astring manner then how it converted to date field if any one can know?

I have the controller here I tried to the multiple field filterization but in the birthdatefield only not work it's give an error can any one help me to remove this error:Compile Error: Method does not exist or incorrect signature: YEAR(String) at line 124 column 46
My controller:
public class SortOfContactListController {
    public List<Contact> ContactListTable {get; set;}
    public String ExpressionOfSort = 'name';
    public String DirectionOfSort = 'ASC';
    public List<Contact> ContactResults {get; set;}
    
    public SortOfContactListController() {
        ContactListTable = new List<Contact>();
        ContactResults = new List<Contact>();
    }
    
   /* public string searchtext {
        get;
        set;
    }*/
    
    public string ContactName {
        get;
        set;
    }
    
     public string Phone {
        get;
        set;
    }
    
     public string AccountName {
        get;
        set;
    }
    // String AccountName = Apexpages.currentPage().getParameters().get('AccountName');
    public String BirthDate {
        get;
        set;   
    }
    
    /*public string BirthdateMIN {
        get;
        set;
    } 
    
    public string BirthdateMAX {
        get;
        set;   
    }*/
    
    public string Email {
        get;
        set;
    }
    
    public string MailingCity {
        get;
        set;
    }
    
    public string MailingCountry {
        get;
        set;
    }
    
    public string MailingPostalCode {
        get;
        set;
    } 
    
    
    public String ExpressionSort {
        get {
            return ExpressionOfSort;
        }
        set {
            If(value == ExpressionOfSort) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                ExpressionOfSort = value;
            }
        }
    
    }
    
    public String getDirectionOfSort() {
        If(ExpressionOfSort == Null || ExpressionOfSort == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }
    
    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }
    
    public List<Contact>getContacts() {
        return ContactListTable;
    }
    
    public PageReference ViewData() {
        String FullSortExpression = ExpressionOfSort + ' ' + DirectionOfSort;
        system.debug('ExpressionOfSort:::::'+ExpressionOfSort);
        system.debug(DirectionOfSort);
        
        //String Queryitem = new String();
       String Queryitem = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ORDER BY ' + FullSortExpression +' Limit 1000';
       system.debug(Queryitem);
       
        ContactListTable = DataBase.query(Queryitem);
        system.debug(ContactListTable);
        return Null;
    }
    
    public PageReference Search(){
    String searchquery = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ';
    system.debug(searchquery);
    String condition = '';
    if (ContactName != null && ContactName != '')
        condition += ' and Name LIKE \'' + ContactName + '%\'';
    if (AccountName != null && AccountName != '')
        condition += ' and Account.Name LIKE \'' + AccountName + '%\'';
    if (BirthDate != null && BirthDate != '')
        condition += ' and Birthdate = \'' + YEAR(Birthdate) + '%\'' ;
    if (Phone != null && Phone != '')
        condition += ' and Phone LIKE \'' + Phone + '%\'';
    if (Email != null && Email != '')
        condition += ' and Email LIKE \'' + Email + '%\'';
        
    if (MailingCountry != null && MailingCountry != '')
         condition += ' and MailingCountry LIKE \'' + MailingCountry + '%\'';
    if (MailingCity != null &&  MailingCity != '')
         condition += ' and MailingCity LIKE \'' + MailingCity + '%\'';
    if (MailingPostalCode != null && MailingPostalCode != '')
         condition += ' and MailingPostalCode LIKE \'' + MailingPostalCode + '%\'';
    if(condition != ''){
    searchquery += condition;
    System.debug(' searchquery:::::::: '+searchquery);
    
    ContactResults = DataBase.query(searchquery);
    system.debug(' ContactResults::::::::::: '+ContactResults);
  
    }
    return Null;
    
    }
}
Best Answer chosen by Mohan Raj 33
Gaurav Jain 103Gaurav Jain 103
Hi Mohan,

Please use this your modified code:
public class SortOfContactListController {
    public List<Contact> ContactListTable {get; set;}
    public String ExpressionOfSort = 'name';
    public String DirectionOfSort = 'ASC';
    public List<Contact> ContactResults {get; set;}
    
    public SortOfContactListController() {
        ContactListTable = new List<Contact>();
        ContactResults = new List<Contact>();
    }
    
   /* public string searchtext {
        get;
        set;
    }*/
    
    public string ContactName {
        get;
        set;
    }
    
     public string Phone {
        get;
        set;
    }
    
     public string AccountName {
        get;
        set;
    }
    // String AccountName = Apexpages.currentPage().getParameters().get('AccountName');
    public String BirthDate {
        get;
        set;   
    }
    
    /*public string BirthdateMIN {
        get;
        set;
    } 
    
    public string BirthdateMAX {
        get;
        set;   
    }*/
    
    public string Email {
        get;
        set;
    }
    
    public string MailingCity {
        get;
        set;
    }
    
    public string MailingCountry {
        get;
        set;
    }
    
    public string MailingPostalCode {
        get;
        set;
    } 
    
    
    public String ExpressionSort {
        get {
            return ExpressionOfSort;
        }
        set {
            If(value == ExpressionOfSort) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                ExpressionOfSort = value;
            }
        }
    
    }
    
    public String getDirectionOfSort() {
        If(ExpressionOfSort == Null || ExpressionOfSort == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }
    
    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }
    
    public List<Contact>getContacts() {
        return ContactListTable;
    }
    
    public PageReference ViewData() {
        String FullSortExpression = ExpressionOfSort + ' ' + DirectionOfSort;
        system.debug('ExpressionOfSort:::::'+ExpressionOfSort);
        system.debug(DirectionOfSort);
        
        //String Queryitem = new String();
       String Queryitem = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ORDER BY ' + FullSortExpression +' Limit 1000';
       system.debug(Queryitem);
       
        ContactListTable = DataBase.query(Queryitem);
        system.debug(ContactListTable);
        return Null;
    }
    
    public PageReference Search(){
    String searchquery = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ';
    system.debug(searchquery);
    String condition = '';
    if (ContactName != null && ContactName != '')
        condition += ' and Name LIKE \'' + ContactName + '%\'';
    if (AccountName != null && AccountName != '')
        condition += ' and Account.Name LIKE \'' + AccountName + '%\'';
    if (BirthDate != null && BirthDate != '')
        condition += ' and Birthdate = \'' + Date.valueOf(Birthdate).Year() + '%\'' ;
    if (Phone != null && Phone != '')
        condition += ' and Phone LIKE \'' + Phone + '%\'';
    if (Email != null && Email != '')
        condition += ' and Email LIKE \'' + Email + '%\'';
        
    if (MailingCountry != null && MailingCountry != '')
         condition += ' and MailingCountry LIKE \'' + MailingCountry + '%\'';
    if (MailingCity != null &&  MailingCity != '')
         condition += ' and MailingCity LIKE \'' + MailingCity + '%\'';
    if (MailingPostalCode != null && MailingPostalCode != '')
         condition += ' and MailingPostalCode LIKE \'' + MailingPostalCode + '%\'';
    if(condition != ''){
    searchquery += condition;
    System.debug(' searchquery:::::::: '+searchquery);
    
    ContactResults = DataBase.query(searchquery);
    system.debug(' ContactResults::::::::::: '+ContactResults);
  
    }
    return Null;
    
    }
}
Please mark this as  best answer if your query is resolved, so it will help others in future.

Thanks,
Gaurav Jain
 

All Answers

Dilip_VDilip_V
Hi Mohan,

1.First convert birthdate to date.
2.Then get year using date.year() method.
Try like this.
String dateValue='2015-12-31';//date should be this format
Date x = Date.valueOf('2015-12-31');//Converts string to date
integer i=x.year();//Extract year

Let me know if it helps.

Make it as best answer if it helps.

THanks.
 
Gaurav Jain 103Gaurav Jain 103
Hi Mohan,

Please use this your modified code:
public class SortOfContactListController {
    public List<Contact> ContactListTable {get; set;}
    public String ExpressionOfSort = 'name';
    public String DirectionOfSort = 'ASC';
    public List<Contact> ContactResults {get; set;}
    
    public SortOfContactListController() {
        ContactListTable = new List<Contact>();
        ContactResults = new List<Contact>();
    }
    
   /* public string searchtext {
        get;
        set;
    }*/
    
    public string ContactName {
        get;
        set;
    }
    
     public string Phone {
        get;
        set;
    }
    
     public string AccountName {
        get;
        set;
    }
    // String AccountName = Apexpages.currentPage().getParameters().get('AccountName');
    public String BirthDate {
        get;
        set;   
    }
    
    /*public string BirthdateMIN {
        get;
        set;
    } 
    
    public string BirthdateMAX {
        get;
        set;   
    }*/
    
    public string Email {
        get;
        set;
    }
    
    public string MailingCity {
        get;
        set;
    }
    
    public string MailingCountry {
        get;
        set;
    }
    
    public string MailingPostalCode {
        get;
        set;
    } 
    
    
    public String ExpressionSort {
        get {
            return ExpressionOfSort;
        }
        set {
            If(value == ExpressionOfSort) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                ExpressionOfSort = value;
            }
        }
    
    }
    
    public String getDirectionOfSort() {
        If(ExpressionOfSort == Null || ExpressionOfSort == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }
    
    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }
    
    public List<Contact>getContacts() {
        return ContactListTable;
    }
    
    public PageReference ViewData() {
        String FullSortExpression = ExpressionOfSort + ' ' + DirectionOfSort;
        system.debug('ExpressionOfSort:::::'+ExpressionOfSort);
        system.debug(DirectionOfSort);
        
        //String Queryitem = new String();
       String Queryitem = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ORDER BY ' + FullSortExpression +' Limit 1000';
       system.debug(Queryitem);
       
        ContactListTable = DataBase.query(Queryitem);
        system.debug(ContactListTable);
        return Null;
    }
    
    public PageReference Search(){
    String searchquery = ' SELECT Id, Name, Account.Name, Birthdate, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ';
    system.debug(searchquery);
    String condition = '';
    if (ContactName != null && ContactName != '')
        condition += ' and Name LIKE \'' + ContactName + '%\'';
    if (AccountName != null && AccountName != '')
        condition += ' and Account.Name LIKE \'' + AccountName + '%\'';
    if (BirthDate != null && BirthDate != '')
        condition += ' and Birthdate = \'' + Date.valueOf(Birthdate).Year() + '%\'' ;
    if (Phone != null && Phone != '')
        condition += ' and Phone LIKE \'' + Phone + '%\'';
    if (Email != null && Email != '')
        condition += ' and Email LIKE \'' + Email + '%\'';
        
    if (MailingCountry != null && MailingCountry != '')
         condition += ' and MailingCountry LIKE \'' + MailingCountry + '%\'';
    if (MailingCity != null &&  MailingCity != '')
         condition += ' and MailingCity LIKE \'' + MailingCity + '%\'';
    if (MailingPostalCode != null && MailingPostalCode != '')
         condition += ' and MailingPostalCode LIKE \'' + MailingPostalCode + '%\'';
    if(condition != ''){
    searchquery += condition;
    System.debug(' searchquery:::::::: '+searchquery);
    
    ContactResults = DataBase.query(searchquery);
    system.debug(' ContactResults::::::::::: '+ContactResults);
  
    }
    return Null;
    
    }
}
Please mark this as  best answer if your query is resolved, so it will help others in future.

Thanks,
Gaurav Jain
 
This was selected as the best answer