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
satakshisatakshi 

Soql query to check duplicate name

Hello,

I am wriring soql query where i want to check duplicate survey name.
If  survey with name "Test" is already exist then survey with " Test Survey" name will save successfully. It will show error msg if we try to save using these names "T", "Te" , "Tes", "Test". I want to change my soql query (Like) so it will check for duplicate properly.

public class QuestionRepositorySave{

public Question_Repository__c ques;
public string qname{get;set;}
public string msg{get;set;}
public string description{get;set;}


public QuestionRepositorySave(ApexPages.StandardController controller){

ques = new Question_Repository__c();

}

public PageReference SaveAndRedirect(){

//List<Question_Repository__c> qList = [select id, name from Question_Repository__c where Repository_Name__c like:qname];
String query = 'select id, name from Question_Repository__c where Repository_Name__c like \'%' + qname+ '%\'';


 System.debug('************************************+query+*****************************************************'+query);

List<Question_Repository__c> qList= Database.query(query);
 System.debug('************************************+qList +*****************************************************'+qList);


if(qList.isEmpty())
{

ques.Repository_Name__c = qname;
ques.Description__c = description;
 System.debug('************************************+BS+*****************************************************'+qname);

insert ques;
     System.debug('**##################***********+aS+*************##############**********'+qname);

 PageReference pr = new PageReference('/apex/SBDemoMainQB');
 pr.setRedirect(true);
 return pr;
}


 else {       
    System.debug('**##################***********+aS+*************##############**********');
  //  msg ='This record is already exists';
  //  return null;
 PageReference es= new PageReference('/apex/SurveyNamePage');
 es.setRedirect(true);
 return es;
 
      }

return null;
}
 public PageReference Cancel()
  {
    PageReference pr1 = new PageReference('/apex/SBQuestionBank');
    pr1.setRedirect(true);
    return pr1;
  }
 
}

Thanks & Regards,
Satakshi
SandhyaSandhya (Salesforce Developers) 
Hi,

You can use something like below to see duplicates.

 
List<AggregateResult> acc=[SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(AggregateResult aggres : acc)
{
System.debug('Finding duplicate names'+aggres);
}

Please refer below links


http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_query_aggregateresult.htm
 
Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya