+ Start a Discussion
jpbenjpben 

Trigger to Update Lookup field

Hi All,

 

I need help with my trigger.  I have an Account with a lookup relationship to a custom object (Practice_Location__c).  I want to auto populate the lookup field (Practice_Location__c) on the Account after creating a Practice Location record.  Account and Practice_Location__c objects have NPI_Number__c (short text data type) field in common, which I am using to map.  My trigger below is not updating the Practice_Location__c field on the Account.  Please advice. 

 

 

 

trigger SoloPractice on Practice_Location__c (after insert, after update) {


if (Trigger.isInsert){
map<String, ID> mapnpi = new map<String, ID>();

// loop through trigger and add NPI and Practice Location ID to map
for (Practice_Location__c p : trigger.new){
if (p.Type_of_Location__c == 'Solo Practice'&& p.Status__c == 'Active'){
mapnpi.put(p.NPI_Number__c,p.Id);

}
}
// create a map of Doctors (Account)
map<String, Account> accts = new map<String, Account>([
Select Id, Practice_Location__c, NPI_Number__c from Account Where RecordTypeId = '01I700000001xE2' AND NPI_Number__c in :mapnpi.keyset()
]);

// iterate over the list of Doctors (Account) and assign the Practice_Location__c
for (Account acct : accts.values()) {
acct.Practice_Location__c = mapnpi.get(acct.NPI_Number__c);
}
update(accts.values());

}
}

prady-cmprady-cm

Are you getting any error or just not updating.

 

change the following

// iterate over the list of Doctors (Account) and assign the Practice_Location__c
for (Account acct : accts.values()) {
acct.Practice_Location__c = mapnpi.get(acct.NPI_Number__c);
}
update(accts.values());

 

 

to

List<account> ac = new List<Account();

// iterate over the list of Doctors (Account) and assign the Practice_Location__c
for (Account acct : accts.values()) {
acct.Practice_Location__c = mapnpi.get(acct.NPI_Number__c);

ac.add(acct);
}
update(ac);

 

 

Let me know if this works