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.