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
Daniel BlevinsDaniel Blevins 

Deployment Failed for the following code

I am getting this error and it fails the deployment of my code, what am I missing to get this working? 

Class NameMethod NameError MessageABSI_TEST_MassTaskControllermyUnitTestSystem.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId] 
Stack Trace: Class.ABSI_TEST_MassTaskController.myUnitTest: line 68, column 1

It is failing on the following code:

trigger CaseTeamAddition on Case (after insert) {
 
if (Trigger.isAfter){
                If (Trigger.isInsert){
                                CaseTeamRole caseTeamRole = [SELECT Id 
FROM CaseTeamRole 
WHERE Name = 'Case Update Team'
LIMIT 1];
 
                                List<Id> accountIdList = new List<Id>();
                                
                                For (Case currentCase : Trigger.new){
                                                accountIdList.add(currentCase.AccountId);
                                }
                                
                                List<Contact> contactList = [SELECT Id
                                                                                    FROM Contact
                                                                                    WHERE AccountId IN : accountIdList
                                                                                   AND Get_Email_Updates__c = true];
                                
                                List<CaseTeamMember> caseMembersToInsert = new List<CaseTeamMember>();
                                For (Case currentCase : Trigger.new){
                                               
                                                For (Contact currentContact : contactList){
                                                                If (currentContact.AccountId == currentCase.AccountId){
                                                                               
                                                                                CaseTeamMember newMember = new CaseTeamMember(ParentId = currentCase.Id,
                                                                                                                                                                                                MemberId = currentContact.Id,
                                                                                                                                                                                                TeamRoleId = caseTeamRole.Id);
                                                                                caseMembersToInsert.add(newMember);
                                                                }
                                                }
                                }
 
                                if (caseMembersToInsert.size() > 0){
                                                insert caseMembersToInsert;
                                }
                                                               
                                }
}                               
    }
 

Best Answer chosen by Daniel Blevins
v varaprasadv varaprasad
Hi Daniel,

As per my understanding in following test class ABSI_TEST_MassTaskController you are inserting portal user record at line no 68.
Without adding a contact id to the user.
Please add contact id in user record and check once.

For better understanding please post your sample test class code. 

I hope it helps you.
Please let me know in case of other help.

Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Daniel,

As per my understanding in following test class ABSI_TEST_MassTaskController you are inserting portal user record at line no 68.
Without adding a contact id to the user.
Please add contact id in user record and check once.

For better understanding please post your sample test class code. 

I hope it helps you.
Please let me know in case of other help.

Thanks
Varaprasad
This was selected as the best answer
Daniel BlevinsDaniel Blevins
Would i add this via apex or can i add it in salesforce itself if so how?