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
Synthia BeauvaisSynthia Beauvais 

Compile Error: Incompatible types in ternary operator: Integer, String

I am trying to get a count of the number of contacts on an account, I am getting Error: Compile Error: Incompatible types in ternary operator: Integer, String at line 72 column 42. 
 
trigger NumberofContacts on Contact (after insert, after update, after delete, after undelete) {
    Map<Id, List<Contact>> mapAcctIdContactList = new Map<Id, List<Contact>>();
    Map<Id, List<Contact>> mapAcctIdDelContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> listAcct = new List<Account>();
    
    if(trigger.isInsert) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)) {
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            } else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) {
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);   
                AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Contact Con : trigger.new) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con);     
                AcctIds.add(Con.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Contact Con : trigger.Old) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);    
                AcctIds.add(Con.AccountId); 
            }
        }  
    }   
    
    if(AcctIds.size() > 0) {
        listAcct = [SELECT Id, Number_of_Contacts__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account acct : listAcct) {
            Integer noOfConts = 0;
            if(mapAcctIdContactList.containsKey(acct.Id)) {
                noOfConts += mapAcctIdContactList.get(acct.Id).size();
            }
            if(mapAcctIdDelContactList.containsKey(acct.Id)) {
                noOfConts -= mapAcctIdDelContactList.get(acct.Id).size();
            }
            acct.Number_of_Contacts__c = acct.Number_of_Contacts__c == null ? noOfConts : (acct.Number_of_Contacts__c + noOfConts);
        }
        
        update listAcct;    
    }
}

Thanks in advance!
Best Answer chosen by Synthia Beauvais
SwarnaSankhaSinghSwarnaSankhaSingh
Hi Synthia,

From the error message it looks like the field named Number_of_Contacts__c on the Account object is set as a TEXT field.

The first approach (a hacky one) would be to modify your existing code snippet on Line 72 to this line of code:
acct.Number_of_Contacts__c = acct.Number_of_Contacts__c == null ? string.valueof(noOfConts) : string.valueof(integer.valueof(acct.Number_of_Contacts__c) + noOfConts);
The Second and definitely the more BETTER solution would be to change the Number_of_Contacts__c from a TEXT to a NUMBER field and leave your existing code as is.

I hope this help; do let me know how it worked out for you and which approach you went along with.

If you feel that your question was answered then do flag the appropriate answer as the solution to your query.

Kind Regards,
Swarna.

All Answers

SwarnaSankhaSinghSwarnaSankhaSingh
Hi Synthia,

From the error message it looks like the field named Number_of_Contacts__c on the Account object is set as a TEXT field.

The first approach (a hacky one) would be to modify your existing code snippet on Line 72 to this line of code:
acct.Number_of_Contacts__c = acct.Number_of_Contacts__c == null ? string.valueof(noOfConts) : string.valueof(integer.valueof(acct.Number_of_Contacts__c) + noOfConts);
The Second and definitely the more BETTER solution would be to change the Number_of_Contacts__c from a TEXT to a NUMBER field and leave your existing code as is.

I hope this help; do let me know how it worked out for you and which approach you went along with.

If you feel that your question was answered then do flag the appropriate answer as the solution to your query.

Kind Regards,
Swarna.
This was selected as the best answer
SwarnaSankhaSinghSwarnaSankhaSingh
Hi Synthia,

I am glad you found the solution; just curious which option you went ahead with?

Kind Regards,
Swarna.
Synthia BeauvaisSynthia Beauvais
Thank you Swarna!! I changed the field to number. Could you possible assist with the trigger below? I am still learning and I want to study both of these methods.

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XcCkQAK 

 
SwarnaSankhaSinghSwarnaSankhaSingh
Hi Synthia,

I checked this trigger as well as the one you mentioned in the other post. So basically what you want to do is keep a track of Active vs. Inactive Contacts associated with an Account at the Account level - correct? And the field on the Contact is Inactive__c which if True means that the Contact is Inactive.

I understand that you are learning APEX which is really good and for starters your code is pretty slick. Did you know that you could also manage to achive the same result using Process Builder?

Kind Regards,
Swarna.
Synthia BeauvaisSynthia Beauvais
I would have to use a flow inconjunction with the process builder. I found that the trigger would be easier. I would like to get the total number of contacts and the total number of active contacts. 
 
trigger NumberOfContacts on Account (before insert, before update) {
    if(trigger.isinsert)
        for(account a:trigger.new)
            a.Number_of_contacts__c = 0;
    else
        for(account a:[select id,(select id from contacts) from account where id in :trigger.new])
            trigger.newmap.get(a.id).Number_of_contacts__c = a.contacts.size();
}

If inactive_c is true do not calculate in field Number_of_Active_Contacts__c 
Synthia BeauvaisSynthia Beauvais
User-added image