• Jonathan Wong
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Online Marketing Manager / Certified Salesforce Admin
  • O.Berk Company


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi,

I need some help, I think my problem must be a simple one where I must have a typo, but I've been looking at this for the past two days and I can't figure out where I've made my mistake, can you guys give my code a once over and let me know what I did wrong?

The class is actually functioning correctly, I deployed it and it works as intended (searching an object for a keyword string and return a list of records). But when it comes to the unit test I can't seem to get it right...

The code for my class:
public class PackagingCrashCourseSearch {
	
	public String incomingkw = ApexPages.currentPage().getParameters().get('q');	
	public String keyword = '%'+incomingkw+'%'; 
	public pccListItems[] pccList {get; set;} // List of all Product Spotlights
	
	public PackagingCrashCourseSearch(){
		pccList = getpccList();
	}
	
	// All Product Spotlights List
    public pccListItems[] getpccList(){
    	date target_date;
    	target_date = Date.today()+1;
        
        List<List<sObject>> searchPost = [FIND :keyword IN ALL FIELDS RETURNING PCC_Post__c(Post_Title__c,Short_Description__c,Publish_Date__c,Public_URL__c,Post_By__c WHERE Publish_Date__c < :target_date ORDER BY Publish_Date__c DESC)];
		PCC_Post__c[] pccList = (PCC_Post__c[])searchPost[0];
        
        if (pccList.size() == 0)
            return null;
                
        Map<ID, PCC_Post__c> pccMap = new Map<ID, PCC_Post__c>();
        for (Integer i = 0; i < pccList.size(); i++){
            if (pccList.size() != 0){
            	PCC_Post__c pccItem = pccList[i];
                pccMap.put(pccItem.ID, pccItem);
            }
        }
        
        Map<ID, ID> pccAttachments = new Map<ID, ID>();
        for (Attachment att : [SELECT ParentID FROM Attachment WHERE Name LIKE '%-thumb%' AND ParentID IN :pccMap.keySet()]){
            pccAttachments.put(att.ParentID, att.ID);
        }
        
        pccListItems[] attLines = new pccListItems[]{};
        for (ID pccID : pccMap.keySet()){
            attLines.add(new pccListItems(pccID, pccMap.get(pccID), pccAttachments.get(pccID)));
        }
        return attLines;
    }
    // --- END All Product Spotlights List
    
    public class pccListItems {
        public ID pccID {get; set;}
        public String title {get; set;}
        public String pccUrl { get; set; }
        public Date publishedDate { get; set; }
        public String pccDesc { get; set; }
        public ID attID {get; set;}
        public String postBy { get; set; }
        
        public pccListItems(ID pccid, PCC_Post__c pcc, ID attchID){
            pccID = pccid;
            title = pcc.Post_Title__c;
            pccUrl = pcc.Public_URL__c;
            publishedDate = pcc.Publish_Date__c;
            pccDesc = pcc.Short_Description__c;
            attID = attchID;
            postBy = pcc.Post_By__c;
        }
    }

}

My Unit Test Code:
/*
	Test class for PackagingCrashCourseSearch
*/
@isTest
public class PackagingCrashCourseSearchTest {
	
	public static testmethod void testPackagingCrashCourseSearch(){
		
		String testQry = 'test';
		Date tPostDate = Date.newInstance(2015,1,1);
		
		PCC_Post__c pccPost = new PCC_Post__c(Active__c = true,Publish_Date__c = tPostDate, Post_Title__c = testQry,Post_Body__c = 'TEST Test test');
		insert pccPost;
		
		Attachment attach = new Attachment();
        attach.Name = 'testPost-thumb.jpg';
        Blob bodyBlob = Blob.valueOf('Test Attachment Body');
        attach.body = bodyBlob;
        attach.parentId = pccPost.Id;
        insert attach;
		
		ApexPages.CurrentPage().getParameters().put('q', testQry);
		
		PackagingCrashCourseSearch pccList = new PackagingCrashCourseSearch();
		
		PackagingCrashCourseSearch.pccListItems[] resultList;
		
		resultList = pccList.getpccList();
		
		system.assertEquals(1,resultList.size());
    }
}

Thank you so much in advance.
Hi,

I need some help, I think my problem must be a simple one where I must have a typo, but I've been looking at this for the past two days and I can't figure out where I've made my mistake, can you guys give my code a once over and let me know what I did wrong?

The class is actually functioning correctly, I deployed it and it works as intended (searching an object for a keyword string and return a list of records). But when it comes to the unit test I can't seem to get it right...

The code for my class:
public class PackagingCrashCourseSearch {
	
	public String incomingkw = ApexPages.currentPage().getParameters().get('q');	
	public String keyword = '%'+incomingkw+'%'; 
	public pccListItems[] pccList {get; set;} // List of all Product Spotlights
	
	public PackagingCrashCourseSearch(){
		pccList = getpccList();
	}
	
	// All Product Spotlights List
    public pccListItems[] getpccList(){
    	date target_date;
    	target_date = Date.today()+1;
        
        List<List<sObject>> searchPost = [FIND :keyword IN ALL FIELDS RETURNING PCC_Post__c(Post_Title__c,Short_Description__c,Publish_Date__c,Public_URL__c,Post_By__c WHERE Publish_Date__c < :target_date ORDER BY Publish_Date__c DESC)];
		PCC_Post__c[] pccList = (PCC_Post__c[])searchPost[0];
        
        if (pccList.size() == 0)
            return null;
                
        Map<ID, PCC_Post__c> pccMap = new Map<ID, PCC_Post__c>();
        for (Integer i = 0; i < pccList.size(); i++){
            if (pccList.size() != 0){
            	PCC_Post__c pccItem = pccList[i];
                pccMap.put(pccItem.ID, pccItem);
            }
        }
        
        Map<ID, ID> pccAttachments = new Map<ID, ID>();
        for (Attachment att : [SELECT ParentID FROM Attachment WHERE Name LIKE '%-thumb%' AND ParentID IN :pccMap.keySet()]){
            pccAttachments.put(att.ParentID, att.ID);
        }
        
        pccListItems[] attLines = new pccListItems[]{};
        for (ID pccID : pccMap.keySet()){
            attLines.add(new pccListItems(pccID, pccMap.get(pccID), pccAttachments.get(pccID)));
        }
        return attLines;
    }
    // --- END All Product Spotlights List
    
    public class pccListItems {
        public ID pccID {get; set;}
        public String title {get; set;}
        public String pccUrl { get; set; }
        public Date publishedDate { get; set; }
        public String pccDesc { get; set; }
        public ID attID {get; set;}
        public String postBy { get; set; }
        
        public pccListItems(ID pccid, PCC_Post__c pcc, ID attchID){
            pccID = pccid;
            title = pcc.Post_Title__c;
            pccUrl = pcc.Public_URL__c;
            publishedDate = pcc.Publish_Date__c;
            pccDesc = pcc.Short_Description__c;
            attID = attchID;
            postBy = pcc.Post_By__c;
        }
    }

}

My Unit Test Code:
/*
	Test class for PackagingCrashCourseSearch
*/
@isTest
public class PackagingCrashCourseSearchTest {
	
	public static testmethod void testPackagingCrashCourseSearch(){
		
		String testQry = 'test';
		Date tPostDate = Date.newInstance(2015,1,1);
		
		PCC_Post__c pccPost = new PCC_Post__c(Active__c = true,Publish_Date__c = tPostDate, Post_Title__c = testQry,Post_Body__c = 'TEST Test test');
		insert pccPost;
		
		Attachment attach = new Attachment();
        attach.Name = 'testPost-thumb.jpg';
        Blob bodyBlob = Blob.valueOf('Test Attachment Body');
        attach.body = bodyBlob;
        attach.parentId = pccPost.Id;
        insert attach;
		
		ApexPages.CurrentPage().getParameters().put('q', testQry);
		
		PackagingCrashCourseSearch pccList = new PackagingCrashCourseSearch();
		
		PackagingCrashCourseSearch.pccListItems[] resultList;
		
		resultList = pccList.getpccList();
		
		system.assertEquals(1,resultList.size());
    }
}

Thank you so much in advance.
Hi

I am not finding Feed-Based Layout Option while creating page layout for custom objects. The same is available for standard object like Account. Is it expected behavior?

Thanks
Priyadarshi