• jreeh
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies

Hi,

 

I have an apex trigger on Case object that is supposed to automatically associate Cases to Contact records based on the AccountId field and its properties.

My trigger works well when I update existing Cases, but it throws the "attempt to de-reference a null object" error whenever I create a new Case.

 

This trigger first looks at the Email__c field set on the Account associated (Case.Account.Email__c) and tries to find existing contacts with that email address. If it didn't find a contact with that email address, it creates a Contact with some account info and associates it with the Case accordingly. If it did find contacts with that email address, it will query for them and associate them with the Case.

 

Here is my code (the line causing the error is highlighted in red):

 

trigger caseNSFBefore on Case (before insert, before update) {
    List<String> emailAddresses = new List<String>();
    List<String> accountIds = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.AccountId != null)
        {
            accountIds.add(caseObj.AccountId);
            System.debug('AccountEmail*********' + accountIds);
            List<Account> listAccounts = [Select Id,Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds];
            for (Account a :listAccounts){
            emailAddresses.add(a.Email__c);
            System.debug('AccountEmail*********' + emailAddresses);
            }

        }
    }
    //fetch all the Account records from database
    Map<Id, Account> mapAccounts = new Map<Id, Account>([Select Id, Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds]);

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    List<Contact> listContacts = [Select Id,Email From Contact Where Email in :emailAddresses];
    Set<String> takenEmails = new Set<String>();
    for (Contact c:listContacts) {
        takenEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
    List<Case> casesToUpdate1 = new List<Case>();



    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.AccountId !=null &&
           !takenEmails.contains(mapAccounts.get(caseObj.AccountId).Email__c))
        {
                List<Account> listAccounts = [Select Id,Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds];
                for (Account a :listAccounts){
                    
                Contact cont = new Contact(FirstName= a.firstName__c,
                                            LastName= a.lastName__c,
                                            AccountId = a.Id,
                                            Phone = a.primePhone__c,
                                            Email= a.Email__c);
                emailToContactMap.put(mapAccounts.get(caseObj.AccountId).Email__c,cont);
                casesToUpdate.add(caseObj);
                
                }
        } else {
        casesToUpdate1.add(caseObj);    
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(mapAccounts.get(caseObj.AccountId).Email__c);
        
        caseObj.ContactId = newContact.Id;
    }
        for (Case caseObj:casesToUpdate1) {
        Contact existingContact = [select id from Contact where Email = :mapAccounts.get(caseObj.AccountId).Email__c  limit 1];        
        caseObj.ContactId = existingContact.Id;
    }
}

 

 

I would appreciate your help.

 

 

Thanks,

 

  • November 09, 2012
  • Like
  • 0

I am trying to get count of all Accounts which includes certain keyword present (multi-pick list).  But I get the following error:

 

System.LimitException: Too many SOQL queries: 101

 

I'm fairly new to Apex code.  The problem is that I am running SOQL inside a FOR loop, but I'm not quite sure how to resolve it. Apex Code is as below.  Any help would be appreciated.  Thanks.

 

 

public String getType() {
return type;
}
public void setType(String type){
this.type= type;
}

public Integer getTotal() {
return total;
}
public void setTotal(Integer total){
this.total= total;
}

public Integer getValid() {
return valid;
}
public void setValid(Integer valid){
this.valid= valid;
}

}

 

 

public List<phonesterDat> getCompanyData() {
List<phonesterDat> var1 = new List<phonesterDat>();
for (Keyword__c keyword : [select kw__c from Keyword__c]) {
phonesterDat var = new phonesterDat();
String str = keyword.kw__c;
var.setType(str);
var.setTotal([select count() from Account where DD_Segment__c includes (:str)]);
var1.add(var);
}
return var1;
}

 

If you want to whole code base. It is available here http://pastie.org/5151131

trigger IncrementData on IB_Data__c (before insert) {
    List<IB_Data__c> objOldI = [select id,IncrementNO__c from IB_Data__c orderby createddate desc  limit 1];
    for(IB_Data__c objI : trigger.new){
        if(objOldI.size()>0){
            if(objOldI[0].IncrementNo__c != null){
                objI.IncrementNo__c = objOldI[0].incrementNo__c + 1;
                
            }
        }
        else{
            objI.incrementNo__c = 1;
            
        }
    }
}

 

 

Hi guys i have written above trigger. Its working fine with record by recod insertion. But when i import the bulk data trhough data loader its not working and showing incrementNo__c is only '1' for all the records. But as per my requirement based on record count i have to increase this number.

 

In this situation all the records that are imported by data loader is having the same createddate and time. So its showing 1 for all the records. How can i increase this number when i import the data through data loader for all the records imported.

 

Please let me give your suggestions.

Hi I have a list of records that i am displaying in visual force page like below

 

code        Name

 

A001         Test1

A002          Test2

A003          Test3

 

 

Is there any possibility to make a text as bold from the apex class. Suppose for "A002", can i show this value in bold text??