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 

trigger not inserting record


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
Suraj Tripathi 47Suraj Tripathi 47

Hi Shivi,

I go through your code and I found that you are inserting Opportunity on Before Trigger

I want to tell you that how you can query Account on the basis of the name As in Before Trigger Name is not saved in Org.

So You should use After Insert then Your code would work fine.

 Please mark it as Best Answer if your query is solved.

https://developer.salesforce.com/forums/?id=906F000000090jjIAA You can take reference from this Link also

Thank you

Andrew GAndrew G
@suraj that reference relates to Before Triggers not having access to the Trigger.Old component.  Trigger Old is not referenced in the code nor does the code attempt to reference the Opp Id before it has been correctly inserted.

@shivi
I would guess that you are not invoking your method to insert the actual Account records.   It seems to be a method sitting within another method but not being invoked.


regards
Andrew