+ Start a Discussion
SF API 9SF API 9 

Trigger that Excludes Null Values

I grabbed the trigger below from a website but it keeps comparing null values on the Lead object to null values on the Account object. 

Basically, I just want to make sure that when Addy_Account_Id__c is entered on the Lead object, it checks if there are any Accounts with a matching Addy_Account_Id__c. If they do match, the SFDC Account Id should be stamped on the Lead record in the "Convert_To_Existing_Account__c " field. 

However, this trigger is matching based on null values. I would like to exclude those. Please Help!!!
 
trigger pullSFDCaccountonlead on lead (before update) {

    Set<String> codes = new Set<String>();
    for (lead o : trigger.new) codes.add(o.Addy_Account_Id__c );

    Map<String, Account> siteAccounts = new Map<String, Account>();
    for (Account a : [
        SELECT Addy_Account_Id__c FROM Account
        WHERE Addy_Account_Id__c IN :codes
    ]) siteAccounts.put(a.Addy_Account_Id__c , a);

       for (lead o : Trigger.new)
    {
        Account siteAccount = siteAccounts.get(o.Addy_Account_Id__c );
       Id parentId = (siteAccount != null) ? null : siteAccount.Id;
        o.Convert_To_Existing_Account__c = parentId;
    }
}

 
Krishna.AvvaKrishna.Avva
Hi,

You do not need to loop through trigger.new in line 12. Use the Set to loop through and use the Map to assign the SiteAccout. 

Regards,
Krishna Avva
MagulanDuraipandianMagulanDuraipandian
The issue is with the below code
Id parentId = (siteAccount != null) ? null : siteAccount.Id;

Change it as below

Id parentId = (siteAccount == null) ? null : siteAccount.Id;
--
Magulan Duraipandian
www.infallibletechie.com
SF API 9SF API 9
Snap! That is actually my attemp to fix the code. The original is:
 
trigger pullSFDCaccountonlead on lead (before update) {

    Set<String> codes = new Set<String>();
    for (lead o : trigger.new) codes.add(o.Addy_Account_Id__c );

    Map<String, Account> siteAccounts = new Map<String, Account>();
    for (Account a : [
        SELECT Addy_Account_Id__c FROM Account
        WHERE Addy_Account_Id__c IN :codes
    ]) siteAccounts.put(a.Addy_Account_Id__c , a);

       for (lead o : Trigger.new)
    {
        Account siteAccount = siteAccounts.get(o.Addy_Account_Id__c );
       Id parentId = (siteAccount == null) ? null : siteAccount.Id;
        o.Convert_To_Existing_Account__c = parentId;
    }
}

When i change it back to "Id parentId = (siteAccount == null) ? null : siteAccount.Id;" - my code continues to match on  blanks. If "Addy_Account_Id__c" is null on the Lead object, there is no need for this trigger to fire. Do you know how I can update this? 
 
Deepali KulshresthaDeepali Kulshrestha
Hi ,

Just put this check where you are putting the parent Id:

 Account siteAccount = siteAccounts.get(o.Addy_Account_Id__c );
       Id parentId = (siteAccount == null) ? null : siteAccount.Id;
       if(parentId != null)
        o.Convert_To_Existing_Account__c = parentId;
    }
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com