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 

Need to automate adding a user to Case Team - Help!

We have a custom object "Case Resource" that allows us to build a table of resources with applicable bill rates on any given case.  We also use the Case Team object to control access to community users on a case by case basis.  I'd like to create either a workflow or trigger (not sure which is applicable), to automatically add a user to the case team, when they are manually added to the custom object as a case resource.  I need help writing the code on either the workflow or trigger if either could get the job done.
pconpcon
You'll have to do this in a trigger.  Without knowing the structure of CaseResource object, I'll just have to guess.  It would be something like:

CaseTeamRole role = [select Name from CaseTeamRole where Name = 'MyTeamRole'];
List<CaseTeamMember> members = new List<CaseTeamMember>();

for (CaseResource resource: Trigger.new) {
     if (resource.User__c != null && resource.Case__c != null) {
          members.add(new CaseTeamMember(
               ParentId = resource.Case__c
               MemberId = resource.User__c,
               RoleId = role.Id
          ));
     }
}

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

NOTE: This code has not been tested and may contain typographical or logical errors.
pconpcon
Yes, that is correct.  I just did the body of the trigger.  I would look at these two posts about writing triggers.  The first breaks down a trigger line by line.  In your case, you'll be doing it on the CaseResource__c object instead [1].  The second talks about classifying your trigger [2].  It's useful if you will be doing more on this object and want control over order of operations.

The full trigger would look like:

Trigger CaseResource_CaseTeamMemberCreation(after insert) {
     CaseTeamRole role = [select Name from CaseTeamRole where Name = 'MyTeamRole'];
     List<CaseTeamMember> members = new List<CaseTeamMember>();

     for (CaseResource__c resource: Trigger.new) {
          if (resource.User__c != null && resource.Case__c != null) {
               members.add(new CaseTeamMember(
                    ParentId = resource.Case__c
                    MemberId = resource.User__c,
                    RoleId = role.Id
               ));
          }
     }

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

The trigger will only fire after insert and does not take into account if the user is already on the CaseTeamMember list.  You will probably want to modify it to do a check for that if there is a chance that the user is already part of the CaseTeamMembers list

[1] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[2] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/