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
PhryPhry 

Trigger to update accounts not working

Hello folks,

 

                        I've been working on fixing a trigger we have setup on our salesforce domain. This trigger was setup more then a year ago  by someone else and at that time was working. However for quite awhile now it just does not seem to be firing at all. What the trigger is suppose to do is add to and update our salesforce customer accounts database from nightly uploads with the dataloader from our in house database info. The Dataloader is functioning correctly, with .csv file contents is being uloaded to the salesforce org. I can see the updated customer info as Customer Imports Object on the cloud. However the trigger is then supposed to take this info and update the actual Salesforce Account objects, which it is not. I've listed the Trigger and dependent class that maps all the data from the customer imports data to the accounts on the cloud, as well as the Account_import_c object description info. I have attempted to run logs on this trigger and it does not appear to be firing at all. Both trigger and class are valid and active. I have run tests on these and code coverage is 100% on the trigger and 98% on the class. Unfortunately i cannot contact the original author of all of this, i've been tasked with trying to get our salesforce accounts to be updated each morning again when the dataloader performs the upload to salesforce. Any help or insight as to what might be wrong with these would be greatly appreciated.  Thanks Mike

 

 

Here is the tigger accountImportAfterInsertUpdate.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ==================================================================================
//   Object: accountImportAfterInsertUpdate 
//
//  
// Comments: Automatically create Statement and Service Accounts, plus Contacts,
//           whenever a new Account Import record is inserted or upserted.
// ==================================================================================
//  Changes: 2011-07-05 Initial version.
// ==================================================================================

trigger accountImportAfterInsertUpdate on Account_Import__c (after insert, after update)
{
    // These records are coming from CommSoft. There is a single record in the CSV
    // file, from which we derive up to three separate records. We want to ensure
    // that no duplicate records are created but updates to existing records are
    // preserved. We also want to capture any import errors and store them on the
    // import record. This is why we are calling an @future class to do the actual
    // work, because we cannot directly update the record in an after trigger. 
    Set<Id> ids = system.trigger.newMap.keySet();
    importAccount.doImport(ids);
}

 

  
PhryPhry

 Here is importaccount class that is a dependency of the trigger

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

// ==================================================================================
//   Object: importAccount 
// 
//  
// Comments: Imports Statement and Service Accounts from Account_Import__c. Also
//       creates Contact records for these accounts.
// ==================================================================================
//  Changes: 2011-07-05 Initial version.
// ==================================================================================

public with sharing class importAccount
{
  public static map<Id, Account_Import__c> importMap = new map<Id, Account_Import__c>();
  public static Id currentImport;
  public static set<String> statementAccountIds = new set<String>();
  public static list<Account> statements = new list<Account>();
  public static map<String, Account> statementMap = new map<String, Account>();  
  public static set<String> serviceAccountIds = new set<String>();
  public static list<Account> services = new list<Account>();
  public static map<String, Account> serviceMap = new Map<String, Account>();
  public static Map<String, String> serviceTypeMap = 
    new Map<String, String>{ 
      'ALM' => 'Alarm Monitoring',
      'ADV' => 'Directory Advertising',
      'ANS' => 'Answering Services',
      'ASR' => 'ASR',
      'B1'  => 'Business Phone',
      'B1H' => 'Business Phone Trunk Hunt',
      'CAB' => 'Cable',
      'CEL' => 'Centrex-I, II, III',
      'CSB' => 'Centrex-Small Business',
      'DAM' => 'Damages',
      'DCT' => 'Direct Cut-Through',
      'DID' => 'Direct Inward Dialing',    
      'DSL' => 'Internet-DSL',    
      'EQP' => 'Equipment Leases',    
      'ERB' => 'Enhanced Ringing',    
      'ERR' => 'Enhanced Ringing',    
      'E91' => 'Create E911 Records',    
      'FLT' => 'Foreign Listing',    
      'FRD' => 'Internet-DSL-Front Royal',    
      'INS' => 'Insurance Billing',    
      'INT' => 'Internet-Dial up',    
      'ISD' => 'ISDN',    
      'IWM' => 'Wire Maintenance Plans',    
      'MCP' => 'Misc Cables/Parts',    
      'MDU' => 'MDU',    
      'MSC' => 'Miscellaneous billings',    
      'OIS' => 'OIS  On-Line Pages',    
      'PAG' => 'Paging Service',    
      'PRI' => 'PRI',    
      'PSC' => 'Payphone (COCOT)',    
      'PSJ' => 'Payphone (Joint)',    
      'PSN' => 'Payphone (Coinless)',    
      'PSP' => 'Payphone (Public)',    
      'RCF' => 'Remote Call Forwarding',    
      'RNT' => 'Building Rental',    
      'R1'  => 'Residential Phone',    
      'SLB' => 'Toll Only Billing',    
      'SPC' => 'Special Circuit',    
      'STC' => 'STC Test lines',    
      'TFS' => 'Toll Free Service',    
      'TSS' => 'Time Share',    
      'TVA' => 'Cable Advertising', 
      'VML' => 'Voicemail'};
  public static Map<String, String> statusMap = 
    new Map<String, String>{ 
      'A' => 'Active',
      'D' => 'Disconnected',
      'P' => 'Temporarily Disconnected'};

  @future
  public static void doImport(Set<Id> importIds)
  {
    try
    {
      // Get the record types for this object.
      ID statementRecordType;
      ID serviceRecordType;
      List<RecordType> recordTypes = [SELECT Id, Name FROM RecordType WHERE SObjectType = 'Account'];
      for (RecordType rt : recordTypes)
      {
        if (rt.Name == 'Statement Account') statementRecordType = rt.Id;
        if (rt.Name == 'Service Account') serviceRecordType = rt.Id;
      }
      if (statementRecordType == null || serviceRecordType == null)
      {
        // We really cannot handle errors in an @future call.
        system.debug('Record types have not been defined for Accounts. Please contact your Salesforce administrator.');
        return;
      }
      
      // Get a list of import records.
      importMap = new map<Id, Account_Import__c>(
        [SELECT Id,
            Name,
            ACI_Email__c,
            ACT_Service_Type__c,
            ACT_Status__c,
            City__c,
            Community_Code__c,
            CUS_First_Name__c,
            CUS_Last_Name__c,
            CUS_MI__c,
            CUS_Number__c,
            CUS_Permanent_Phone_Nbr__c,
            Error_Message__c,
            House_Street_Numb__c,
            House_Street_Numb_Suffix__c,
            Postal_Code__c,
            Service_Acct_Nbr__c,
            State__c,
            Statement_Code__c,
            Street_Name__c
           FROM Account_Import__c
          WHERE Id IN :importIds]);
    
      // Get a list of account IDs.
      // This is a parent-child relationship between Statement Accounts
      // and Service Accounts. If there isn't a Statement Account with
      // this ID, create one of these first. If there isn't a Service
      // Account with this ID, then create that as a child of that Statement
      // Account.
      for (Account_Import__c ai : importMap.values())
      {
        statementAccountIds.add(ai.CUS_Number__c);
        serviceAccountIds.add(ai.Service_Acct_Nbr__c);
      }
    
      // Get back a list of Statement Accounts.
      getStatementAccounts();
        
      // Look at each record and decide whether a matching
      // Statement Account exists.
      Account localAccount;
      List<Account> newAccounts = new List<Account>();
      List<Account> oldAccounts = new List<Account>();
      for (Account_Import__c ai : importMap.values())
      {
        localAccount = statementMap.get(ai.CUS_Number__c);
        if (localAccount == null)
        {
          newAccounts.add(new Account(Name = ai.CUS_Number__c, 
            RecordTypeId = statementRecordType));
          statementAccountIds.add(ai.CUS_Number__c);
        }
        else
        {
          // We don't bother to update anything because there's nothing to update.
        }
      }
      if (newAccounts.size() > 0)
      {
        insert newAccounts;
        // Refresh this list.
        getStatementAccounts();
      }
  
      // Get back a list of Service Accounts.
      getServiceAccounts();

      // Look at each record and decide whether a matching
      // Service Account exists.
      newAccounts = new List<Account>();
      localAccount = new Account();
      Account localStatement = new Account();
      for (Account_Import__c ai : importMap.values())
      {
        // TODO: Make sure we are mapping the statement ID to the service ID!
        localAccount = serviceMap.get(ai.Service_Acct_Nbr__c);
        localStatement = statementMap.get(ai.CUS_Number__c);
        if (localAccount == null && localStatement != null)
        {
          // Insert new Service Account.
          localAccount = new Account(
            RecordTypeId = serviceRecordType,
            ParentId = localStatement.Id,
            Name = ai.Service_Acct_Nbr__c,
            First_Name__c = ai.CUS_First_Name__c,
            Last_Name__c = ai.CUS_Last_Name__c,
            Phone = ai.CUS_Permanent_Phone_Nbr__c,
            ShippingCity = ai.City__c,
            ShippingState = ai.State__c,
            ShippingPostalCode = ai.Postal_Code__c,
            Community_Code__c = ai.Community_Code__c);
          // Some fields are transformed along the way.
          // Assemble the street address.
          localAccount.ShippingStreet = 
            ((ai.House_Street_Numb__c == null ? '' : ai.House_Street_Numb__c + ' ') +
            (ai.Street_Name__c == null ? '' : ai.Street_Name__c + ' ') +
            (ai.House_Street_Numb_Suffix__c == null ? '' : ai.House_Street_Numb_Suffix__c)).trim();
          // Set the status.
          localAccount.Status__c = getStatus(ai.ACT_Status__c);
          // Set the service type.
          localAccount.Service_Type__c = getServiceType(ai.ACT_Service_Type__c); 
          // Remember to insert it!
          newAccounts.add(localAccount);
          serviceAccountIds.add(ai.Service_Acct_Nbr__c);
        }
        else
        {
          // Update Service Account with this information.
          localAccount.First_Name__c = ai.CUS_First_Name__c;
          localAccount.Last_Name__c = ai.CUS_Last_Name__c;
          localAccount.Phone = ai.CUS_Permanent_Phone_Nbr__c;
          localAccount.ShippingCity = ai.City__c;
          localAccount.ShippingState = ai.State__c;
          localAccount.ShippingPostalCode = ai.Postal_Code__c;
          localAccount.Status__c = getStatus(ai.ACT_Status__c);
          localAccount.Service_Type__c = getServiceType(ai.ACT_Service_Type__c); 
          localAccount.Community_Code__c = ai.Community_Code__c;
          localAccount.ShippingStreet = 
            ((ai.House_Street_Numb__c == null ? '' : ai.House_Street_Numb__c + ' ') +
            (ai.Street_Name__c == null ? '' : ai.Street_Name__c + ' ') +
            (ai.House_Street_Numb_Suffix__c == null ? '' : ai.House_Street_Numb_Suffix__c)).trim();
          oldAccounts.add(localAccount);
        }
      }
      if (newAccounts.size() > 0)
      {
        insert newAccounts;
      }
      if (oldAccounts.size() > 0)
      {
        update oldAccounts;
      }
      getServiceAccounts();

      // Get a list of existing Contacts for these Service Accounts.
      Set<Id> accountIds = new Set<Id>();
      for (Account a : services)
      {
        accountIds.add(a.Id);
      }
      list<Contact> contacts =
        [SELECT Id, 
            Name,
            AccountId,
            Account.Name, 
            FirstName, 
            LastName, 
            MailingStreet, 
            MailingCity,
            MailingState,
            MailingPostalCode,
            Phone,
            Email
           FROM Contact
          WHERE AccountId IN :accountIds
          ORDER BY LastName, FirstName];
      map<String, Contact> contactMap = new map<String, Contact>();
      for (Contact c : contacts)
      {
        contactMap.put(c.LastName + (c.FirstName == null ? '' : c.FirstName), c);
      }
          
      // Loop through all import records and see if there
      // is a matching Contact record for each Service Account.
      // If not, create it.
      Contact localContact;
      List<Contact> newContacts = new List<Contact>();
      List<Contact> oldContacts = new List<Contact>();
      for (Account a : services)
      {
        localContact = contactMap.get((a.Last_Name__c == null ? '' : a.Last_Name__c) + 
          (a.First_Name__c == null ? '' : a.First_Name__c));
        if (localContact == null)
        {
          localContact = new Contact(
            AccountId = a.Id,
            FirstName = a.First_Name__c,
            LastName = a.Last_Name__c,
            Phone = a.Phone,
            Email = a.Email__c,
            MailingStreet = a.ShippingStreet,
            MailingCity = a.ShippingCity,
            MailingState = a.ShippingState,
            MailingPostalCode = a.ShippingPostalCode);
          newContacts.add(localContact);
        }
        else
        {
          localContact.Phone = a.Phone;
          localContact.Email = a.Email__c;
          localContact.MailingStreet = a.ShippingStreet;
          localContact.MailingCity = a.ShippingCity;
          localContact.MailingState = a.ShippingState;
          localContact.MailingPostalCode = a.ShippingPostalCode;
          oldContacts.add(localContact);
        }
      }
      if (newContacts.size() > 0)
      {
        insert newContacts;
      }
      if (oldContacts.size() > 0)
      {
        update oldContacts;
      }
    }
    catch (Exception ex)
    {
      // Update the import record with any errors.
      system.debug(ex);
    }
  }
  
  private static String getServiceType(String val)
  {
    String retVal = serviceTypeMap.get(val);
    if (retVal == null || retVal == '') retVal = val;
    return retVal;
  }
  
  private static String getStatus(String val)
  {
    String retVal = statusMap.get(val);
    if (retVal == null || retVal == '') retVal = val;
    return retVal;
  }

  private static void getStatementAccounts()
  {
    statements = 
      [SELECT Id, Name, RecordTypeId
         FROM Account
        WHERE Name IN :statementAccountIds];
    statementMap = new Map<String, Account>();
    for (Account a : statements)
    {
      statementMap.put(a.Name, a);
    }
  }
  
  private static void getServiceAccounts()
  {
    services =  
      [SELECT Id, 
          Name,
          ParentId,
          Parent.Name,
          RecordTypeId,
          First_Name__c,
          Last_Name__c,
          Status__c,
          Service_Type__c,
          BillingStreet,
          BillingCity,
          BillingState,
          BillingPostalCode,
          BillingCountry,
          ShippingStreet,
          ShippingCity,
          ShippingState,
          ShippingPostalCode,
          ShippingCountry,
          Email__c,
          Phone
         FROM Account
        WHERE Name IN :serviceAccountIds];
    serviceMap = new Map<String, Account>();
    for (Account a : services)
    {
      serviceMap.put(a.Name, a);
    }
  }
}

 

PhryPhry

Singular Label

Account Import

Description

Account Imports; allows Data Loaders to import all data in one pass; errors (if any) are stored on the imported record; once a record is completely imported, it is deleted; the field names for this object are based on the originating CommSoft field names; the mapping between these original fields and Salesforce fields are found in the accountImportAfterInsertUpdate trigger

Plural Label

Account Imports

Enable Reports

 

Object Name

Account_Import

Track Activities

 

API Name

Account_Import__c

Track Field History

 

 

 

Deployment Status

Deployed

 

 

Help Settings

Standard salesforce.com Help Window

 

 

 Account_import_c object