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
Nec 123zxcNec 123zxc 

Conditional Statement using Apex Class

How to store records with limitation? Like if a user wants to store multiple data  to a certain object but the object accepts 1 record only, how to do that? Thanks!
Best Answer chosen by Nec 123zxc
mukesh guptamukesh gupta
Hi Nec,

you can use before insert trigger that's will stop the user to create more then one record,
 
trigger caseTrigger on Case(before insert)
{
List<Case> caseList = [Select id from Case where isActive = true LIMIT 1];

for(Case sNew : trigger.new)
{
 if(caseList.size() > 0 )
   sNew.addError('silver accepts 1 active case');
 
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Nec,

I'm not able to understand what you want based on the given information, it's a request from you to explain your problem with 
example so that I can help you. 


Thank you!

Regards 
Suraj Tripathi
Nec 123zxcNec 123zxc

Hello Suraj,

On the image the silver accepts 1 active case only. Do you have code for that?

Hello Suraj. Here is what I want to say. On the image the silver accepts 1 case only. Do you get now what I was saying?

 

Thank you,
Nec

 

mukesh guptamukesh gupta
Hi Nec,

you can use before insert trigger that's will stop the user to create more then one record,
 
trigger caseTrigger on Case(before insert)
{
List<Case> caseList = [Select id from Case where isActive = true LIMIT 1];

for(Case sNew : trigger.new)
{
 if(caseList.size() > 0 )
   sNew.addError('silver accepts 1 active case');
 
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 
This was selected as the best answer
Nec 123zxcNec 123zxc
Thank you bro. Stay safe! :)