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 - Change Item disponibility when rented

Hi, 

I´m going to leave below some pictures of a process builder I created that I really need to change it into a Apex trigger, if someone knows how to please give me this help. Thank you.

User-added imageUser-added imageUser-added imageUser-added imageUser-added image
Best Answer chosen by Victor Apolinario
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try the below trigger on Rent object.
 
trigger rentInventoryTrigger on Rent__c (after update) {
 Rent__c oldrent = new Rent__c();
    Map<ID, Inventory_Item__c> parentobj = new Map<ID, Inventory_Item__c>(); 
      List<Id> listIds = new List<Id>();

    List<Inventory_Item__c> invlist= new List<Inventory_Item__c>();
    For(Rent__c re:Trigger.new){
        if(Trigger.isupdate)
         oldrent= Trigger.oldmap.get(re.id);
        
        If(  (re.Status__c!='finished' ||( re.Status__c!='late' && re.status__c!='In Progress'))){
           listIds.add(re.Inventory_Item__c); 
           
        }
    }
    
     parentobj = new Map<Id, Inventory_Item__c>([SELECT id, Status__c FROM Inventory_Item__c WHERE ID IN :listIds]);
    if(parentobj.size()>0){
  for (Rent__c ren: Trigger.new){
    Inventory_Item__c tobeupdate= parentobj.get(ren.Inventory_Item__c);
      
      if(ren.Status__c!='finished'){
          tobeupdate.Status__c='rented';
          invlist.add(tobeupdate);
      }
      else if(ren.Status__c!='In progress' && ren.Status__c!='Late'  ){
          tobeupdate.Status__c='available';
          invlist.add(tobeupdate);
      }
      
  }
    if(invlist.size()>0)
        update invlist;
    }
}

Let me know if you face any issues.

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

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

This seems to be duplicate for the below question. 

https://developer.salesforce.com/forums?dc=Apex_Code_Development#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=ALLQUESTIONS&id=9062I000000R3zcQAC (https://developer.salesforce.com/forums?dc=Apex_Code_Development#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=ALLQUESTIONS&id=9062I000000R3zcQAC)

I have taken up the above question. Can you close this question by marking this as best answer so it avoids duplicate efforts by others.

Thanks,
 
Victor ApolinarioVictor Apolinario
Actually no. 
They are different. The other one we needed to change the Status of the Contact based on the Rent Status. 

In this one we´re checking the Rent Status to see if it does not equal the string 'Finished',  we change the Inventory Item to the Rented Status in the picklist.
If not, we check if the Rent Status does not equal to the strings 'Late' or 'In Progress', and if that is true the Inventory Item Status should change to Available in the picklist.

Is that clearer now? 
Hope you can help.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try the below trigger on Rent object.
 
trigger rentInventoryTrigger on Rent__c (after update) {
 Rent__c oldrent = new Rent__c();
    Map<ID, Inventory_Item__c> parentobj = new Map<ID, Inventory_Item__c>(); 
      List<Id> listIds = new List<Id>();

    List<Inventory_Item__c> invlist= new List<Inventory_Item__c>();
    For(Rent__c re:Trigger.new){
        if(Trigger.isupdate)
         oldrent= Trigger.oldmap.get(re.id);
        
        If(  (re.Status__c!='finished' ||( re.Status__c!='late' && re.status__c!='In Progress'))){
           listIds.add(re.Inventory_Item__c); 
           
        }
    }
    
     parentobj = new Map<Id, Inventory_Item__c>([SELECT id, Status__c FROM Inventory_Item__c WHERE ID IN :listIds]);
    if(parentobj.size()>0){
  for (Rent__c ren: Trigger.new){
    Inventory_Item__c tobeupdate= parentobj.get(ren.Inventory_Item__c);
      
      if(ren.Status__c!='finished'){
          tobeupdate.Status__c='rented';
          invlist.add(tobeupdate);
      }
      else if(ren.Status__c!='In progress' && ren.Status__c!='Late'  ){
          tobeupdate.Status__c='available';
          invlist.add(tobeupdate);
      }
      
  }
    if(invlist.size()>0)
        update invlist;
    }
}

Let me know if you face any issues.

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

Thanks,
This was selected as the best answer
Victor ApolinarioVictor Apolinario
Thanks for your help Sai,

In this one I just had to add a after insert in the first line and it worked perfecty.