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
sfdotcomsfdotcom 

Trigger to update status

Hi,

Could you please help me on changing the below trigger (which is written by Sandeep sankhla) , so that Plan__c status will be changed based on Account. That is if Account status is Active, then Plan__ c status should be Active, if Account status is InActive, then Plan__ c status should be InActive, if Account status is null, then Plan__ c status should be null.

Here
1. Account and Plan objects with many to many relationship and Accountplan as a Junction Object
2. Account obj with status field Active and inactive.
3. and Plan obj with status field Active and inactive

Trigger AccountTrigger on Account(After Update)
{
    set<Id> setAccIds = new set<Id>();
    list<Plan__c> lstPlanToUpdate = new list<Plan__C>();
    
    
    for(Id objAcc : trigger.newmap.Keyset())
    {
        if((trigger.newmap.get(objAcc).Status__c == 'InActive')  &&(trigger.newmap.get(objAcc).Status__c != trigger.oldMap.get(objAcc).Status__c))
        {
            setAccIds.add(objAcc);
        }
    }
    
    for(AccountPlan__c objAP : [Select Id, Plan__c, Account__c from AccountPlan__c where Account__c IN :setAccIds])
    {
            Plan__c objPlan = new Plan__c(Id = objAP.Plan__c);
            objPlan.Status__C = 'InActive';
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;

}

Thank You
Best Answer chosen by sfdotcom
Vishal Negandhi 16Vishal Negandhi 16
Trigger AccountTrigger on Account(After Update)
{
    set<Id> setAccIds = new set<Id>();
    list<Plan__c> lstPlanToUpdate = new list<Plan__C>();
    
    
    for(Id objAcc : trigger.newmap.Keyset())
    {
        if(trigger.newmap.get(objAcc).Status__c != trigger.oldMap.get(objAcc).Status__c)
        {
            setAccIds.add(objAcc);
        }
    }
    
    for(AccountPlan__c objAP : [Select Id, Plan__c, Account__c, Account__r.Status__c from AccountPlan__c where Account__c IN :setAccIds])
    {
            Plan__c objPlan = new Plan__c(Id = objAP.Plan__c);
            objPlan.Status__c = objAP.Account__r.Status__c;
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;

}

Earlier code only checked if the status was changed to Inactive, now we have removed that and every time status is changed for Account, all the related values are updated.

All Answers

Vishal Negandhi 16Vishal Negandhi 16
Trigger AccountTrigger on Account(After Update)
{
    set<Id> setAccIds = new set<Id>();
    list<Plan__c> lstPlanToUpdate = new list<Plan__C>();
    
    
    for(Id objAcc : trigger.newmap.Keyset())
    {
        if(trigger.newmap.get(objAcc).Status__c != trigger.oldMap.get(objAcc).Status__c)
        {
            setAccIds.add(objAcc);
        }
    }
    
    for(AccountPlan__c objAP : [Select Id, Plan__c, Account__c, Account__r.Status__c from AccountPlan__c where Account__c IN :setAccIds])
    {
            Plan__c objPlan = new Plan__c(Id = objAP.Plan__c);
            objPlan.Status__c = objAP.Account__r.Status__c;
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;

}

Earlier code only checked if the status was changed to Inactive, now we have removed that and every time status is changed for Account, all the related values are updated.
This was selected as the best answer
sfdotcomsfdotcom
Thank You Vishal.