You need to sign in to do that
Don't have an account?
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.
Thanks in advance!
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!
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: 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
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: 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.
I am glad you found the solution; just curious which option you went ahead with?
Kind Regards,
Swarna.
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XcCkQAK
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.
If inactive_c is true do not calculate in field Number_of_Active_Contacts__c