0

I would like to update the checkbox field 'New' to false when there's a new record inserted or updated when the colID is more than the previous colID.

For instance: My record as shown:

ProdID = 1 ColID = 5 New = True

ProdID = 2 ColID = 6 New = True

So basically after ProdID =2 is inserted, ProdID = 1 record checkbox field should be updated to false

I've tried the below Trigger:

Public Decimal colold;
Public Boolean newiold;

List<Product__c> productsToUpdate = new List<Product__c>{};
List<Product__c> prodList = [SELECT Name,Collection__c, New__c FROM Product__c where Id in : Trigger.new];

Id selid;


for(Product__c pold : Trigger.new){
     if (pold.New__c == true) {    
         selid = pold.id;
    }
 }

for(Product__c aa: prodList)
{
   if(aa.Id == selid && colold > aa.Collection__c)
   {
      aa.New__c=false;
      productsToUpdate.add(aa);
   }
}
update productsToUpdate;
1

Try following approach :

List<Product__c> productsToUpdate = new List<Product__c>{};
//Insert scenario
public void handleAfterInsert(){
    List<Product__c> prodList = [SELECT Name,Collection__c, New__c FROM Product__c];

    for(Product__c newproduct : Trigger.new){
         for( Product__c oldproduct : prodList)
         {
           if(newproduct.Collection__c > oldproduct.Collection__c)
           {
             newproduct.New__c = true;
             oldproduct.New__c = false;
             productsToUpdate.add(oldproduct);
           }
         }
     }

}

public void handleAfterUpdate(){
//Update scenario
//Exclude the current records and get all the previous ones
    List<Product__c> prodList = [SELECT id, Name,Collection__c, New__c FROM Product__c
                                  Where id not in : Trigger.new];

    for(Product__c newproduct : Trigger.new){
         for( Product__c oldproduct : prodList)
         {
           if(newproduct.Collection__c > oldproduct.Collection__c)
           {
             newproduct.New__c = true;
             oldproduct.New__c = false;
             productsToUpdate.add(oldproduct);
           }
         }
     }

}

update productsToUpdate;
0

If I understood correctly, you need to keep the latest record marked as 'new' and every other record with the new checkbox unchecked. It would be a good idea to first declare two lists: one for the objects that activate the trigger, and other for the objects that already exists in SF.

You would need to first check what is the object in the trigger with the closest date/time to now. You need to get that object's ID and use it later. Then, every other object in the trigger would have to get the checkbox set to false. After doing this, you need to check for records in SF that have the field equal to true and get their IDs. Finally, you have to set those to false, and set the first object's field (the one I reference in the first phrase in this paragraph) to true.

And then you want to insert the new objects in SF, and update the others. I believe there is no need to run the trigger after update since it will run only when new objects are inserted.

  • Issit possible for you to edit my code? I just want to know my mistake. – fb69 Mar 27 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.