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
Venkat adityaVenkat aditya 

when lookup values doesn't change means error should display using trigger

Hi, 

I have 2 Lookup fields in object. 

I have selected values for 2 lookup fields, and saved. 

Now if i want to edit that record, at that time if i am entering same look up values(not changing lookup values) means it should display an error that "Same set of values are Entered, please change the lookup values" 

I am thinking that we can done this through trigger. 
Can any one help with this scenario.

Thanks in advance.
 
Best Answer chosen by Venkat aditya
rajat Maheshwari 6rajat Maheshwari 6
Hi Venkat,

Below is code snippet, please use this : - 
trigger childObjectTrigger on childObject (before update)
 {
    for(childObject chld_Obj : Trigger.new)
       {
          if(chld_Obj.ParentLookupField==Trigger.oldMap.get(chld_Obj.id).ParentLookupField && chld_Obj.ParentLookupField1==Trigger.oldMap.get(chld_Obj.id).ParentLookupField1)
               
{
      chld_Obj.addError('Same set of values are Entered, please change the lookup values');
}

}
}
Thanks
 

All Answers

Hemant_JainHemant_Jain
You can do it by simple validation rule, change the below formula as per your requirement:
 
AND(NOT(ISCHANGED(Lookup_Field1__c)) ,NOT(ISCHANGED(Lookup_Field2__c)))


 
rajat Maheshwari 6rajat Maheshwari 6

Hi Venkat,

Sorry for late response, 

At your requirement, Trigger logic would give result :  - 

1. Whenever child object is updated and its lookupfield old values are equal to lookupfield new values, then Trigger will fire to generate an validation error : - Same set of values are Entered, please change the lookup values

2. Suppose, you edit the child Object and doesn't touch lookup field value, Trigger will again generate error. because lookup field before update to child object have same value .


Please let me know, once you get understand the outcome of trigger logic, if implemented on your requirement.

 

Thanks

 

rajat Maheshwari 6rajat Maheshwari 6
Hi Venkat,

Below is code snippet, please use this : - 
trigger childObjectTrigger on childObject (before update)
 {
    for(childObject chld_Obj : Trigger.new)
       {
          if(chld_Obj.ParentLookupField==Trigger.oldMap.get(chld_Obj.id).ParentLookupField && chld_Obj.ParentLookupField1==Trigger.oldMap.get(chld_Obj.id).ParentLookupField1)
               
{
      chld_Obj.addError('Same set of values are Entered, please change the lookup values');
}

}
}
Thanks
 
This was selected as the best answer
Venkat adityaVenkat aditya
Hi Rajat,

Thanks for your solutions.