1

I am the sole SFDC Admin at a non-profit with just declarative knowlege. There is an existing Trigger on the Campaign Member that was created by a predecessor. This trigger updates a couple Contact fields when the Campaign Member is created. It takes values from 1 long text area field on the Campaign Member (the values in that field are set in the Eventbrite Sync and populate on separate lines within the text box). There is a new question (Campaign Member field) the business has added to the Campaign in Eventbrite and have added that to the sync so it comes in to the Campaign member field. The field on the Campaign member is called "Eventbrite Answers" and the 2 existing Contact fields that get set from the Trigger are: Attendee Type, Res Scale of Work and Com Scale of work. The new Contact field is: Training discovered by.

Here is the existing/working trigger code:

trigger ImportEBCampaignMember on CampaignMember (before insert, before update) {
  List<Contact> contacts = new List<Contact>();  // list of contacts for batch update
  Contact con;                // contact object, to construct each update
  String cmContId;              // contact ID from campaign member
  String cmAns;                // answers from campaign member
  List<String> cmAnsLines;          // answers, split into separate lines
  List<String> tok;              // line, split into label and value
  String tokt;                // trimmed value (white space removed)
  // populate sets of valid picklist values to ensure only valid values are written (update with picklist changes)
  // sets instead of lists since apex doesn't support 'contains' for lists
  Set<String> validAType = new Set<String>{'Architect','Builder','Code Official','Developer','Electrical Engineer',
'Facility Owner','General Contractor','HVAC Contractor','Manufacturer','Mechanical Engineer','Other'};
  Set<String> validRSow = new Set<String>{'<10 units/year','>10 and < 100 units/year','>100 units/year'};
  Set<String> validCSow = new Set<String>{'<25,000','25,000-50,000','50,000-100,000','>100,000'};

  for(CampaignMember cm: Trigger.new) {    // loop through all campaign members in this batch
    cmContId = cm.ContactId;        // contact ID from campaign member
    cmAns = cm.gweb__Eventbrite_Answers__c; // answers from campaign member
    if (String.isNotBlank(cmAns) && String.isNotBlank(cmContId)) {  // only process records w/ non-blank answers and non-blank ID
      con = new Contact(id=cmContId);    // create a contact with the desired ID
      cmAnsLines = cmAns.split('\n',0);  // split answers into separate lines
      for (String l: cmAnsLines) {    // for each line
        tok = l.split(':',0);      // split line into tokens on ':'
        if (tok.size() != 2) continue;  // must be a line divided by a single ':'
        tokt = tok.get(1).trim();    // trim the passed value, if needed
        // based on left token, decide which field to update
        if (tok.get(0).equalsIgnoreCase('Attendee Type') && validAType.contains(tokt)) { con.Attendee_Type__c = tokt; continue; }
        if (tok.get(0).startsWith('If other,')) { con.Other_Type__c = tokt; continue; }
        if (tok.get(0).startsWith('Please provide the # of units') || tok.get(0).startsWith('How many square feet')) {
          if (tok.get(1).contains('000') && validCSow.contains(tokt)) { con.Com_Scale_of_Work__c = tokt; continue; }
          if (tok.get(1).contains('units') && validRSow.contains(tokt)) { con.Res_Scale_of_Work__c = tokt; continue; } 
        }
      }
      contacts.add(con);          // add this contact to the list
    }
  }
  update contacts;              // update the indicated contacts
} 

I edited the code as follows but while it still set the existing values, it did not set the final answer on the new Contact field

trigger EBTrigger on CampaignMember (before insert, before update) {
    List<Contact> contacts = new List<Contact>();  // list of contacts for batch update
    Contact con;                                // contact object, to construct each update
    String cmContId;                            // contact ID from campaign member
    String cmAns;                               // answers from campaign member
    List<String> cmAnsLines;                    // answers, split into separate lines
    List<String> tok;                           // line, split into label and value
    String tokt;                                // trimmed value (white space removed)
    // populate sets of valid picklist values to ensure only valid values are written (update with picklist changes)
    // sets instead of lists since apex doesn't support 'contains' for lists
    Set<String> validAType = new Set<String>{'Architect','Builder','Code Official','Developer','Electrical Engineer',
    'Facility Owner','General Contractor','HVAC Contractor','Manufacturer','Mechanical Engineer','Other'};
    Set<String> validRSow = new Set<String>{'<10 units/year','>10 and < 100 units/year','>100 units/year'};
    Set<String> validCSow = new Set<String>{'<25,000','25,000-50,000','50,000-100,000','>100,000'};
    Set<String> validTrainDisc = new Set<String>{'National Grid Website','Email Blast','Word of Mouth','USGBC Website','AIA Website','RI Builder Association Calendar'};

    for(CampaignMember cm: Trigger.new) {       // loop through all campaign members in this batch
        cmContId = cm.ContactId;                // contact ID from campaign member
        cmAns = cm.gweb__Eventbrite_Answers__c; // answers from campaign member
        if (String.isNotBlank(cmAns) && String.isNotBlank(cmContId)) {  // only process records w/ non-blank answers and non-blank ID
            con = new Contact(id=cmContId);     // create a contact with the desired ID
            cmAnsLines = cmAns.split('\n',0);   // split answers into separate lines
            for (String l: cmAnsLines) {        // for each line
                tok = l.split(':',0);           // split line into tokens on ':'
                if (tok.size() != 2) continue;  // must be a line divided by a single ':'
                tokt = tok.get(1).trim();       // trim the passed value, if needed
                // based on left token, decide which field to update
                if (tok.get(0).equalsIgnoreCase('Attendee Type') && validAType.contains(tokt)) { con.Attendee_Type__c = tokt; continue; }
                if (tok.get(0).startsWith('If other,')) { con.Other_Type__c = tokt; continue; }
                if (tok.get(0).startsWith('Please provide the # of units') || tok.get(0).startsWith('How many square feet')) {
                    if (tok.get(1).contains('000') && validCSow.contains(tokt)) { con.Com_Scale_of_Work__c = tokt; continue; }
                    if (tok.get(1).contains('units') && validRSow.contains(tokt)) { con.Res_Scale_of_Work__c = tokt; continue; } 
                if (tok.get(2).equalsIgnoreCase('Training Discovered By') && validTrainDisc .contains(tokt)) { con.Training_Discovered_By__c = tokt; continue; }
                }
            }
            contacts.add(con);                  // add this contact to the list
        }
    }
    update contacts;                            // update the indicated contacts
}

Here is what the Campaign Member field looks like on the record I created: enter image description here

0

You need to change this:

if (tok.get(2).equalsIgnoreCase('Training Discovered By') && validTrainDisc.contains(tokt)) { con.Training_Discovered_By__c = tokt; continue; }

To this:

if (tok.get(0).equalsIgnoreCase('Training Discovered By') && validTrainDisc.contains(tokt)) { con.Training_Discovered_By__c = tokt; continue; }

To explain the answer, the code breaks apart the long text field into rows and the splits those strings into parts, the label and the value. The two parts are put into a list of strings, that's what the split does. The list index is zero based and get(0) gets the first item in the list, in this case that's the label of each row. The Training Discovered By row is a simple label and value row so get(0) is gives you the label value and get(1) or tokt is the value.

  • Unfortunately changing the 2 to a zero did not work. E – user7417 May 18 '14 at 16:21
  • After reviewing the code further, you were correct with the 0 but I also needed to move that line up to be the first – user7417 May 18 '14 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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