You need to sign in to do that
Don't have an account?
Apex developer 21
Can anyone help me to make a unit test for this it keeps giving me 37%
trigger NoteOnContentversion on ContentVersion (before insert, before update) { for (ContentVersion c : Trigger.new) { for (ContentDocumentLink link : [ SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :c.ContentDocumentId ]){ Id parentId = link.LinkedEntityId; System.debug('link.LinkedEntityId '+ link); Boolean isOrderRegel = parentId.getSObjectType() == Orderregel__c.SObjectType; if (isOrderRegel && Approval.isLocked(parentId)){ c.addError('Approval pending. You do not have the permission to edit this note, Please contact your administrator.'); } } } }
@IsTest private class NoteOnContentversion { @IsTest static void refuse_shouldAddError_whenOrderregelIsLocked() { // arrange Account a = new Account( Name = '123', Account_Status_DS__c = 'Strategic' ); insert a; Orderregel__c orderregel = new Orderregel__c( Account__c = a.Id, Orderbegindatum__c = Date.today() ); insert orderregel; Approval.lock(orderregel); ContentVersion n = new ContentVersion(); insert n; ContentDocumentLink cl = new ContentDocumentLink(); insert cl; // act DmlException caughtException; try { insert cl; insert n; } catch(DmlException e) { caughtException = e; } // assert System.assertNotEquals(null, caughtException); } }
All Answers
Instead of inserting the newly created records at 2 places, only try doing so where you can catch the exception(s) -
try{Here}catch(DmlException e){handle exception}
and try to manage your assertion in the excepotion handling.
Try the above
Make sure your assertion is within range of your catch.
I hope this helps, if not, you can always post developer oriented questions here (http://salesforce.stackexchange.com/)
Maybe there's some validation on ContentDocumentId and I cannot add a reference to the account there?
Lets try null instead?
When a test fails, it covers no code. On which line did the test fail and what is the failure cause?
Perhaps no exception was thrown, which caused assertion failure?
07:56:54:267 EXCEPTION_THROWN [22]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NoteOnContentversion: execution of BeforeInsert
07:56:54:000 FATAL_ERROR caused by: System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.