function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RitchiebRitchieb 

Setting child custom objects to inactive

Hi All,

 

I have made a custom object called RAG_Status__c as a child of the Master object Acctounts, the relationship is called Account__C.

 

My custom object has the following fields, Active__c (checkbox), Effective_Date__c (formula - Date/Time with created date in as the formula).

 

I wish to flag only the most recent child of an Account as active and flag all other child objects as inactive. I found a post here that seems to do what I need, but I have not been able to get it to work.

 

The post is here: http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-Update-Status/td-p/264273/highlight/true

 

Can anyone tell me what I am doing wrong? This saves and is active but doesnt amend the records as expected?

 

My code i have amended is here; 

 

trigger RAGinactiveActive on RAG_Status__C(after insert, after update) {

    if(!Trigger.isExecuting){ //check that the trigger isnt already executing
    

Set<Id> emtoupdate = new Set<Id> ();

if (Trigger.isInsert || Trigger.isUpdate ) {
for (RAG_Status__C c: Trigger.New) {
emtoupdate.add(c.Account__c);
}
}

if (Trigger.isDelete) {
for (RAG_Status__C c: Trigger.Old) {
emtoupdate.add(c.Account__c);
}
}

List<RAG_Status__C> ctxsToUpdate = new List<RAG_Status__C>{};

boolean firstRecord = true;

for(RAG_Status__C ctx : [Select Id, Active__c, Effective_Date__c from RAG_Status__C where Account__c =: emtoupdate ORDER BY Effective_Date__c DESC])
{
if(firstRecord)
{
ctx.Active__c = True;
firstRecord = false;
}
else
ctx.Active__c = False;
 
ctxsToUpdate.add(ctx);
 
}
if(ctxsToUpdate != null && !ctxsToUpdate.IsEmpty())
Database.update(ctxsToUpdate);
 
}
}

 

 

 

 

 

RitchiebRitchieb

Hi All,

 

Anyone got any ideas on this?

RitchiebRitchieb

Can anyone help on this? I'm still unable to get it working. Would be much appreciated!