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
Mounika Karri 6Mounika Karri 6 

if I have one parent and several childs and if these childs have a picklist field where if I choose one value in one of the childs then I cannot choose the same value for another child.How can I write a trigger for this situation

Raj VakatiRaj Vakati
try this
trigger updatePublishDate on Child__c(before insert) {
  // Aggregate all parent Ids
  Set<Id> parentIds = new Set<Id>();
  for(Child__c record: {
  Map<Id, AggregateResult> results = new Map<Id, AggregateResult>([
    SELECT Parent__c Id, Count(Id) cntId ,Picklist__c pck
    FROM Child__c
    WHERE Parent__c = :parentIds
    GROUP BY Parent__c ,Picklist__c]);
   for(Child__c record: {
		if(results.contains(record.Parent__c) && results.get(record.Parent__c).get('pck') == c.Picklist__c && results.get(recordId).get('cntId') >1){
		record.addErrror('More than one value');