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
viadeoviadeo 

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;
   }

Best Answer chosen by Admin (Salesforce Developers) 
viadeoviadeo

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

SeAlVaSeAlVa

Line 23, how do you expect 

 

   if(conlist.size()<0)

 to be true?

 

Regards

viadeoviadeo

yes. is it wrong ?

 

dmchengdmcheng

It could evaluate to true in the vicinity of the Large Hadron Collider.

viadeoviadeo

thanks  to make me smile but could you help me to solve my problem now ?

viadeoviadeo

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

SeAlVaSeAlVa

Try with 

if(conlist.size()==0)

instead of 

if(conlist.size()<0)

 

 

Anyway, you should bulkify it (you should not have any query inside loops).

 

You might want to change the line

if(c.status=='expiré')

 to

else if(c.status=='expiré')

 

 

Regards.

 

P.S. as conlist is a List, its size will never be lower than 0 [might be empty (== 0), or have something (> 0)]

if(conlist.size()<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.

viadeoviadeo

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;
   }

 

viadeoviadeo

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;
}

 
 
This was selected as the best answer