You need to sign in to do that
Don't have an account?
trigger to change values field on parent when field on child change
Hi ,
I'm triying to develop a trigger which will change custom field on account when contract's status will change
1/account'scustom field take 'active' value when contract's status take 'activé' value .
2/account's custom field take 'inactive 'value when contract's status take 'expiré ' value but only if there is no contract link to this account with 'activé' status .
i have wrote my code step 1 is ok but impossible to have step 2
thanks for your suggestions
trigger workflow2 on Contract (After update) {
List<Account>accList=new List <Account>();
set<id>accIds = new Set<id>();
for(Contract c:Trigger.new)
{
accIds.add(c.AccountId);
}
Map<id,Account>accMap=new Map<id,Account>([Select(select id,status from contracts)from Account Where id in :accIds]);
//--------------------------------------------------------------------------------------------------//
for(Contract c:Trigger.new)
{
if(c.status=='activé')
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='active';
accList.add(ac);
}
if(c.status=='expiré')
{
List<Account>conlist=new List<account>([Select(select id from contracts where Status='activé')from Account where id in :accIds]);
if(conlist.size()<0)
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='inactive';
accList.add(ac);
}
else if (conlist.size()>0)
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='active';
accList.add(ac);
}
}
}
//-----------------------------------------------------------------------------------------------------//
update accList;
}
Hi all
finally it's working !!
thanks SeAI VA for your help !! :)
trigger workflow2 on Contract (after update) {
List<Account>accList=new List <Account>();
set<id>accIds = new Set<id>();
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
for(contract c:trigger.new)
{
accIds.add(c.AccountId);
}
Map<id,Account>accMap=new Map<id,Account>([Select(select status from contrats__r)from account Where id in :accIds]);
List<contract> myContracts = [select AccountId from contract where AccountId in :accIds and status='activé']; // select all the contracts with the status tu activé
Set<ID> myAccountsIDs = new Set<ID>();
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
for (contract c : myContracts)
{
myAccountsIDs.add(c.AccountId);
}
for (contract c :trigger.new)
{
if(c.status=='activé')
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='active';
accList.add(ac);
}
if(c.status=='expiré'&& myAccountsIDs.contains(c.accountid) == false)
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='inactive';
accList.add(ac);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
update accList;
}
All Answers
Line 23, how do you expect
to be true?
Regards
yes. is it wrong ?
It could evaluate to true in the vicinity of the Large Hadron Collider.
thanks to make me smile but could you help me to solve my problem now ?
in fact i want to be able to evaluate list of account's contracts after changes in on of them (activated to expired ) .i try several method and finally try 'size'.
i'm a newbie as you can see and this is first time i have this kind of problem .
thanks to help me improve myself in apex development
Try with
instead of
Anyway, you should bulkify it (you should not have any query inside loops).
You might want to change the line
to
Regards.
P.S. as conlist is a List, its size will never be lower than 0 [might be empty (== 0), or have something (> 0)]
P.S.2 This afternoon, if I had time, I will try to post a bulkified version, but first confirm if it works or not.
thanks for your answer but it doesn't work well the is that account field still 'active ' in any case
this my new code
trigger workflow2 on Contract (After update) {
List<Account>accList=new List <Account>();
set<id>accIds = new Set<id>();
for(Contract c:Trigger.new)
{
accIds.add(c.AccountId);
}
Map<id,Account>accMap=new Map<id,Account>([Select(select id,status from contracts)from Account Where id in :accIds]);
//--------------------------------------------------------------------------------------------------//
for(Contract c:Trigger.new)
{
if(c.status=='activé')
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='active';
accList.add(ac);
}
else if(c.status=='expiré')
{
List<Account>conlist=new List<account>([Select(select id from contracts where Status='activé')from Account where id in :accIds]);
if(conlist.size()==0)
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='inactive';
accList.add(ac);
}
}
}
//-----------------------------------------------------------------------------------------------------//
update accList;
}
Hi all
finally it's working !!
thanks SeAI VA for your help !! :)
trigger workflow2 on Contract (after update) {
List<Account>accList=new List <Account>();
set<id>accIds = new Set<id>();
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
for(contract c:trigger.new)
{
accIds.add(c.AccountId);
}
Map<id,Account>accMap=new Map<id,Account>([Select(select status from contrats__r)from account Where id in :accIds]);
List<contract> myContracts = [select AccountId from contract where AccountId in :accIds and status='activé']; // select all the contracts with the status tu activé
Set<ID> myAccountsIDs = new Set<ID>();
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
for (contract c : myContracts)
{
myAccountsIDs.add(c.AccountId);
}
for (contract c :trigger.new)
{
if(c.status=='activé')
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='active';
accList.add(ac);
}
if(c.status=='expiré'&& myAccountsIDs.contains(c.accountid) == false)
{
account ac=accMap.get(c.AccountId);
ac.A_I__c='inactive';
accList.add(ac);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
update accList;
}