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
Tanmay SahaiTanmay Sahai 

Some help with Trigger

Hi All,

I have created a trigger to automate my opportunity creation from Event. The trigger is working fine but the related contact of the created opportunity doesn't get attached to it under Contact Role( Related Contact) but as per standard process, if an opportunity is created from the contact, the opportunity shows the related to Contact.

How can I achieve this above requirement by making the change in the below trigger code:

trigger EventTrigger on Event (after update)
{

    List<opportunity> opportunities = new List<opportunity>();
    List<Event> eventToprocessList = new List<Event>();
    String eventType;
    List<String> oppEventIdList = new List<String>();
    
    for(Event event :Trigger.new)
    {
        eventType = event.Event_Type__c;
                if(!event.Event_Type__c.containsIgnoreCase('Demo') || event.Demo_Status__c != 'Complete')continue;
    
        eventToprocessList.add(event);
        oppEventIdList.add(event.Id);
     }
   
       Set<String> eventIdSet = new Set<String>();
       for(Opportunity opp : [SELECT Id, Event_ID__c FROM Opportunity WHERE Event_ID__c = : oppEventIdList])
       {
           eventIdSet.add(opp.Event_ID__c);
       }

    for(Event event : eventToprocessList)
    {
    
        if(!eventIdSet.IsEmpty() && eventIdSet.contains(event.Id)) continue;
        
      
        //Creating opppty
        opportunity opp = new opportunity();
            opp.Name = event.Subj_del__c + ' ' + '| RP |'+ ' ' + event.Seats__c + ' ' + 'Seats';
            //Name = 'Test oppp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
            opp.CloseDate = Date.Today()+ 90;
            opp.Type = 'New Business';
            //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
             opp.Notes_del__c = event.Notes__c;
            //fill other mandatory fields comma seperated as shown above
            //opp.AccountId = event.WhatId;
            opp.AccountId = Trigger.newMap.get(event.Id).AccountId;
            //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
            opp.StageName = 'Pre-Qualified';
            opp.Referring_SDR__c= event.OwnerId;
            opp.Contact_Name__c = event.Related_Contact_Name__c;
            opp.opportunity_Owner__c= event.Assigned_AE__c;
            opp.Amt__c= event.Amount__c;
            opp.Seat_Number__c= event.Seats__c;
            opp.LeadSource= event.Lead_Source__c;
            opp.New_Lead_Source__c= event.New_Lead_Source__c;
            //opp.LeadSource = Trigger.newMap.get(event.WhoId).Lead_Source__c;
            //opp.New_Lead_Source__c= Trigger.newMap.get(event.WhoId).New_Lead_Source__c;
            opp.Contact_Name__c = event.WhoId;
            opp.Event_ID__c = event.Id;
            //opp.Account_Name__c = Trigger.newMap.get(event.WhoId).Account;
        
         opportunities.add(opp);
    }
    system.debug('List of Records to be Created: '+opportunities);
    
    if(opportunities.size()>0)
    {
        insert opportunities;
    }
  }

Look forward for your responses. Thanks is advance!