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
Mishal JoseMishal Jose 

How to compare field of existing record and new record

Need to solve a problem, I need to throw a error if the record with different value of country is inserted in object. Example: I need to check the value of existing country field and if the new record is inserted with different country value, I need to throw an error. Is there a way to achieve this.? 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mishal,

Are you okay to use Trigger on that object is this scenerio?

Thanks,
 
Sudhanshu Pandey 15Sudhanshu Pandey 15
Is your country field is of picklist datatypes?
you can perform logic as
first make a set<country datatype>() or map <id,string> and populate that with one record
thn you have to itearate record with collection thn
check if(set.contains(record re.country){
than save that record
else  {
record.adderror('your error messsage');
Mishal Jose 6Mishal Jose 6
HI Sai, Yes I am okay to use Trigger also this country field is a picklist data type.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mishal ,

Thanks for confirmation . Do you meant that If i am creating a record with country as India and if no records are present with Country as India then it should throw an error?

Thanks,
 
Mishal Jose 6Mishal Jose 6
Yes, there is no records existing with the country as India. It should throw an error.