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 - Block Contact Defaulting to rent Items.

Hi guys, 

I created a validation rule a while ago to prevent a contact with the "Defaulting" Status to rent a item. 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
Thank you
Best Answer chosen by Victor Apolinario
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Victor,

Can you try the below trigger logic.
 
trigger rentContValid on Rent__c (before insert) {
   Map<Id,Contact> mapcon= new Map<Id,Contact>();
    set<Id> Contactid= new Set<Id>();
    For(Rent__c ren: Trigger.new){
Contactid.add(ren.Contact__c);
    }
    
    if(Contactid.size()>0){
        mapcon=new Map<Id, Contact>([SELECT id, Status__c FROM Contact WHERE ID IN :Contactid]);
    }
    if(mapcon.size()>0){
        
    
     for (Rent__c ren: Trigger.new){
    Contact tobeupdate= mapcon.get(ren.Contact__c);
      
         if(tobeupdate.Status__c=='Defaulting'){
             ren.adderror('contact status cannot be Defauting');
         }
         
     }
    }
}

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,

Do you mean that when creating a new rent record you should not be able to select contact which status is Defaulting? Do you need this validation via Trigger?

Thanks,
 
Victor ApolinarioVictor Apolinario
Hey Sai,

Basically yes. What we need to do here is when a new rent is being created, I will select a contact that is renting the item and if the Status of the contact that is trying to rent the item is 'Defaulting' then he won´t be able to rent, only if his status is 'Normal'. 
So I can´t finish creating a new rent if the contact I selected is 'Defaulting'.
I made a validation rule for that but now I need a Apex Trigger to do that.

I hope this made it more clear for you.
If you need to check the name of any field or object just let me know.

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Thanks for confirming the requirement. I will share the apex trigger code for the same in some time.

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

Can you try the below trigger logic.
 
trigger rentContValid on Rent__c (before insert) {
   Map<Id,Contact> mapcon= new Map<Id,Contact>();
    set<Id> Contactid= new Set<Id>();
    For(Rent__c ren: Trigger.new){
Contactid.add(ren.Contact__c);
    }
    
    if(Contactid.size()>0){
        mapcon=new Map<Id, Contact>([SELECT id, Status__c FROM Contact WHERE ID IN :Contactid]);
    }
    if(mapcon.size()>0){
        
    
     for (Rent__c ren: Trigger.new){
    Contact tobeupdate= mapcon.get(ren.Contact__c);
      
         if(tobeupdate.Status__c=='Defaulting'){
             ren.adderror('contact status cannot be Defauting');
         }
         
     }
    }
}

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