You need to sign in to do that
Don't have an account?
abhilash reddy 30
how to restrict creation of new contacts to certain number for particular account based on checkbox field on account object?
My Requirement is:
--->i have a check box filed on account named as joint account.
--->while creating new account end user checkd that check box and saved the account record.
---->here is my main requirement part:if check box is checked user can able to create only 2 contacts for that particular account,if user trying to create third contact it has to throw an error.
----->if check box is not checked user can able to create only 1 contact for that particular account,if user trying to create second contact it has to throw an error.
How can we achieve above requirement i tried a lot but i am unsuccessful
--->i have a check box filed on account named as joint account.
--->while creating new account end user checkd that check box and saved the account record.
---->here is my main requirement part:if check box is checked user can able to create only 2 contacts for that particular account,if user trying to create third contact it has to throw an error.
----->if check box is not checked user can able to create only 1 contact for that particular account,if user trying to create second contact it has to throw an error.
How can we achieve above requirement i tried a lot but i am unsuccessful
All Answers
You can write a trigger on Contact before insert.
1.get the count of related contact for the particular account for which you are going to insert a new contact.
2.Now check the status of checkBox for the account record.
3.Put a condition for if(checkbox == true) then if contact record count is more that 2 display an error and vice versa.
Hope this will help you in writing a trigger presuming you are handy in writing trigger.
Happy to Help you !!
I accomplish task Using Trigger
trigger TestLimitofChild_At on Contact (before insert) {
set<id> setid =new set<id>();
Decimal countTotal;
Boolean check;
for(Contact c:trigger.new)
{
setid.add(c.accountId);
}
list<Account> parent = [select Id,joint_account__c from Account WHERE Id =:setid];
List<AggregateResult> pre_child_total = [select count(AccountId) from Contact where AccountId =:setid];
for(AggregateResult ar:pre_child_total)
{
countTotal=(Decimal)ar.get('expr0');
system.debug('count'+countTotal);
}
for(Account ac:parent)
{
check=ac.joint_account__c;
}
for(Contact cr:Trigger.new)
{
if(check==true && countTotal > 1)
{
cr.adderror('U Cannot ');
}
else if(check==false && countTotal > 0)
{
cr.adderror('U Cannot add more 2');
}
else
{
}
}
}
,
I think Naveen already posted a code for the same please try and let me know if you have any question.
Thanks !
for(Contact cr:Trigger.new)
{
if(check==true && countTotal > 2)
{
cr.adderror('U Cannot ');
}
else if(check==false && countTotal > 1)
{
cr.adderror('U Cannot add more 2');
}
As Your Wish u change integer it's also worked Finely.....but Change like this..
But Best practice You use my second version of code because it's bulkified.My first version works for only single records not bulkified manner....
Thanks
But in ur second version of code, if check box is true it is allowing 3 contact records creation but i want only 2 contacts and when check box is not checked it is allowing 2 contact records creation but i want only1...
and second thing is when i change the data type from decimal to integer it is not working as before..why?
In bulkified trigger if check box is true it is working fine but when check box is unchecked it has to allow user to create one contact record and if user try to create second one it has to throw the error.but your code does not allow me to create even single contact when check box is unchecked could you plz send me the correction code of it thank you...
My Bulkified Code Is worked And Meet Your Criteria.And I can't Understand why Is not working For U.Check Ur Org Any validaion rules on that
the below part of code not working.meaning if check box is unchecked it has to allow user to create one contact not more than that.but it does not allow me to even create one contact
else if(check.containsKey(cr.AccountId) && !countmap.containsKey(cr.AccountId))
{
countmap.put(cr.AccountId,1);
}
else if(check.containsKey(cr.AccountId) && !countmap.containsKey(cr.AccountId))
{
countmap.put(cr.AccountId,1);
}
In above block of code u r checking else if condition in 'check.containsKey(cr.AccountId)' it means again u r traversing through the map which contains check box value true but accordind to requirement we have to traverse through map which contains check box value false...
This part of code i done Check box is false And the account Is Parent Of child contact
........