Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

So I have a trigger that's working well - it looks at some multi-select fields on a Job object and then matches those to Accounts with the same values, pulling in the Account Manager's email address.

The only issue is that it pulls them in like so: ben@aol.com,tammy@msn.com,roger@hotlink.net.

When I really need it to pull in as: ben@aol.com, tammy@msn.com, roger@hotlink.net.

However, adding that extra space in there has proven quite difficult. I've tried adding it after the comma and as a separate piece, but it doesn't work or the code breaks.

Any help appreciated, code is below, Thanks!

  trigger CCPEmailTrigger on AVTRRT__Job__c (before insert, before update) {
  Set<ID> accIds = new Set<ID>();
  Set<ID> pipelineIds = new Set<ID>();
  Map<ID, String> accNameMap = new Map<ID, String>();
  Map<ID, String> pipeLineNameMap = new Map<ID, String>();
  List<Account> matchingAccounts = new List<Account>();
  String clientOppMatchString = '';
  String pipeLineMatchString = '';
  for(AVTRRT__Job__c job : Trigger.new) {
    if(job.Publish_CCP__c && (Trigger.isInsert || (Trigger.isUpdate && !Trigger.oldMap.get(job.id).Publish_CCP__c))) {
      accIds.add(job.AVTRRT__Account_Job__c);
      pipelineIds.add(job.Pipeline__c);
    }
  }
  for(Account acc : [Select Name from Account Where ID IN :accIds]) {
    accNameMap.put(acc.id, acc.Name);
    if(clientOppMatchString.contains(acc.Name)) {
      continue;
    }
    if(clientOppMatchString != '') {
      clientOppMatchString =  clientOppMatchString + ',\''+ String.escapeSingleQuotes(acc.Name) + '\'';
    } else {
      clientOppMatchString =  '\''+ String.escapeSingleQuotes(acc.Name) + '\'';
    }
  }
  for(Pipeline__c pipeLine : [Select Name From Pipeline__c]) {
    pipeLineNameMap.put(pipeLine.ID, pipeLine.Name);
    if(pipeLineMatchString.contains(pipeLine.Name)) {
      continue;
    }
    if(pipeLineMatchString == '') {
      pipeLineMatchString =  '\''+ String.escapeSingleQuotes(pipeLine.Name) + '\'';
    } else {
      pipeLineMatchString =  pipeLineMatchString + ',\''+ String.escapeSingleQuotes(pipeLine.Name) + '\'';
    }
  }
  System.debug('Query =>' + 'Select ID, Clients_for_Job_Opportunities__c, Pipelines_for_Job_Opportunities__c, Account_Manager_eMail_1__c ' +
                       + ' from Account Where CCP_Status__c = \'Active\' AND Clients_for_Job_Opportunities__c includes (' + clientOppMatchString + ') AND' + 
                       + ' Pipelines_for_Job_Opportunities__c includes (' + pipeLineMatchString + ')');
  if(!pipeLineNameMap.isEmpty() && !accNameMap.isEmpty()) {
    matchingAccounts = Database.query('Select ID, Clients_for_Job_Opportunities__c, Pipelines_for_Job_Opportunities__c, Account_Manager_eMail_1__c ' +
                       + ' from Account Where CCP_Status__c = \'Active\' AND Clients_for_Job_Opportunities__c includes (' + clientOppMatchString + ') AND' + 
                       + ' Pipelines_for_Job_Opportunities__c includes (' + pipeLineMatchString + ')');
  }
  String emailStr;             
  for(AVTRRT__Job__c job : Trigger.new) {
    emailStr = '';
    if(job.Publish_CCP__c && (Trigger.isInsert || (Trigger.isUpdate && !Trigger.oldMap.get(job.id).Publish_CCP__c))) {
      // Loop Through each account and find matching accounts
      for(Account acc : matchingAccounts) {
        if(
          acc.Clients_for_Job_Opportunities__c.contains(accNameMap.get(job.AVTRRT__Account_Job__c)) &&
          acc.Pipelines_for_Job_Opportunities__c.contains(pipeLineNameMap.get(job.Pipeline__c)) &&
          acc.Account_Manager_eMail_1__c != null
        ) {
          if(emailStr != '') {
            emailStr = emailStr + ',';
          }
          emailStr = emailStr + acc.Account_Manager_eMail_1__c;
        }
      }
      job.eMails_for_CCP_Job_Op_eMail__c = emailStr;
    }
  }             
}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Change

      if(emailStr != '') {
        emailStr = emailStr + ',';
      }

to

      if(emailStr != '') {
        emailStr = emailStr + ', '; // <-- add space following the comma
      }

That should put the desired extra space in for you.

share|improve this answer
    
Thanks @crmprogdev - I tried that once before but it didn't add the space? I'll implement again and see if it was just an accident the first time. –  McD Jul 12 at 15:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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