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
tulasiram chtulasiram ch 

How to perform updation like below scenario

  1. Account ==== Contact   on contact Status__C field is there (Total 10 records)
If one record status is changed to Approved then ‘Remaining 9 records status__c would be changed to Declined ’ . But first Record Status__c  would be Same(Approved).
How can we achieve this through?
1.trigger
2. Process Builder
  1. Account ==== Contact   on contact Status__C field is there (Total 10 records)
Best Answer chosen by tulasiram ch
AvaneeshAvaneesh
Hi

finally,
I solved your trigger problem and exception and complete requirement you just need to copy paste the code and your trigger is ready 
trigger TriggerForStatusOfcontact on Contact (after insert,after update) {
    Set<id> allNewContact = new Set<id>();
    Set<id> allRelatedAccount = new Set<id>();
    List<Contact> allContactBulk = new List<Contact>();
     for(contact con :trigger.new)
     {
         if(con.status__c !=null && String.valueOf(con.status__c).equals('Approved'))
         {
            allNewContact.add(con.id);  
            allRelatedAccount.add(con.AccountId);
         }
     }
      
      for(contact con:[select id,accountid,status__c from contact where accountid IN:allRelatedAccount])
      {
          if(!allNewContact.contains(con.Id))
          {
             contact cc = new contact();
              cc.Id = con.Id;
              cc.status__c = 'Declined';
              allContactBulk.add(cc);
          }
      }
    
    if(allContactBulk.size()>0)
    {
        update allContactBulk;
    }      
    
}

Please don't forget to mark as best answer ....................................

Thank you 
Avaneesh Singh

All Answers

AvaneeshAvaneesh
Hi 
for this requirement, you can use a trigger.
I didn't get your last line " But first Record Status__c  would be Same(Approved). "
what you are trying to say.... give an example

Thank you 
Avaneesh Singh
tulasiram chtulasiram ch
Thank you Avinash..." But first Record Status__c  would be Same(Approved). " Means 10 records are there
Suppose First record Status__c changed to Approved , then Only Remaining 9 records Status__c will be changed to Declined not 10 records.
Record 1 : Status__c = approved
Record 2-10 : Status__c = Declined.

How can we achieve through Trigger. Can you give me a code please.
AvaneeshAvaneesh
Hii
okay i am writing a trigger for you just give me few minutes 

Thank you
AvaneeshAvaneesh
Hi
I did your requirement and this trigger is working fine in my org 
here you need a picklist in contact name status__c and value of picklist are Approved and 
Declined
make sure spelling are correct 
trigger TriggerForStatusOfcontact on Contact (after insert,after update) {
    Set<id> allNewContact = new Set<id>();
    Set<id> allRelatedAccount = new Set<id>();
    List<Contact> allContactBulk = new List<Contact>();
     for(contact con :trigger.new)
     {
         if(con.Id !=NULL && String.valueOf(con.status__c).equals('Approved'))
         {
            allNewContact.add(con.id);  
            allRelatedAccount.add(con.AccountId);
         }
     }
      
      for(contact con:[select id,accountid,status__c from contact where status__c='Approved' AND accountid IN:allRelatedAccount])
      {
          if(!allNewContact.contains(con.Id))
          {
             contact cc = new contact();
              cc.Id = con.Id;
              cc.status__c = 'Declined';
              allContactBulk.add(cc);
          }
      }
    
    if(allContactBulk.size()>0)
    {
        update allContactBulk;
    }      
    
}

Cheers send me two beer's and don't forget to mark as best answer ----------------


Thank you 
Avaneesh Singh
AvaneeshAvaneesh
Hi
Let me know if still you are facing any problem.you just have to copy and paste the code 

Thank you
Avaneesh Singh


 
tulasiram chtulasiram ch
I am getting following error , its not taking new inserting records with out status field value. If i am giving any value in the status field its fine working, but that is not my scenario.
1.If i inserted 9 records with Status__c == null, when i tried to insert 10th record with Status__c==Approved then only remaining 9 records   Status__c==Declined
2. i inserted 10 records, then i updated one record from  Status__c == Approved then only remaining 9 records status__c would be Declined.
Error: Invalid Data. 

Review all error messages below to correct your data.
Apex trigger updateApprovedandDeclined caused an unexpected exception, contact your administrator: updateApprovedandDeclined: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateApprovedandDeclined: line 8, column 1
Mustafa JhabuawalaMustafa Jhabuawala
The code provided by Avaneesh seems correct.

The error you are facing related to null pointer is, you are trying to perform dot operation on a null object. Can you share your code so that it would be easier to point out the issue.

 
AvaneeshAvaneesh
Hi

finally,
I solved your trigger problem and exception and complete requirement you just need to copy paste the code and your trigger is ready 
trigger TriggerForStatusOfcontact on Contact (after insert,after update) {
    Set<id> allNewContact = new Set<id>();
    Set<id> allRelatedAccount = new Set<id>();
    List<Contact> allContactBulk = new List<Contact>();
     for(contact con :trigger.new)
     {
         if(con.status__c !=null && String.valueOf(con.status__c).equals('Approved'))
         {
            allNewContact.add(con.id);  
            allRelatedAccount.add(con.AccountId);
         }
     }
      
      for(contact con:[select id,accountid,status__c from contact where accountid IN:allRelatedAccount])
      {
          if(!allNewContact.contains(con.Id))
          {
             contact cc = new contact();
              cc.Id = con.Id;
              cc.status__c = 'Declined';
              allContactBulk.add(cc);
          }
      }
    
    if(allContactBulk.size()>0)
    {
        update allContactBulk;
    }      
    
}

Please don't forget to mark as best answer ....................................

Thank you 
Avaneesh Singh
This was selected as the best answer
Sukanya BanekarSukanya Banekar
Hi,
Do changes in Avaneesh code as below from line 14 to 21
 
​for(contact con:[select id,accountid,status__c from contact where status__c='Approved' AND accountid IN:allRelatedAccount])
      {
          if(!allNewContact.contains(con.Id))
          {
              con.status__c = 'Declined';
              allContactBulk.add(con);
          }
      }

You dont need to create new Contact objec.

Thanks,
Sukanya Banekar