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
Justin Cairns 14Justin 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();

}
Best Answer chosen by Justin Cairns 14
Art Smorodin 7Art Smorodin 7
If TAFS_General_Manager_tag__c field on an Account object is of string type which always starts with "s" followed by a number (like s12, or s1) try using substring function: substring(startIndex) returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String.
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.