Custom label:
Name: Case_Number_Details
Value: Please enter the valid 8 character Case Number (e.g. 00123456) after the equal sign in the URL above to view the case details.
Visualforce Page:
<apex:page controller="GetCaseByNumber" title="Get Case By Number" showHeader="false">
<script>
if ("{!CaseQuery}".length > 0) {
if ("{!CaseQuery}" != "<apex:outputText value="{!$Label.Case_Number_Details}"/>" && "{!CaseQuery}" != "You Must Supply a Case Number") window.location = "/{!CaseQuery}";
}
</script>
<h1 style="display:block;margin:25px;font-size:16px;">{!CaseQuery}</h1>
</apex:page>
Apex Class:
public class GetCaseByNumber {
public static String getCaseQuery () {
String caseNumberDetails = Label.Case_Number_Details;
string caseNumber = ApexPages.CurrentPage().getParameters().get('CaseNumber');
List <Case> c = [SELECT Id FROM Case Where CaseNumber =: caseNumber];
if (c.size()>0) {
return string.valueOf(c[0].Id);
}
else return caseNumberDetails;
}
}
private class GetCaseByNumberTest {
static testmethod void TestAccdata(){
Profile prof = [select id from profile where name='System Administrator'];
User us = new User(alias = 'tuser', email='TestUser@testingorg.com',
emailencodingkey='UTF-8', lastname='TestingUser', languagelocalekey='en_US',
localesidkey='en_US', profileid = prof.Id,
timezonesidkey='America/Los_Angeles', username='TestUser@testingorg.com');
insert us;
System.runAs (us) {
Account ac = new Account(Name ='Test', OwnerId =us.Id);
insert ac;
Contact cn = new Contact (FirstName = 'Test', LastName = 'Test',AccountId = ac.Id,email='testcase@org.com');
insert cn;
Test.startTest();
string RTID = [select id from RecordType where SobjectType = 'Case' and Name = 'Support Case'].Id;
System.assertNotEquals(null,RTID);
case c = new Case(AccountId=ac.Id ,ContactId = cn.Id,Status='New',Origin='Phone',RecordTypeId = RTID,Subject = 'TestCase',Description ='Test',Product_Type__c = 'My Product);
insert c;
PageReference myVfPage = Page.getCaseByNumber;
Test.setCurrentPage(myVfPage);
ApexPages.currentPage().getParameters().put('CaseNumber',ApexPages.currentPage().getParameters().get(c.CaseNumber));
GetCaseByNumber controller = new GetCaseByNumber();
GetCaseByNumber.getCaseQuery();
Test.stopTest();
}
}