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
Victor ApolinarioVictor Apolinario 

Apex - Set Contact Status

Hi,

I´ll leave some pictures below of a process builder I created that I really need to change it into a Apex trigger. If someone knows this please give this hand, it will be a gigantic help.
User-added imageUser-added imageUser-added imageUser-added imageUser-added image
Best Answer chosen by Victor Apolinario
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

A small change in the trigger .
 
trigger rentContactTrigger on Rent__c (after insert, after update) {
    Rent__c oldrent = new Rent__c();
    Map<ID, Contact> parentobj = new Map<ID, Contact>(); 
      List<Id> listIds = new List<Id>();

    List<Contact> conlist= new List<Contact>();
    For(Rent__c re:Trigger.new){
        if(Trigger.isupdate)
         oldrent= Trigger.oldmap.get(re.id);
        
        If( (Trigger.isinsert ||(Trigger.isupdate )) && (re.Status__c=='Late' || re.Status__c=='Finished')){
           listIds.add(re.Contact__c); 
           
        }
    }
    
     parentobj = new Map<Id, Contact>([SELECT id, Status__c FROM Contact WHERE ID IN :listIds]);
    if(parentobj.size()>0){
  for (Rent__c ren: Trigger.new){
    Contact tobeupdate= parentobj.get(ren.Contact__c);
      
      if(ren.Status__c=='Late'){
          tobeupdate.Status__c='Defaulting';
          conlist.add(tobeupdate);
      }
      if(ren.Status__c=='Finished'){
          tobeupdate.Status__c='Normal ';
          conlist.add(tobeupdate);
      }
      
  }
    if(conlist.size()>0)
        update conlist;

}
}

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you explain this in words like what you are doing in first decision box and first update and same in the second one as the screenshot is not clear.
So I can share trigger logic for the same.

Thanks,
 
Victor ApolinarioVictor Apolinario
Yes, sure. 
So basically in the Rent object we´re checking if the Status equals the string 'Late'. If that is true, the Contact Status will change to Defaulting in the picklist.
If not, we´re going to check if the Rent Status equals the string 'Finished', and if that is true the Contact Status will change to Normal in the picklist.

I hope now it´s more clear to you.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Thanks for confirming on it. Can you confirm when is this process Builder Triggering. Only on create or only on update . can you confirm that as well.

Thanks,
Victor ApolinarioVictor Apolinario
Hey Sai,

I´m really not sure, but I believe it´s on update. 
Because in my mind the item will be rented and that should be the trigger to change the status. 

Does that make sense for you too?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try below triger logic on Rent object.
 
trigger rentContactTrigger on Rent__c (after insert, after update) {
    Rent__c oldrent = new Rent__c();
    Map<ID, Contact> parentobj = new Map<ID, Contact>(); 
      List<Id> listIds = new List<Id>();

    List<Contact> conlist= new List<Contact>();
    For(Rent__c re:Trigger.new){
        if(Trigger.isupdate)
         oldrent= Trigger.oldmap.get(re.id);
        
        If( (Trigger.isinsert ||(Trigger.isupdate && re.Status__c!=oldrent.Status__c )) && (re.Status__c=='Late' || re.Status__c=='Finished')){
           listIds.add(re.Contact__c); 
           
        }
    }
    
     parentobj = new Map<Id, Contact>([SELECT id, Status__c FROM Contact WHERE ID IN :listIds]);

  for (Rent__c ren: Trigger.new){
    Contact tobeupdate= parentobj.get(ren.Contact__c);
      
      if(ren.Status__c=='Late'){
          tobeupdate.Status__c='Defaulting';
          conlist.add(tobeupdate);
      }
      if(ren.Status__c=='Finished'){
          tobeupdate.Status__c='Normal ';
          conlist.add(tobeupdate);
      }
      
  }
    if(conlist.size()>0)
        update conlist;

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

A small change in the trigger .
 
trigger rentContactTrigger on Rent__c (after insert, after update) {
    Rent__c oldrent = new Rent__c();
    Map<ID, Contact> parentobj = new Map<ID, Contact>(); 
      List<Id> listIds = new List<Id>();

    List<Contact> conlist= new List<Contact>();
    For(Rent__c re:Trigger.new){
        if(Trigger.isupdate)
         oldrent= Trigger.oldmap.get(re.id);
        
        If( (Trigger.isinsert ||(Trigger.isupdate )) && (re.Status__c=='Late' || re.Status__c=='Finished')){
           listIds.add(re.Contact__c); 
           
        }
    }
    
     parentobj = new Map<Id, Contact>([SELECT id, Status__c FROM Contact WHERE ID IN :listIds]);
    if(parentobj.size()>0){
  for (Rent__c ren: Trigger.new){
    Contact tobeupdate= parentobj.get(ren.Contact__c);
      
      if(ren.Status__c=='Late'){
          tobeupdate.Status__c='Defaulting';
          conlist.add(tobeupdate);
      }
      if(ren.Status__c=='Finished'){
          tobeupdate.Status__c='Normal ';
          conlist.add(tobeupdate);
      }
      
  }
    if(conlist.size()>0)
        update conlist;

}
}

Thanks,
​​​​​​​
This was selected as the best answer