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 

help me in trigger to create lookup

Scenario 
I want to create contact lookup(Notification_Receipient_Email_c) on opportunity object from email field(Notification_Receipient_Contact_Emailc) and if email doesnt exist then first create contact and assign it email and then create// its lookup.And created contact name should be displayed in Notification_Receipient_Name_c field.

My trigger is creating lookup and if email exist or doesnt exist ,it is creating a contact and then creating its lookup except that it is not displaying the contact name in my  Notification_Receipient_Name__c

Example for when contact exist:-

Notification_Receipient_Contact_Email__c
- shivi1@sf.com //this email exist

Notification_Receipient_Email__c-shivani//lookup created with the contact name where email exist

Notification_Receipient_Name__c
         //novalue (expected value-shivi)

Exame for when contact not exist:-

Notification_Receipient_Contact_Email__c- test@chk.com //this email not exist

Notification_Receipient_Email-test//lookup created by creating its contact

Notification_Receipient_Name
         //novalue (expected value-test
Plz help me out how to display name in above field as well...how to incorporate that logic


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

handler class
public static void InsertContactEmail(List<Opportunity> oppList){
        
        //map<string,id> mapOpptytocontactEmail = new map<string,id>();
        map<string,id>maptoUpdateoppty = new map<string,id>();
       
        
        set<string> getemail= NEW set<string>();  
        
        for(Opportunity op : opplist){
            system.debug('opp details' + op);
            if(op.Notification_Recipient_Contact_Email__c != null){
                
                getemail.add(op.Notification_Recipient_Contact_Email__c);
                
            } 
        }
        
        
        //for existing one
        for(Contact con :[Select id,npe01__HomeEmail__c from contact where npe01__HomeEmail__c IN :getemail]){
            system.debug(con.npe01__HomeEmail__c + 'inside existing map');
            maptoUpdateoppty.put(con.npe01__HomeEmail__c,con.Id);
        } 
        
        //for non existing    
        List<contact> conUpd = new List<Contact>();
        
        for(String str : getemail){
            system.debug(str);
            if(!maptoUpdateoppty.containsKey(str)) {
                List <String> name = str.split('@', 2);
                conUpd.add(new Contact(Lastname=name[0],npe01__HomeEmail__c=str));
            }
        }
        
        if(conUpd.size() > 0){
            insert conUpd;
        }
        
        for(Contact con : conUpd){
            if(!maptoUpdateoppty.Containskey(con.npe01__HomeEmail__c)){
                system.debug(con.npe01__HomeEmail__c + 'inside non=existing');
                maptoUpdateoppty.put(con.npe01__HomeEmail__c,con.Id);
               
                
            }
            
        }
        
        for(Opportunity op : opplist){
            if(op.Notification_Recipient_Contact_Email__c != null){
                op.npsp__Notification_Recipient_Contact__c = maptoUpdateoppty.get(op.Notification_Recipient_Contact_Email__c);
                
            }
        }    
        
        plz help me out