0
trigger CM_season_Indicator on CM_Season__c (before insert, before update) {
List<CM_Season__c> SeasonObjList = new List<CM_Season__c>();  
Set<Id> SeasonObjIds = new Set<Id>();
for(CM_Season__c seasonObj : trigger.new){
   if(seasonObj.CM_is_current_season__c == true){
     SeasonObjIds.add(seasonObj.id);
    } break;
}

SeasonObjList = [SELECT id,CM_is_current_season__c FROM CM_Season__c  WHERE CM_is_current_season__c=true  AND Id NOT IN:SeasonObjIds];
for(CM_Season__c seasonObj:SeasonObjList)
    seasonObj.CM_is_current_season__c=false;
update SeasonObjList;

}

3
  • 1
    What is the problem you are getting?
    – tugce
    Apr 15, 2019 at 6:16
  • Do you want all other CM_Season__c's CM_is_current_season__c to be marked as false when there is at least one record which is marked as true ? In that case, you will have only one such record with field marked as true in your entire database. Apr 15, 2019 at 6:21
  • yes vijay ur understanding is true only one record in season object should have CM_is_current_season__c true if any other is made true then already existing true should be made false i'm facing such error: 11:05:39:013 FATAL_ERROR System.DmlException: Update failed. First exception on row 0 with id a0F6E00000B5Mb8UAF; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0F6E00000B5Mb8) is currently in trigger CM_season_Indicator, therefore it cannot recursively update itself: []
    – priya n
    Apr 15, 2019 at 6:30

2 Answers 2

0

So you are getting that error due to this following problem

  • You are updating same records again and again. (Explanation in below points).
  • Let's say you are updating/inserting a record with CM_is_current_season__c as true, then you will get the value in SeasonObjIds in update context not in insert context as Id's are not generated in insert context.
  • Your SOQL will give you results where the value of CM_is_current_season__c is true and Id not in SeasonObjIds and then you update the returned results.
  • This will trigger the before update trigger again, and now, the new value of CM_is_current_season__c will be false and hence it won't enter your If condition.
  • Hence SeasonObjIds will be null, now when your SOQL is fired, you will get the records where CM_is_current_season__c as true and Id not in SeasonObjIds which will be null.
  • This result of SOQL will again contain the same records as the value of CM_is_current_season__c will be true although you updated it to false in your last update, but as the records are still in transaction, in database it will still be true. Hence that records will again be queried and your code will again try to update the same records which will lead to the error you are getting.

Following trigger should do the trick for you

trigger CM_season_Indicator on CM_Season__c (before insert, before update) {
    List<CM_Season__c> SeasonObjList = new List<CM_Season__c>();  
    Set<Id> SeasonObjIds = new Set<Id>();
    for(CM_Season__c seasonObj : trigger.new){
        if(Trigger.isUpdate && Trigger.IsBefore){
            if(seasonObj.CM_is_current_season__c == true ||
                (Trigger.oldMap.get(seasonObj.Id).CM_is_current_season__c== true && seasonObj.CM_is_current_season__c == false)){
                SeasonObjIds.add(seasonObj.id);
            }
        }
    }

    SeasonObjList = [SELECT id,CM_is_current_season__c FROM CM_Season__c  WHERE CM_is_current_season__c=true  AND Id NOT IN:SeasonObjIds];
    for(CM_Season__c seasonObj:SeasonObjList)
        seasonObj.CM_is_current_season__c=false;
    update SeasonObjList;
}

Note :

  • There is no need to add a break in your for loop.
  • You will need to handle one more scenario when multiple records with CM_is_current_season__c as true are updated or inserted.
  • ID's are not generated in before insert context, hence add condition to add the ID only when it is in update context.
0

I assume your requirement is to make the checkbox inactive for all the records, whenever there is at-least a single record with checkbox as active, during insert/update. You can remove- CM_is_current_season__c=true from the where condition. Also you don't need a dml statement in the before triggers (removed Update SeasonObjList) Since record is still not saved into database, so you need a dml statement specifically to update a record

Try below code works -

trigger CM_season_Indicator on CM_Season__c (before insert, before update) {

List SeasonObjList = new List();
Set SeasonObjIds = new Set();

for(CM_Season__c seasonObj : trigger.new){

if(seasonObj.CM_is_current_season__c == true){ SeasonObjIds.add(seasonObj.id); }

break;

}

SeasonObjList = [SELECT id,CM_is_current_season__c FROM CM_Season__c WHERE Id NOT IN:SeasonObjIds];

for(CM_Season__c seasonObj:SeasonObjList)

{

seasonObj.CM_is_current_season__c=false;

}

// update SeasonObjList;

1
  • The above code is failing when i'm checking other record checkbox as well without making existing record check box to false both are true.....
    – priya n
    Apr 15, 2019 at 6:53

You must log in to answer this question.

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