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
Jon FoyJon Foy 

Help with Trigger for Case Team

I have the following trigger that adds a member to the case team, whenever a custom object "Case_Resource__c" is added to the Case.  The only problem is, the Resource_Name__c on the Case_Resource__c is a lookup value of the Contact object.  So when the Trigger creates a case team memeber, it is created as a Contact.  I want it to be created as a User, or Partner User.

Is there a way to update this code so that the Case Team Memeber Added is for the UserId (or Name...not sure which one) that matches the Contact Name?

trigger CaseResource_CaseTeamMemberCreation on Case_Resource__c (after insert) {
    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Team Member'];
    List<CaseTeamMember> members = new List<CaseTeamMember>();
   
    for (Case_Resource__c resource: Trigger.new) {
        if (resource.Resource_Name__c != null && resource.Case__c != null) {
            members.add(new CaseTeamMember(
                ParentID = resource.Case__c,
                MemberID = resource.Resource_Name__c,
                TeamRoleID = role.Id

           ));
        }
    }
   
    if (!members.isEmpty()) {
        insert members;
    }
   

}
pconpcon
The problem is that you need look up your users based on some information on the contact record.  In the code below, I'm using Email.  This is probably not what you want to use, but should be good enough to get you started.
 
trigger CaseResource_CaseTeamMemberCreation on Case_Resource__c (after insert) {
    CaseTeamRole role = [
        select Name
        from CaseTeamRole
        where Name = 'Team Member'
    ];

    List<CaseTeamMember> members = new List<CaseTeamMember>();

    //Let's assume that email is unique enough, it's probably not
    //  but you can modify this to make it key off of some data you
    //  have that is unique

    Set<Id> contactIds = new Set<Id>();

    for (Case_Resource__c resource: Trigger.new) {
        if (resource.Resource_Name__c != null && resource.Case__c != null) {
            contactIds.add(resource.Resource_Name__c);
        }
    }

    contactIds.remove(null);

    if (!contactIds.isEmpty()) {
        Set<String> externalIds = new Set<String>();
        Map <Id, Contact> contactMap = new Map<Id, Contact>([
            select Email
            from Contact
            where Id in :contactIds
        ]);

        for (Contact contact: contactMap.values()) {
            externalIds.put(contact.Email);
        }

        Map<String, User> externalIdToUserMap = new Map<String, User>();

        for (User u: [
            select Email
            from User
            where Email in :externalIds
        ]) {
            externalIdToUserMap.put(u.Email, u);
        }   

        for (Case_Resource__c resource: Trigger.new) {
            if (resource.Resource_Name__c != null &&
                resource.Case__c != null &&
                contactMap.containsKey(resource.Resource_Name__c)
            ) { 
                Contact contact = contactMap.get(resource.Resource_Name__c);

                if (externalIdToUserMap.containsKey(contact.Email)) {
                    members.add(new CaseTeamMember(
                        ParentId = resource.Case__c,
                        MemberId = externalIdToUserMap.get(contact.Email).Id,
                        TeamRoleId = role.Id
                    ));
                }   
            }   
        }   
        
        if (!members.isEmpty()) {
            insert members;
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors