+ Start a Discussion
Nichole SmithNichole Smith 

Comparing 2 values and populating a lookup

I have a custom object that we record calls in. Once a new call is created the phone label field, .IBP__PhoneLabel__c, is filled in with the unique external id of an Account and the name of the call as such: 452226- Outbound Call 3/18/2016. I created a trigger to pull the unqiue id from this phone label, 452226, and use that to match the unique id field on the Account, SPID__c, and once there was a match I was asscoiate the call record to the Account via the lookup field called, Account__c. Here is my trigger. There is no errors but the account isnt being populated. What am I missing?

trigger CallMatch on IBP__Ifbyphone_Call__c (before insert) {
 Map<String,id> SPIDS = new Map<String,id>();
    for(Account spid: [select id,SPID__c from Account]){
       SPIDS.put(spid.SPID__C,spid.id);
    }
    
    for(IBP__Ifbyphone_Call__c dt :trigger.New){
      if(dt.IBP__PhoneLabel__c != null){
       string spid1 = dt.IBP__PhoneLabel__c.substring(dt.IBP__PhoneLabel__c.indexOf('[')+1, dt.IBP__PhoneLabel__c.indexOf('-'));
        if(SPIDS.containsKey(spid1)){
            dt.Account__c = SPIDS.get(spid1);
        }
      }
        else{
            dt.Account__c = null;
        }
    }
}
James LoghryJames Loghry
Simply put, the spid1 key does not exist in your "SPIDS" map.  It looks to me like your storing the Account external Id (452226 in your example) as the map key, but you're actually looking to see if the map contains the PhoneLabel.  Is PhoneLabel supposed to match the Account external Id?  If not, you've found your problem.
Nichole SmithNichole Smith
The phone label will have the account external id in the first part of the string. So I tried to use the substring features to pull out the external id from the phone label field to match up with the map key.