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
phil.vaseyphil.vasey 

Delete record having ContentPost chatter raises obscure exception

private static testmethod void UnitTest_DeleteOpportunity()
{
// create an Account

Account testAccount = new Account() ;
testAccount.Name = 'Alpha Corporation' ;
insert testAccount ;

// create an Opportunity on that Account

Opportunity testOpportunity = new Opportunity() ;
testOpportunity.AccountId = testAccount.Id ;
testOpportunity.Name = 'Alpha Holdings PLC' ;
testOpportunity.StageName = 'Negotiation/Review' ;
testOpportunity.CloseDate = Date.Today() ;
insert testOpportunity ;

// create a ContentVersion

ContentVersion testContentVersion = new ContentVersion() ;
testContentVersion.Title = 'Test Document' ;
testContentVersion.PathOnClient = 'Test Document.docx' ;
testContentVersion.VersionData = Blob.ValueOf( 'Test Document Version' ) ;
testContentVersion.Origin = 'H' ;
insert testContentVersion ;

// add that ContentVersion as a FeedItem to the Opportunity

FeedItem testFeedItem = new FeedItem() ;
testFeedItem.ParentID = testOpportunity.Id ;
testFeedItem.RelatedRecordID = testContentVersion.Id ;
testFeedItem.Type = 'ContentPost' ;
insert testFeedItem ;

// test: delete the Opportunity raises an exception

String expectedStart = 'Delete failed. First exception on row 0 with id ' ;
String expectedEnd = '; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []' ;
String actual = '' ;
try
{
delete testOpportunity ;
}
catch ( Exception e )
{
actual = e.GetMessage() ;
}
System.AssertEquals( true, actual.StartsWith( expectedStart ) ) ;
System.AssertEquals( true, actual.EndsWith( expectedEnd ) ) ;
}

 

 

Can anybody explain why this exception is being raised?