0

I am using before update trigger

I want to uncheck checkbox for remaining record in same object if one record checkbox is checked.

trigger trigger_ManagePrimaryMember on Team_Members__c (before update) {

    Set<Id> currentprimarymemberid = new Set<Id>();
    Set<Id> currentprimarymembrteamid = new Set<Id>();
    List<Team_Members__c> teamid =new List<Team_Members__c>();
    List<Team_Members__c> Updateprimarymember = new List<Team_Members__c>();

    for(Team_Members__c primarymember : trigger.new)
    { 
       currentprimarymemberid.add(primarymember.Id);
    }

    teamid= [select Id,Team__c from Team_Members__c where Id in :currentprimarymemberid];

    for(Team_Members__c teammembers :teamid)
    {
        currentprimarymembrteamid.add(teammembers.Team__c);
    }

    for(Team_Members__c teammembers :[select id,Name from Team_Members__c
            where Team__c in :currentprimarymembrteamid])
    {
        if(!currentprimarymemberid.contains(teammembers.id))
        {
            // making the checkbox to false for other member in the team
            teammembers.check_box__c= false;
        }
    }
}

Thanks in advance

3
  • What is not working in your solution? How can we help you?
    – Mark Pond
    Commented Apr 20, 2014 at 17:22
  • What do you mean by remaining record?
    – greenstork
    Commented Apr 20, 2014 at 18:49
  • @greenstork consider I have 3 records in my custom object when i check the check box for one record the remaining record checkbox should get unchecked automatically..
    – Mangoski
    Commented Apr 21, 2014 at 7:04

1 Answer 1

1

I think you want to do is: lets say you have 3 records A, B & C

When user update record A with check box checked then you want to uncheck check boxes on B & C.

If that is right then you should not try to do this in triggers.

Here is how i would address this:

When trigger is fired on A i will check if check box is check then log a message in other object noting the id of A and time stamp.

In scheduled Batch process the log messages.

5
  • Yes you are correct then how can i achieve this without using triggers
    – Mangoski
    Commented Apr 21, 2014 at 7:02
  • As i already told you "When trigger is fired on A i will check if check box is check then log a message in other object noting the id of A and time stamp. In scheduled Batch process the log messages." Commented Apr 21, 2014 at 9:01
  • Over-complicated. Just use an after- trigger to query for all other child records and update their checkbox fields to false. Commented Apr 21, 2014 at 20:52
  • @DavidSchach i agree that my solution is more complicated that yours but the there are certain advantages of my approach. 1) It can handle bulk data 2) it can handle conflicts properly. You can imagine what will happen if you update 10K records using Data loader or API in your approach. If the number of records to be updated in Team_Members__c are low then your solution will work fine but not otherwise. Commented Apr 28, 2014 at 5:41
  • 1
    Completely agree, I'd also not do that in a trigger. Though it may be good to know the real use case, as this is probably something better tacked in an alternative functional design. Commented Jun 20, 2014 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .