+ Start a Discussion
rashmi salesforcerashmi salesforce 

Many to Many relationship trigger

How can i achive the following req:

i have Account and Plan objects with mamy to many relationship and Accountplan as a Junction Object

and  Account obj with status field Active and inactive
and Plan obj with status field Active and inactive

If Account Status field is inactive then change all Account related plans status to inactive only if plan related all accounts are inActive.

.......... Could you Please help me out in this req.

Could you please give me a snopshot of code here...


Thanks in Advance....................
Best Answer chosen by rashmi salesforce
sandeep sankhlasandeep sankhla
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;

}
Please refer the above code for same..also correct and provide all API names based on you robjects...it will work fro you..

Please check and let me knwo if it helps you..

Thanks,
Sandeep

All Answers

sandeep sankhlasandeep sankhla
Hi,

You can write trigger on Account and then when account status changess..you can collect all account Id and then you can query on junction object where account In that collceted collection...you will get all plan ids from there and then you can update those plan ids...
please check adn let me know if it helps or you need more details..

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
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;

}
Please refer the above code for same..also correct and provide all API names based on you robjects...it will work fro you..

Please check and let me knwo if it helps you..

Thanks,
Sandeep
This was selected as the best answer
rashmi salesforcerashmi salesforce
Thank you very much sandeep.... but i wanted to know one thing 

every thing completed but may i know the logic for underline one

If Account Status field is inactive then change all Account related plans status to inactive only if plan related all accounts are inActive.
sandeep sankhlasandeep sankhla
Hi Rashmi,

First we are collecting all the accounts where status is inactive...

then we are taking all the junction record for that record where that account is used..

then we are collecting all plan related to those junction object...

then we are updating those plan record

because we will get plan records only where account is inactive...
let me knwo if this helps you or you have any doubts..

Did you check this code by running and testing ?

thanks,
Sandeep
rashmi salesforcerashmi salesforce
Thank you very much sandeep
sfdotcomsfdotcom
Hi Sandeep,

Could you please change the above trigger, so that Plan 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.

I tried below trigger. It's working fine one time update. If we update the same Account for multiple times, then it is throwing error as 'Duplicate Id'.

Trigger AccountTrigger1 on Account(After Update, after insert)
{
    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))
         if((trigger.newmap.get(objAcc).Status__c == 'InActive') )
        {
            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;

    for(Id objAcc : trigger.newmap.Keyset())
    {
   
         if((trigger.newmap.get(objAcc).Status__c == 'Active') )
        {
            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 = 'Active';
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;


    for(Id objAcc : trigger.newmap.Keyset())
    {
   
         if((trigger.newmap.get(objAcc).Status__c == '') || (trigger.newmap.get(objAcc).Status__c == null) || (trigger.newmap.get(objAcc).Status__c == '--None--') )
        {
            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 = null;
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;
    
}

Thank You
sfdotcomsfdotcom
I think we do not need to write the above complex code, we can simply map the Account Status with Plan. But i am unable to map the Account status with Plan__c status.

Trigger AccountTrigger1 on Account(After Update, after insert)
{
    set<Id> setAccIds = new set<Id>();
    list<Plan__c> lstPlanToUpdate = new list<Plan__C>();
    
    
    for(Id objAcc : trigger.newmap.Keyset())
    {
   
            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';  Here the problem is occurring
         
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;
}
sfdotcomsfdotcom
I think we do not need to write the above complex code, we can simply map the Account Status with Plan. But i am unable to map the Account status with Plan__c status.

Trigger AccountTrigger1 on Account(After Update, after insert)
{
    set<Id> setAccIds = new set<Id>();
    list<Plan__c> lstPlanToUpdate = new list<Plan__C>();
    
    
    for(Id objAcc : trigger.newmap.Keyset())
    {
   
            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';  Here the problem is occurring
         
            lstPlanToUpdate.add(objPlan);
    }
    
    update lstPlanToUpdate;
}

Please change the above code.