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
mayur Kalbandemayur Kalbande 

system admin can create only one record for per contact(lookup) if admin try to create another record with same contact then show error

system admin can create only one record for per contact(lookup) if admin try to create another record with same contact then show error
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

Can you confirm on which object you are trying this? And also can you confirm what is the API anme of the contact lookup on that object so can share the code accordingly .

Thanks.
 
mayur Kalbandemayur Kalbande
Here is One custom object name is target setting on that object distributor name(no. of distributor) field is present which is lookup to target setting object. (distributor(parent) & target setting(child)). so user have only permit to create only one record per yer for each distributor otherwise show error(if user try to create another record with same distributor show error 'you can't create more than one record for + distributor name. in present year')
mayur Kalbandemayur Kalbande
Hello Sir,
Plz give me some hints
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

Sure will share the code in some time. So distributor cannot have more than one  target setting created in an year? Is that your requirement. Correct me if any wrong in it.

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

Can you try below trigger on Target Setting object.
 
trigger TriggerCountTarget on target_setting__c (after insert, after update, after undelete) {
Set<Id> DistributorIds = new Set<Id>();
  for(target_setting__c record: Trigger.new) {
    DistributorIds.add(record.distributor_name__c);
  }
  DistributorIds.remove(null);
  Set<Id> morethan2Contacts = new Map<Id, AggregateResult>([
    SELECT distributor_name__c  Id
    FROM target_setting__c
    WHERE createddate=THIS_YEAR  and  distributor_name__c  = :DistributorIds
    GROUP BY distributor_name__c 
    HAVING COUNT(Id) > 1]).keySet();
  for(target_setting__c record: Trigger.new) {
    if(moreThan2Contacts.contains(record.distributor_name__c )) {
      record.distributor_name__c .addError('you can t create more than one record in present year');
    }
  }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

Let me know if you face any issue using this code so Can modify it based on it.

Thanks,
 
mayur Kalbandemayur Kalbande
Yes Sir, Here Target setting record created by Territory Manager. when TM Create record, TM select distributor name. when create other record that time distributor name is other as previous. if TM select same Distributor name as previous that time show error.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

I have developed trigger according to your ask. Can you check and confirm on it.

Thanks
 
mayur Kalbandemayur Kalbande
i.e One distributor have only one record in an year. if TM try to create another record for same distributor then show error message.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mayur,

Did you tried the code shared above.

Thanks,
 
mayur Kalbandemayur Kalbande
Sir I want in before insert scenario
Sai PraveenSai Praveen (Salesforce Developers) 
hI Mayur,

It works in before insert only. Once you click on save then it will not save ans show you the error. It will not insert the second record.

Thanks,
mayur Kalbandemayur Kalbande
Sir Set<id> = new map Show error
Sai PraveenSai Praveen (Salesforce Developers) 
HI Mayur,

Can we connect and check entire trigger once.

Thanks,
 
mayur Kalbandemayur Kalbande
How We Connect Sir
Sai PraveenSai Praveen (Salesforce Developers) 
HI Mayur,

Can we connect over gmeet. 
Thanks,