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 - Can´t rent a Rented item

Hello

I have a validation rule that prevents a Inventory Item with the Rented status, to be rented by someone else, and now I need to change that to a Apex Trigger, can you help me with that?
The object of this validation rule is Rent__c.User-added image
If you need any other information, feel free to ask. 
​​​​​​​Thank you
Best Answer chosen by Victor Apolinario
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try the  below apex trigger on Rent object.
 
trigger rentInvValid on Rent__c (before insert) {
 Map<Id,Inventory_Item__c> inventoryid= new Map<Id,Inventory_Item__c>();
    set<Id> inventoryids= new Set<Id>();
    For(Rent__c ren: Trigger.new){
inventoryids.add(ren.Inventory_Item__c);
    }
    
    if(inventoryids.size()>0){
        inventoryid=new Map<Id, Inventory_Item__c>([SELECT id, Status__c FROM Inventory_Item__c WHERE ID IN :inventoryids]);
    }
    if(inventoryid.size()>0){
        
    
     for (Rent__c ren: Trigger.new){
    Inventory_Item__c tobeupdate= inventoryid.get(ren.Inventory_Item__c);
      
         if(tobeupdate.Status__c=='Rented'){
             ren.adderror('Inventory status cannot be Rented');
         }
         
     }
    }
}

Let me know if you face any issues.

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

Thanks,

All Answers

Gian Piere VallejosGian Piere Vallejos
Not sure if I'm following. Do you mean that you need to replace this valudation rule with an Apex Trigger?
Victor ApolinarioVictor Apolinario

Exactly. 

I need to do the exator same thing this validation rule does, but now with a Apex Trigger. 

Sai PraveenSai Praveen (Salesforce Developers) 
HI Victor,

Is this similar to that if new rent it getting created and iinventory Itsm status wirh Rented should not be able to save in new record?

Thanks,
 
Victor ApolinarioVictor Apolinario
Hi Sai, 

In this one is basically checking if the Inventory Item status is 'Rented'. If yes, when a new rent is being created and someone try to rent this item they won´t be able to (because it´s already rented).

I think this way is more clear for you.
Thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try the  below apex trigger on Rent object.
 
trigger rentInvValid on Rent__c (before insert) {
 Map<Id,Inventory_Item__c> inventoryid= new Map<Id,Inventory_Item__c>();
    set<Id> inventoryids= new Set<Id>();
    For(Rent__c ren: Trigger.new){
inventoryids.add(ren.Inventory_Item__c);
    }
    
    if(inventoryids.size()>0){
        inventoryid=new Map<Id, Inventory_Item__c>([SELECT id, Status__c FROM Inventory_Item__c WHERE ID IN :inventoryids]);
    }
    if(inventoryid.size()>0){
        
    
     for (Rent__c ren: Trigger.new){
    Inventory_Item__c tobeupdate= inventoryid.get(ren.Inventory_Item__c);
      
         if(tobeupdate.Status__c=='Rented'){
             ren.adderror('Inventory status cannot be Rented');
         }
         
     }
    }
}

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