0

I have a custom object named PCS_Adoption_Detail__c which contains a master-detail lookup relationship field called Pet_Name__c which points to PCS_Pet__c custom object. Now I need to restrict the user to insert a record in PCS_Adoption_Detail__c only when if the lookup name already exists more than 3 times in this custom object. i.e. when a user tries to insert a record i need to check whether the Pet_Name__c already exists more than 3 times or not in PCS_Adoption_Detail__c object and if it exists then i should show a validation message. Can this be done using validation rules? If not suggest me some other solution.

8
  • use trigger to validate and prevent the user. Mar 7, 2016 at 10:07
  • @Tushar Sharma can you please post a sample logic to my scenario?
    – Ean
    Mar 7, 2016 at 10:08
  • @Ezhilan Just to be clear, is it master detail or lookup relationship?
    – manjit5190
    Mar 7, 2016 at 10:11
  • @manjit_singh Yes it is.
    – Ean
    Mar 7, 2016 at 10:15
  • 3
    @Ezhilan . Since it is a master detail, you could create a rollup summary field on the Pet object that counts the number of child 'Pet Adoption' records under it. Now Create a validation rule in 'Pet Adoption' that fires if the chosen 'Pet Name' lookup records has the value in the earlier mentioned rollup field greater than 3. What do you say , is my understanding correct?
    – Prajith
    Mar 7, 2016 at 11:00

1 Answer 1

0

As mentioned by @Stygon I have used roll-up summary field to get the count and used a validation rule to show message if the count exceeds 3.

1
  • This trigger is not bulkified. You need to handle the use case where the detail records are added in bulk, and some should succeed and others fail depending on your rule. For example: use data loader to import records. Or, a rest service. Or, a clever VF page
    – cropredy
    Mar 7, 2016 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .