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
Eugenia PestelliEugenia Pestelli 

Error validating from sandbox to production INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Hello everybody, can anyone help me to solve this problem? I have a trigger and its test class to take from sandbox to production. Once brought into production I get this error: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Stack Trace: Class.TestPreventcontentdocument.testTrigger: line 10, column 1. Now I guess the problem is the Id but how can I fix it?


This is my trigger:

trigger Preventcontentdocument on ContentDocumentLink (after insert) { Set<Id> parentIds = new Set<Id>(); for(ContentDocumentLink cdl : trigger.new){ parentIds.add(cdl.LinkedEntityId); } if(parentIds.size() > 0) { List<Candidate__c> updatecandidatelist = new List<Candidate__c>(); List<Candidate__c> cadlist = [SELECT Id, Curriculum_Attachment__c FROM Candidate__c WHERE Id IN: parentIds]; if(cadlist.size() > 0) { for(Candidate__c cd : cadlist) { cd.Curriculum_Attachment__c = true; updatecandidatelist.add(cd); } update updatecandidatelist; } } }


this is my test class:

@isTest private class TestPreventcontentdocument { // Test that verifies that the trigger sets the Curriculum_Attachment__c field to true // when a new ContentDocumentLink is inserted static testMethod void testTrigger() { //create testdata Candidate__c cad = new Candidate__c(Surname__c='SurnameTest', Name__c='NameTest'); cad inserts; ContentDocumentLink cdl = new ContentDocumentLink(LinkedEntityId = cad.Id, ContentDocumentId ='0693L000001YtCDQA0'); insert cdl; // Verify that the Curriculum_Attachment__c field of the candidate is set to true cad = [SELECT Curriculum_Attachment__c FROM Applicant__c WHERE Id = :cdl.LinkedEntityId]; System.assertEquals(true, cad.Curriculum_Attachment__c); } }

Please help me in this.. thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Eudenia,

You are hardcoding the ContentDocumentid in your test class. There is chance that the Id may not exit in production. You need to remove the hardcoding of the id in test class and try again.

Thanks,
 
Eugenia PestelliEugenia Pestelli
I proceeded to eliminate the SOUTH, the sandbox test passes, but when I bring it to production it fails, now I get this error System.AssertException: Assertion Failed: Expected: true, Actual: false Stack Trace: Class.TestCurriculumAttachmentTrigger.testTrigger: line 24, column 1.
@isTest
private class TestCurriculumAllegatoTrigger {
  
    static testMethod void testTrigger() {
    Id adminUserId = '0055p00000BIUMt';
        User adminUser= [SELECT Id FROM User WHERE ID= :adminUserId LIMIT 1];
        System.runAs(adminUser) {
        ContentVersion contentVersion = new ContentVersion();
        contentVersion.Title = 'Test Content';
        contentVersion.PathOnClient = 'Test Content.txt';
        contentVersion.VersionData = Blob.valueOf('Test Content');
        insert contentVersion;
        ContentDocument contentDocument = [SELECT Id FROM ContentDocument WHERE LatestPublishedVersionId =:contentVersion.Id];

        Candidato__c cad = new Candidato__c(Cognome__c='CognomeTest', Nome__c='NomeTest');
        insert cad;
        // Verify that the Curriculum_Allegato__c field of the candidate is set to false
        cad = [SELECT Curriculum_Allegato__c FROM Candidato__c WHERE Id = :cad.Id];
        System.assertEquals(false, cad.Curriculum_Allegato__c);
        ContentDocumentLink cdl = new ContentDocumentLink(LinkedEntityId = cad.Id, ContentDocumentId = contentDocument.Id);
        insert cdl;
        // Verify that the Curriculum_Allegato__c field of the candidate is set to true after the trigger is fired
        cad = [SELECT Curriculum_Allegato__c FROM Candidato__c WHERE Id = :cad.Id];
        System.assertEquals(true, cad.Curriculum_Allegato__c);
    }
}
}