-1

For any account if we are inserting contact, then for the first contact primary__c check box becomes true, when we insert second contact then secondary__C check becomes true irrespective of primary__c check box.

Here is my Trigger.

Please suggest.

trigger checkbox on Contact (after insert) {
set<id> ids = new set<id>();
list<contact> newcon = new list<Contact>();
list<contact> newcon1 = new list<Contact>();
list<contact> newcon2 = new list<Contact>();
list<contact> newcon3 = new list<Contact>();

for (contact c :trigger.new){
ids.add(c.accountid);
}

list<Account> newacc =[select id,(select id,primary__c,secondary__c from contacts) from account where id in :ids];
system.debug('===>newacc'+ newacc);

for (account a :newacc){
for (contact c :a.contacts){
newcon.add(c);
system.debug('===>newcon'+newcon);
}
}
for (contact c:newcon){
newcon1.add(c);
system.debug('===>newcon1'+newcon1);
}

for (contact c :newcon1){
if(c.primary__c == false){
c.primary__c =true;
newcon2.add(c);
system.debug('===>c'+c);
}
if(c.primary__c == false){
c.secondary__c =true;
newcon3.add(c);
}
}
update newcon2;
update newcon3;
system.debug('===>newcon2'+newcon2);
}
  • 2
    What specific issue are you encountering with your existing code? It'll be a lot easier to help you if you use indentation to make your code readable. – David Reed Jan 3 at 13:03
  • when i insert contact then both check box becomes true. But my requirement is for first contact primary check box become true & for second contact secondary check box becomes true – vvpps ej Jan 3 at 13:12
  • think about renaming your vbls to be meaningful as to what they are supposed to represent. newconX where X = 1,2,3 is not helpful towards debugging this issue – cropredy Jan 3 at 19:54
-1

For this requirement you need to query count of Contacts on particular account, if this count is zero, then the new contact is primary else secondary, see below code. I have used Aggregate Query to get counts

trigger checkbox on Contact (before insert) {
    set<id> ids = new set<id>();

    for (contact c :trigger.new){
        ids.add(c.accountid);
    }

    AggregateResult[] groupedResults =[SELECT AccountId, Count(Id) FROM Contact WHERE AccountId <> NULL AND AccountId IN : ids GROUP BY AccountId];
    // get count of contacts for account
    Map<Id,Integer> accountContactCount = new Map<Id,Integer>();
    for(AggregateResult ar:groupedResults) {
        Id accid = (ID)ar.get('AccountId');
        if(accid != null ){
            Integer count = (INTEGER)ar.get('expr0');
            accountContactCount.put(accid,count);

        }
    }

    for(Contact c :trigger.new){
        if(!String.isBlank(c.AccountId)){
            if(accountContactCount.containsKey(c.AccountId)){
                // if account has 0 contacts then the current contact becomes primary.
                if(accountContactCount.get(c.AccountId) == null || accountContactCount.get(c.AccountId) == 0){
                    c.primary__c = true; 
                    accountContactCount.put(c.AccountId,1);
                } else {
                    c.secondary__c = true;
                }
            }
        }
    }
}
  • Rahul code is not working even if c.secondary__c = true inserted. – vvpps ej Jan 3 at 16:34
  • secondary__c = true; Field is read only – vvpps ej Jan 3 at 16:34
  • @vvppsej then try commenting this line c.secondary__c = true; – rahul gawale Jan 3 at 17:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.