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
SIVASASNKARSIVASASNKAR 

Throw an Error along with the error record

I want to show an error that already a record is made primary. 'Please uncehck primary on test11 record'

Best Answer chosen by Admin (Salesforce Developers) 
ShravanKumarBagamShravanKumarBagam

 

Hi,

 

   

Try below code...

 

trigger testtrig1 on Test1__c (before insert) {

 

Test1__c t=trigger.new[0];


if(t.primary__c == true){

Test1__c t1= [Select id,Name,Primary__c from Test1__c where Primary__c = true];

t.AddError('Please uncehck primary on'+' '+ t1.name +' '+'record');

}

}

All Answers

Jerun JoseJerun Jose
You can use an apex trigger for this purpose. The trigger will have to query the existing records ignoring the current set and then identify if there are any duplicates.
ShravanKumarBagamShravanKumarBagam

 

Hi,

 

   

Try below code...

 

trigger testtrig1 on Test1__c (before insert) {

 

Test1__c t=trigger.new[0];


if(t.primary__c == true){

Test1__c t1= [Select id,Name,Primary__c from Test1__c where Primary__c = true];

t.AddError('Please uncehck primary on'+' '+ t1.name +' '+'record');

}

}

This was selected as the best answer