You need to sign in to do that
Don't have an account?
rashmi 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....................
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....................
Please check and let me knwo if it helps you..
Thanks,
Sandeep
All Answers
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
Please check and let me knwo if it helps you..
Thanks,
Sandeep
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.
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
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
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;
}
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.