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
ChienChien 

change account name before insert and update

Hi there,

I am trying to make the account name unique for all the contacts that are imported. So, all the contacts go under one account. Here is my code:

trigger addGroup_tgr on Contact bulk (before insert, before update) {
      Map<String, Contact> contactMap = new Map<String, Contact>();
      for (Contact contact : System.Trigger.new) {
          if (contact.E_Mail__c != NULL){
              //string email = contact.E_Mail__c.trim();
              //if (contact.Email != email){
              //      contact.Email = email;
              //}
              contact.Email = contact.E_Mail__c.trim();
          }

          if (contact.Group__c != NULL){
              String newGroup = contact.Group__c.trim();
              if (Trigger.isupdate) {
                  String oldGroup = System.Trigger.oldMap.get(contact.Id).Group__c;
             
                  if (oldGroup != NULL){
                      String[] result = oldGroup.split('/');
                      Boolean isDuplicate = false;
                 
                      for (String t : result){
                          if (t == newGroup){
                              isDuplicate = true;
                          }
                      }
                 
                      if (isDuplicate != true && oldGroup != newGroup){
                          contact.Group__c = oldGroup + '/' + newGroup;
                      }else if (isDuplicate = true){
                          contact.Group__c = oldGroup;
                      }
                  }else{
                      contact.Group__c = newGroup;
                  }
             
              }else if (Trigger.isinsert){
                  contact.Group__c = newGroup;
              }
         
              if (newGroup == 'delete all'){
                  contact.Group__c = '';
              }
          }else{
              if (Trigger.isupdate){
                  contact.Group__c = System.Trigger.oldMap.get(contact.Id).Group__c;
              }
          }

          contact.Account.Name = 'CEOI Contacts';
          //contact.firstname = 'meow';
      }
}

However, I got the following error message:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger addGroup_tgr caused an unexpected exception, contact your administrator: addGroup_tgr: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.addGroup_tgr: line 49, column 11

Anybody can help?
Thanks a lot


Message Edited by Chien on 08-22-2008 07:50 AM

Message Edited by Chien on 08-22-2008 07:51 AM

Message Edited by Chien on 08-22-2008 08:14 AM
JimRaeJimRae

There must be more to it than that.  You are only showing a 3 line trigger, but the error is on line 49.  Can you post all of the code for your trigger?  Also, remember to use the SRC button so the code is displayed correctly.

 

Jim

ChienChien
Hi Jim,

Thanks for the quick response. Other lines of the trigger have nothing to do with this error. Anyway, I've posted the complete code of the trigger.

Thanks,
Sam
JimRaeJimRae
Actually, I think the problem is elsewhere in the code.

I don't think you can set the Account name like that (Line 49) on the Contact record.  what you should be doing is seting the AccountID field for the 'CEOI Contacts' Account.  Something like this (line 50/51):

Code:
trigger addGroup_tgr on Contact bulk (before insert, before update) {
      Map<String, Contact> contactMap = new Map<String, Contact>();
      for (Contact contact : System.Trigger.new) {
          if (contact.E_Mail__c != NULL){
              //string email = contact.E_Mail__c.trim();
              //if (contact.Email != email){
              //      contact.Email = email;
              //}
              contact.Email = contact.E_Mail__c.trim();
          }

          if (contact.Group__c != NULL){
              String newGroup = contact.Group__c.trim();
              if (Trigger.isupdate) {
                  String oldGroup = System.Trigger.oldMap.get(contact.Id).Group__c;
              
                  if (oldGroup != NULL){
                      String[] result = oldGroup.split('/');
                      Boolean isDuplicate = false;
                  
                      for (String t : result){
                          if (t == newGroup){
                              isDuplicate = true;
                          }
                      }
                  
                      if (isDuplicate != true && oldGroup != newGroup){
                          contact.Group__c = oldGroup + '/' + newGroup;
                      }else if (isDuplicate = true){
                          contact.Group__c = oldGroup;
                      }
                  }else{
                      contact.Group__c = newGroup;
                  }
              
              }else if (Trigger.isinsert){
                  contact.Group__c = newGroup;
              }
          
              if (newGroup == 'delete all'){
                  contact.Group__c = '';
              }
          }else{
              if (Trigger.isupdate){
                  contact.Group__c = System.Trigger.oldMap.get(contact.Id).Group__c;
              }
          }

          //contact.Account.Name = 'CEOI Contacts';
          contact.AccountId='0017000000np0sAAB'; //id for CEOI Contact account
          //or
          //contact.AccountId = [select A.id from Accounts A where A.Name = 'CEOI Contact' Limit 1].ID;
          //contact.firstname = 'meow';
      }
}

 

Jim
ChienChien
Hi Jim,

Thank you so much. That works.

Sam