4

I have written a batch class which picks merged ideas and sent certain notification emails to idea creators.Batch works fine. Now same functionality has be tested in test class. In the test class, how to merge two ideas so that when my test class executes batch then batch should get right data to process.

Any thoughts?

1 Answer 1

1

Looking in the documentation for Idea it sounds like you should first insert both ideas. Then on one idea that is to be merged set the ParentIdeaId so that it becomes the merged child. Salesforce should automatically set the IsMerged field to true when queried from then on.

Update I tried this approach to insert and then merge two ideas:

List<Idea> ideas = new List<Idea>();

Idea idea1 = new Idea();
idea1.CommunityId = '09a5000000005HpAAI'; 
idea1.Title = 'Test Idea 1';
ideas.add(idea1);

Idea idea2 = new Idea();
idea2.CommunityId = idea1.CommunityId;
idea2.Title = 'Test Idea 2';
ideas.add(idea2);

insert ideas;

idea2.ParentIdeaId = idea1.Id;
update idea2;

This failed with the following message:

Field is not writeable: Idea.ParentIdeaId line:15

I also tried Merging Records in Apex:

merge idea1 idea2;

This failed with the message:

Specified type Idea cannot be merged line:16

Which makes sense as the docs say:

The only supported object types are Lead, Contact and Account.

This appears to be one of the rare occasions where you will need to use @IsTest(SeeAllData=true) to access realistic test data. You might be able to use Test.loadData(Idea.sObjectType, 'myResource'); to create the Ideas, but I haven't tried it in practice.

You might like to vote for the Idea Exchange post Allow Apex to Merge Idea records


In terms of passing the merged idea to the batch, see Adding a Test for the Batch Apex Class. The important part is to enclose the Database.executeBatch call between a Test.startTest(); and Test.stopTest(); call.

1
  • 2
    ParentIdeaId field is neither createable nor updateable.
    – doga
    Nov 18, 2015 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .