You need to sign in to do that
Don't have an account?
[Lindsey Kiken]
Apex trigger deleting related list versus inserting new record
I have a case apex trigger that is supposed to add a new case team member when the OwnerId is changed. The goal is to have the new OwnerId be entered into the case team and to not alter the case team outside of the OwnerId change. Instead, the new OwnerID is being inserted into the Case Team and is deleting any Case Team Members that were previously in the Case Team, that should remain in the team during the insert, are being deleted.
Please see below for the code:
Please see below for the code:
trigger CaseTeamMember_Owner_Update on Case (after update) { CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team']; Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById(); List<CaseTeamMember> members = new List<CaseTeamMember>(); for (Case c: Trigger.new) { List<CaseTeamMember> mem=[Select id from CaseTeamMember where CaseTeamMember.MemberId=:trigger.new[0].OwnerId AND CaseTeamMember.ParentId=:trigger.new[0].Id]; if(rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case') && mem.size()>0) { } else if(c.CreatedDate != System.now() && c.OwnerId != Trigger.oldMap.get(c.Id).OwnerId && rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case')) { members.add(new CaseTeamMember( ParentID = c.Id, MemberID = c.OwnerId, TeamRoleID = role.Id )); } } for (Case cas: Trigger.new) { List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId]; delete memdelete; } if (!members.isEmpty()) { insert members; } }Removing these line of codes:
for (Case cas: Trigger.new) { List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId]; delete memdelete; }I am fairly certain the following line is causing this issue, but I am not knowledge enough to fix the code:
insert members;Thoughts?
All Answers
Comment these lines putting them in between
/*
*/
like
Other than causing the delete these lines are not even following best practices for bulkified triggers .
Thanks
Shashikant
As per my initial comment: I have tried removing those lines of code, but entire Case Team is still being deleted upon insert of the new value. Any other thoughts?
Cheers,
Lindsey
I just tried in my org same code
1. With the code deleting - it delete the exiting case team member
2. With commenting the code deleting - it did not delete exiting case team member
Please make sure when you comment or remove code is saved to server. You could verify it by opening the trigger in separe window.
Let me know if you still face issue.
Thanks
Shashikant
Thank you for your advice on confirming that the code successfully saved on the server! Although I was able to click on "Quick Save" and have the button seemingly cycle through the saving action, the code did not appear to save the to server. Upon retest: yes, I can confirm that the existing Case Team records are now no longer deleting upon insert.
How would you recommend I update the code so that when the OwnerId is changed on the case, the old OwnerId value is removed from the Case Team?
Cheers,
Lindsey
In order to test the owner change use paste this to your trigger and save:
In order to test
1. Create a Case
2. Verify that there is a Case Team Member Added
3. Change the Owner to some other user
4. Verify that there is a Case Team Member for changed Owner and Existing is not deleted
Thanks
Shashikant
My goal is to have Case Team Member record that has a MemberId that matches the old OwnerId value be deleted upon the completion of this trigger. Any idea how I can code that in place of what I originally had (of which was not working)?
Cheers,
Lindsey
Now I understand that what you want s to delete the Case Team member on this case but not on the other cases.
TO achieve it replace the commented code with this code.
Let me know if you face issues with it.
Thanks
Shashikant