You need to sign in to do that
Don't have an account?
plai83
Case Team Members trigger
On my case object, I have a lookup field to the User object. Whenever that field changes, I want to add that user to the Case Team Member. I have everything set up to that, but if the lookup changes back to a user that's already apart of the Case Team, I get an error saying the user already exists in the case teams.
For the life of me, I can't seem to figure out the logic that would prevent an insert if the user already exists. Any help would be greatly appreciated. Thanks.
trigger AfterUpdate on Case (after update) { List<CaseTeamMember> ctm = new List<CaseTeamMember>(); for(case c: trigger.new){ Case oldCase = Trigger.oldMap.get(c.Id); if(oldCase.Case_Manager__c != c.Case_Manager__c){ CaseTeamMember ctmadd = new CaseTeamMember(); ctmadd.ParentId = c.id; ctmadd.MemberId = c.Case_Manager__c; ctmadd.TeamRoleId = '0B7A0000000PFuq'; ctm.add(ctmadd); } insert ctm; } }
I was able to solve this by doing the following:
Create a set outside the for loop
Then inside the for loop
then finally do the delete outside my for loop
Thank you for taking the time to look at my code and helping out. Much appreciated!
All Answers
Hi, I checked your code, you need to add some logic to your code.
When Case Member is changed. You need to do:
if(Old Case Member != null)
{
//delete case team member for old case member;
}
else if(Case Member != null)
{
//Add a New Case Member to ctm;
}
Hope this helps.
Thanks,
Samba
Thanks Samba,
I used your logic and it worked great.
What logic would I need if I wanted to keep the Case team member instead of deleting it when the the Case Member changes?
Hi,
I thought Case Member and Case Team Member has some relationship. If you still have not solved this question, you can send your develope account to my email. I will check when I have some time.
Thanks,
Samba
I seem to be running into SOQL limitations with the revised code. Is there a better way to write this?
Hi,
I think you can do it. like:
Set<String> caseMembers = new Set<String>();
for(Case c : Trigger.new)
{
Case oldCase = Trigger.oldMap.get(c.Id);
if(oldCase.Case_Manager__c != null || oldCase.Case_Manager__c == null) // I think this row code is not correct.
{
caseMembers.add(oldCase.Case_Manager__c);
caseMembers.add(c.Case_Manager__c);
}
}
for(CaseTeaMember ctmrem : [select Id from CaseTeamMember where MemberID in :caseMembers])
{
//to do something
}
Hope this helps.
Thanks,
Samba
I was able to solve this by doing the following:
Create a set outside the for loop
Then inside the for loop
then finally do the delete outside my for loop
Thank you for taking the time to look at my code and helping out. Much appreciated!
In Line #15, there is a Select Statement on the Case TeamMember object. This SOQL is executed for every Case Record. And this is the reason for casuing the TOO MANY SOQL errors. Probably you have to do this.
1. In the begining itself (before line #9), try to prepare list of Ids & Manager Ids. You can do this by 3-4 line statments
2. Next, query the CaseTeamMember with the list you have prepared
3. Based on the Query results, prepare a Map (Case Id,List<CaseTeamMemberS>
4. Instead of line #15, use the map prepared in step #3.
Great post, very usefull!
My question is, what would a Test Class for something like this look like?