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
abhilash reddy 30abhilash 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
Best Answer chosen by abhilash reddy 30
Naveen KumarNaveen Kumar
Here is bulkified code Let me know if u face any issues........
 
trigger TestLimitofChild_At on Contact (before insert) {
    set<id> setid =new set<id>();
    map<id,Decimal> countmap=new map<id,Decimal>();
    integer i,j;
    map<id,Boolean> check=new map<id,Boolean>();
    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(Id),AccountId from Contact where AccountId =:setid group by AccountId];
    for(AggregateResult ar:pre_child_total)
    {
        countmap.put((id)ar.get('AccountId'),(Decimal)ar.get('expr0'));
        system.debug('count'+countmap);
    }
    for(Account ac:parent)
    {
        if(ac.joint_account__c==true)
        {
            check.put(ac.id,ac.joint_account__c);
        }
    }
    for(Contact cr:Trigger.new)
    {
        if(countmap.containsKey(cr.AccountId) && (integer)countmap.get(cr.AccountId)<2 && check.containsKey(cr.AccountId) )
        {   
            i=(integer)countmap.get(cr.AccountId);
            j=i+1; 
            countmap.put(cr.AccountId,j);
        }
        
        else if(check.containsKey(cr.AccountId) && !countmap.containsKey(cr.AccountId))
        {
            countmap.put(cr.AccountId,1);
        }
        else if(!countmap.containsKey(cr.AccountId) && !check.containsKey(cr.AccountId) )
        {
            countmap.put(cr.AccountId,1);
        }
        else
        {
            cr.adderror('u cant');
        }
        
    }
}

 

All Answers

Lokesh KumarLokesh Kumar
Hi Abhilash,

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 !!
abhilash reddy 30abhilash reddy 30
Hi Lokesh Thanks for quick reply i am new to software industry and as well as to salesforce..could you plz provide code for this i will be highly oweful to u thank you i will wait 4 ur reply
 
Naveen KumarNaveen Kumar
Hi Abhliash,
 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
        {
            
        }
    }
}



 
Lokesh KumarLokesh Kumar
Hi Abhi,
,
I think Naveen already posted a code for the same please try and let me know if you have any question.

Thanks !
abhilash reddy 30abhilash reddy 30
Thank you Naveen And Lokesh thank you very much..
Naveen KumarNaveen Kumar
Here is bulkified code Let me know if u face any issues........
 
trigger TestLimitofChild_At on Contact (before insert) {
    set<id> setid =new set<id>();
    map<id,Decimal> countmap=new map<id,Decimal>();
    integer i,j;
    map<id,Boolean> check=new map<id,Boolean>();
    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(Id),AccountId from Contact where AccountId =:setid group by AccountId];
    for(AggregateResult ar:pre_child_total)
    {
        countmap.put((id)ar.get('AccountId'),(Decimal)ar.get('expr0'));
        system.debug('count'+countmap);
    }
    for(Account ac:parent)
    {
        if(ac.joint_account__c==true)
        {
            check.put(ac.id,ac.joint_account__c);
        }
    }
    for(Contact cr:Trigger.new)
    {
        if(countmap.containsKey(cr.AccountId) && (integer)countmap.get(cr.AccountId)<2 && check.containsKey(cr.AccountId) )
        {   
            i=(integer)countmap.get(cr.AccountId);
            j=i+1; 
            countmap.put(cr.AccountId,j);
        }
        
        else if(check.containsKey(cr.AccountId) && !countmap.containsKey(cr.AccountId))
        {
            countmap.put(cr.AccountId,1);
        }
        else if(!countmap.containsKey(cr.AccountId) && !check.containsKey(cr.AccountId) )
        {
            countmap.put(cr.AccountId,1);
        }
        else
        {
            cr.adderror('u cant');
        }
        
    }
}

 
This was selected as the best answer
Naveen KumarNaveen Kumar
It's worked finely or It's Solve Your issue mark the best answer it's close the thread that will be helpful for others

 
abhilash reddy 30abhilash reddy 30
Thank you naveen in your first code why u declared  countTotal variable as Decimal Instead of integer i did not understand and i did not understand the code part below could u plz explain about this clearly.i am asking why b'coz countTotal should be integer but why u declared it as decimal....
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');
        }
Naveen KumarNaveen Kumar
Hi abhilash,
                 As Your Wish u change integer it's also worked Finely.....but Change like this..
trigger TestLimitofChild_At on Contact (before insert) {
    set<id> setid =new set<id>();
    Integer 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=(Integer)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
        {
            
        }
    }
}

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


 
abhilash reddy 30abhilash reddy 30
Thank you Naveen,
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?
abhilash reddy 30abhilash reddy 30
Hi naveen,
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...
Naveen KumarNaveen Kumar
Hi Abhilash,
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
 
abhilash reddy 30abhilash reddy 30
Hi Naveen,

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);

        }
 
abhilash reddy 30abhilash reddy 30
if check box is checked,that part of code is working fine....
Naveen KumarNaveen Kumar
If Check box is Unchecked I  allow to creation of contact (Only once) the same trigger on my org
 
abhilash reddy 30abhilash reddy 30
I don't know and i don't  have any VR also........
abhilash reddy 30abhilash reddy 30

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...
Naveen KumarNaveen Kumar
If the checkbox is true and the contact is first child of account thus time the contact can be created. That code to be  implemented in That else if part
 
Naveen KumarNaveen Kumar
else if(!countmap.containsKey(cr.AccountId) && !check.containsKey(cr.AccountId) ) { countmap.put(cr.AccountId,1); }

This part of code i done Check box is false And the account Is Parent Of child contact
........
abhilash reddy 30abhilash reddy 30
Thank You Naveen my issue is solved ...thank you very much.....thank u for ur patience....i will be oweful to u.....if i have any kind of doughts in near future how can i contact u