You need to sign in to do that
Don't have an account?
restrict more than one child record in parent record
Hi All,
I have an object X__c. it is having a lookup field to itself XC__c. My requirement is , an user can insert only one child record. Simplifying one parent id should be used to one child record. If that parentid is used again or twice trigger should throw an error.
Regards
I have an object X__c. it is having a lookup field to itself XC__c. My requirement is , an user can insert only one child record. Simplifying one parent id should be used to one child record. If that parentid is used again or twice trigger should throw an error.
Regards
Try out the Unique field. You can capture the obejct record id in a field and set that field as Unique so it will not have the same value for 2 records and in turn 2 records with the same parent id will not be created.
You can refer to:
https://help.salesforce.com/HTViewHelpDoc?id=custom_field_attributes.htm&language=en_US (search for Uniqule)
Hope this will help!
Thanks,
Pratik
Ca we have a trigger for this as we need to display custom error msg. Kindly let me know.
If you are goin for Unique field property, then you will be shown the existing record for the same value but not the custom error message.
You can write a trigger as well to compare value. Here is the sample code (this code is basically identify the duplicate lead based on email same you can replicate for the parent id)
Thanks,
Pratik
P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
trigger contactDemo on Contact( before insert,before update)
{
set<ID> ids= new set<ID>();
for(Contact c:trigger.new)
{
if(c.accountID!=null)
ids.add(c.accountID);
}
List<contact> cts1=[select id,name from contact where accountid in :ids];
System.debug('contacts are ======>'+cts1);
if((Trigger.isInsert &&cts1.size()>0)||(Trigger.isUpdate &&cts1.size()>1))
{
trigger.new[0].addError('duplicate');
}
}
For example,
If for an Account, you only want one opportunity to be created.
You can create a rollup field on Account to count number of Opportunities.
Now write a validation rule such that it throws an error if this rollup count is greater than 0.