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
shivi Tanwarshivi Tanwar 

map text field to lookup field


i want to populate my Notification Recipient Contact(npsp__Notification_Recipient_Contact__c) which is lookup to contact with my custom text field Notification_Recipient_Contact_text__c on opportunity object.

trigger opportunityTrigger on Opportunity (after insert,before insert,before update) {
    
    If (trigger.isBefore && (trigger.isUpdate ||  Trigger.isInsert)) {
         OpportunityTriggerHandler.afterUpdateOpportunity(Trigger.New);
    }
    
       /*else {
                     OpportunityTriggerHandler.afterInsertOpportunity(Trigger.new);
        }
    */
        
}

my handler class
public with sharing class OpportunityTriggerHandler {
/*    public static void afterInsertOpportunity(List<Opportunity> opplst) {
        set<Id> setOfOppIds = new set<Id>();
        for(Opportunity objopp : opplst){
            setOfOppIds.add(objopp.Id);
        }
       if(setOfOppIds != null && setOfOppIds.size() >0){
           system.enqueueJob(new QuickBookIntegrationQueueable(setOfOppIds));
       }
    }
*/
    
    public static void afterUpdateOpportunity(List<Opportunity> opptList){
        Map<String,Opportunity> MapContactName = new Map<String,Opportunity>();
        for(Opportunity opp: opptList){
            MapContactName.put(opp.Notification_Recipient_Contact_text__c,opp);
        }
        List<Contact> conList = [SELECT id,LastName from Contact where LastName
                                in:MapContactName.values()];
        for(Contact con : conList){
            MapContactName.get(con.LastName).npsp__Notification_Recipient_Contact__c = con.ID;
            system.debug('fired');
            
        }
        
    }
    
}

 
PriyaPriya (Salesforce Developers) 

Hi Shivi,

1. Please specify the error you are receiving. 
2. Also I see that in the Handler class under method 'afterUpdateOpportunity' , you are running SOQL on contact but in criteria value, you are setting the list of Opportunity Value instead list of last names as shown below :- 
where LastName in:MapContactName.values()];
3. we suggest to use MapContactName.keyset()

 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan