You need to sign in to do that
Don't have an account?
Justin Cairns 14
How to add an integer to a Trigger.
I'm trying to create a trigger that will Sum Contacts at the Account Level when the Contact Type = Owner. I want to take that number increate it by 1 and add an 's' to the front of it and populate it on an Account Level Field called TAFS_General_Manager_tag__c.
The problem I'm having is comparing the string field to the integer in my trigger. How can I create an integer to use in the trigger? My trigger is listed below. So the end result I want to place in the TAFS_General_Manager_tag__c will be 's' + (sum of contacts with a type of Owner +1)
trigger ContactSumTrigger on Contact (after delete, after insert, after undelete,
after update) {
Contact[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;
// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
acctIds.add(con.AccountId);
}
Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
,AccountId
from Contact
where AccountId in :acctIds and TAFS_Contact_Type__c = 'Owner']);
Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
,TAFS_General_Manager_tag__c
from Account
where Id in :acctIds]);
for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contact con : contactsForAccounts.values()) {
if (con.AccountId == acct.Id)
conIds.add(con.Id);
}
if (acct.TAFS_General_Manager_tag__c != conIds.size())
acct.TAFS_General_Manager_tag__c = 's' + conIds.size();
}
update acctsToUpdate.values();
}
The problem I'm having is comparing the string field to the integer in my trigger. How can I create an integer to use in the trigger? My trigger is listed below. So the end result I want to place in the TAFS_General_Manager_tag__c will be 's' + (sum of contacts with a type of Owner +1)
trigger ContactSumTrigger on Contact (after delete, after insert, after undelete,
after update) {
Contact[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;
// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
acctIds.add(con.AccountId);
}
Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
,AccountId
from Contact
where AccountId in :acctIds and TAFS_Contact_Type__c = 'Owner']);
Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
,TAFS_General_Manager_tag__c
from Account
where Id in :acctIds]);
for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contact con : contactsForAccounts.values()) {
if (con.AccountId == acct.Id)
conIds.add(con.Id);
}
if (acct.TAFS_General_Manager_tag__c != conIds.size())
acct.TAFS_General_Manager_tag__c = 's' + conIds.size();
}
update acctsToUpdate.values();
}
So in theory you can do: Integer ABC = integer.valueof(acct.TAFS_General_Manager_tag__c.substring(1));
acct.TAFS_General_Manager_tag__c.substring(1) -> will get rid of the s in the beginning of the strung;
integer.valueof() - > will convert string "number" to an actual number.
Hope it makes sence.
Art.