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
charlie_vcharlie_v 

Help with Controller Extension test method - Parent/Child relationship

Hi there.

 

Found this code on the boards for a controller Extension and got it working ok, but am struggling with the test method.

 

How do I create a parent record and get it associated with the child record for the test ?? I think the relationship is messing me up ...

 

 

public class PostCommentExtension { public List<Headline_Comment__c> nhc {get; set;} private final Headline__c parHeadline; public PostCommentExtension(ApexPages.StandardController myController) { parHeadline=(Headline__c)myController.getrecord(); nhc = new List<Headline_Comment__c>(); Headline_Comment__c Comment = new Headline_Comment__c(); Comment.Headline__c = parHeadline.id; nhc.add(Comment); } public PageReference save() { insert nhc; PageReference home = new PageReference('/headlines_with_comments?id='+parHeadline.id); home.setRedirect(true); return home; } public PageReference cancel() { PageReference back = new PageReference('/headlines_with_comments?id='+parHeadline.id); back.setRedirect(true); return back; } }

 

 Does anyone have any code they can paste here so I can see how to get this running?  Sorry if this is basic ... Admin going on Developer and still learning.

 

Thank you.

Charlie

 

bob_buzzardbob_buzzard

I'd do something like the following:

 

 

Headline__c headline=new Headline__c();
// set any fields on the headline


insert headline;

// instantiate the extension controller, passing a standard controller
// populated with our headline

PostCommentExtension controller=new PostCommentExtensionController(new ApexPages.StandardController(headline));

controller.save();

 


 

Message Edited by bob_buzzard on 01-08-2010 12:57 AM
Message Edited by bob_buzzard on 01-08-2010 12:58 AM
charlie_vcharlie_v

Getting the following error when I try that:

 

Error: Compile Error: Invalid type: PostCommentExtensionController at line 30 column 45

 

 

static testMethod void testPostCommentExtension() { // Create new Headline__c Headline__c headline = new Headline__c(Name='Test Headline1',Item__c='Test Item1'); insert headline; // Instantiate the extension controller, passing a standard controller // populated with our headline PostCommentExtension controller=new PostCommentExtensionController(new ApexPages.StandardController(headline)); controller.save(); }

 

 

 

 

 

bob_buzzardbob_buzzard
Simple typo - change PostCommentExtensionController to PostCommentExtension