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 update Parent picklist from Child picklist

Hi 

 

how fire a trigger only if a certain value is not present in map .to be clear status 'inactive' on account must be actived only if status on contract is 'expiré ' and also if no contract with status'activé' exist.

 

this is my trigger without the second condition

 

trigger workflow2 on Contract (before 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é'){
account ac=accMap.get(c.AccountId);
ac.A_I__c='inactive';
accList.add(ac);
}

//this trigger must be fire only if in contract Map ,value 'activé' isn't there 
}

update accList;


}

Best Answer chosen by Admin (Salesforce Developers) 
steve456steve456

Trigger AfterUpdateStatusAccount on Contract__c(after update){
    Set<Id> conId = new Set<Id>();
    List<Account> accList = new List<Accounyt>();

    for(Contract con : Trigger.new){
       if(con.Id != null && (Trigger.newMap.get(con.id).Status__c!= Trigger.oldMap.get(con.Id).Status__c) &&con.Status__c!= null)
       {
      conId.add(con.Id);
       }
     }

      List<Contract> conList = [Select Id,Status__c from Contract WHERE  Status__c='Expire'AND Id IN :conId ];
      List<Account> pd = new List<Account>();
      if(conList != null && conList.size()>0){
          pd = [Select Id,Status__c from Account Where Lookup__c =: conList[0].Lookup__c Limit 1];
         }

      for(Account p : pd){
         if(p != null){
           p.Status__c = 'InActive';
          accList.add(p);
            }
         }
     updateaccList;
}

All Answers

steve456steve456

Trigger AfterUpdateStatusAccount on Contract__c(after update){
    Set<Id> conId = new Set<Id>();
    List<Account> accList = new List<Accounyt>();

    for(Contract con : Trigger.new){
       if(con.Id != null && (Trigger.newMap.get(con.id).Status__c!= Trigger.oldMap.get(con.Id).Status__c) &&con.Status__c!= null)
       {
      conId.add(con.Id);
       }
     }

      List<Contract> conList = [Select Id,Status__c from Contract WHERE  Status__c='Expire'AND Id IN :conId ];
      List<Account> pd = new List<Account>();
      if(conList != null && conList.size()>0){
          pd = [Select Id,Status__c from Account Where Lookup__c =: conList[0].Lookup__c Limit 1];
         }

      for(Account p : pd){
         if(p != null){
           p.Status__c = 'InActive';
          accList.add(p);
            }
         }
     updateaccList;
}

This was selected as the best answer
viadeoviadeo

Hi

 

thanks for your solution but could you explain it to me ?