• shivi Tanwar
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
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
Facing issue in creating test class

My trigger will create custom account  lookup from text field on contact object and if account doesnt exist it will first create account then creates lookup.
I am beginner and i am facing issue in creating test class plz help me out.

Here is my trigger
public class DonarMappingFeildContactHandler {
    
    
    public static void CreatePrimaryAffiliation(List<Contact> conList){
        
        Map<String,Id> conAccExist = new Map<String, Id>();
        Map<String,Contact> mapVal = new Map<String,Contact>();
        Set<String> fieldVal = new Set<String>();
        List<Account> newAccList = new List<Account>(); 
        
        try{
        for(Contact con : conList)
        {  //system.debug('contact detail'+ con);  
         if(con.Primary_affliation_text__c != null) {
           system.debug('check primary aff' + con.Primary_affliation_text__c);
            mapVal.put(con.Primary_affliation_text__c, con);
            fieldVal.add(con.Primary_affliation_text__c);
            system.debug('value entered' + con.Primary_affliation_text__c);
         }
        } 
        if(fieldVal != null && fieldVal.size() > 0)
        {
            for(Account acc: [SELECT Id,Name FROM Account WHERE Name In :fieldVal])
            {
                conAccExist.put(acc.name, acc.Id);
            }
            
            for(String str: fieldVal)
            {
                if(!conAccExist.containsKey(str))
                {
                    Account acc = new Account();
                    acc.Name = str;
                    
                    newAccList.add(acc);
                    
                }
            }system.debug('matc value'+ conAccExist);
            
            if(newAccList != null && newAccList.size() > 0)
            {
                insert newAccList;
             
             for(Account accObj : newAccList)
             {
                 if(!conAccExist.containsKey(accObj.Name)){
                     conAccExist.put(accObj.Name, accObj.Id);
                 }
             }
             system.debug('matc value 2'+ conAccExist);
            }
            
            for(Contact cObj : conList){
                if(String.isNotBlank(cObj.Primary_affliation_text__c)){
                    cObj.npsp__Primary_Affiliation__c = conAccExist.get(cObj.Primary_affliation_text__c);
                }
            }      
        }
       }  
        catch(Exception e){
            system.debug('error in rigger');
        }
    }
    
}

my trigger 
trigger DonarMappingFieldContactTrigger on Contact (before insert) {
    DonarMappingFeildContactHandler.CreatePrimaryAffiliation(Trigger.new);
     
}

my test class
@isTest
public class contactTriggerTest {
    
    public static testmethod void testPrimaryAffliation() {
        List<Account> acctlist = new List<Account>();
        Account acct = new Account(name='acct1');
        insert acctList;
        
        List<Contact> conList = new List<Contact>();
        Contact con = new Contact();
        con.LastName = 'newCont';
        con.npsp__Primary_Affiliation__c = acct.Id;
        con.Primary_affliation_text__c = 'acct1';
        insert conlist;
        
        test.startTest();
        DonarMappingFeildContactHandler.CreatePrimaryAffiliation(conlist);
        test.stopTest();
    }
}

it is giving me only 25% covergae plz help me out

I have created a trigger that will autopopulate Lookup field(Account Name,npsp__Notification_Recipient_Contact__c) by mapping custom fields (My_Account_Name__c,npe01__HomeEmail__c) respectively on opportunity object.
My Account Name(My_Account_Name__c) ---> Account Name(Account Lookup ) Myemail(npe01__HomeEmail__c) ---> npsp__Notification_Recipient_Contact__c(Contact Lookup)
i WANT TRIGGER SHOULD ONLY WORKS ON WHILE INSERTING RECORD.hENCE i USED BEFORE INSERT.But, trigger only creating a lookup to email field not for account field .I want tit shold create lookup to both fields i.e account as weell as contact field.

here is my code

trigger opportunityTrigger on Opportunity (after insert,before insert) {

If (trigger.isBefore && trigger.isInsert) {
      OpportunityTriggerHandler.InsertContactEmail(Trigger.New); }
/* else { OpportunityTriggerHandler.afterInsertOpportunity(Trigger.new); } */
}

trigger 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 InsertContactEmail(List<Opportunity> opptList){
        // below code creates contact email lookup
        Map<String,Opportunity> MapContactName = new Map<String,Opportunity>();
        for(Opportunity opp: opptList){
            MapContactName.put(opp.My_Email__c,opp);
        }
        List<Contact> conList = [SELECT id,npe01__HomeEmail__c from Contact where npe01__HomeEmail__c
                                in:MapContactName.keySet()];
        for(Contact con : conList){
            MapContactName.get(con.npe01__HomeEmail__c).npsp__Notification_Recipient_Contact__c = con.ID;
           
        }
            
       
        // below code creates account lookup
         public static void InsertAccountName(List<Opportunity> opptList){
         Map<String,Opportunity> MapAccountName = new Map<String,Opportunity>();
        for(Opportunity opp: opptList){
            MapAccountName.put(opp.My_Account_Name__c.toLowerCase(),opp);
               System.debug('MapAccountName: ' + MapAccountName); 
        }
        List<Account> accList = [SELECT id,Name from Account where Name
                                in:MapAccountName.keySet()];
       
        for(Account acc : accList){
            MapAccountName.get(acc.Name.toLowerCase()).AccountId = acc.ID;
            System.debug('accList: ' + accList);
        }
  }
}
can anyone help me why it is not creating lookup to account 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');
            
        }
        
    }
    
}

 
Hello,

I have a basic trigger that needs to update a lookup field (Discount_code_LU__c). I'm just trying to map the lookup field with the text field (Discount_code__c). The lookup field (Discount_code_LU__c) is related to the Product object. Any assistance will be greatly appreciated!

Thank you!!
trigger UpdateOpportunityProduct on Opportunity (before insert){
    
 for(Opportunity opp:Trigger.new)
 {
     opp.Discount_code_LU__c =  opp.Discount_code__c ;
          
}
}

 
  • February 08, 2017
  • Like
  • 0