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 

I want some help with the trigger to automate custom values on Opportunity creation

Hello All,

I have custom picklist Status on the Event page layout and I am using one of its value to automate Oppty creation using a Trigger. So, when the Status=Complete, it creates an opportunity. Also, the event type = Demo. So the condition is that when my event is Demo(custom picklist value) and its Status is Complete, oppty gets created.
Below is the trigger:

trigger EventTrigger on Event (after update){
    List<Opportunity> opportunities = new List<Opportunity>();

    for(Event event :Trigger.new){
        if(event.Demo_Status__c == 'Complete'){
            Opportunity opp = new Opportunity
                Name = '| RP |' + event.Seats__c,
                //Name = 'Test Opp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
                CloseDate = Date.Today()+ 90,
                Type = 'New Business',
                //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
                Notes__c = event.Notes__c,
                
                //fill other mandatory fields comma seperated as shown above
                AccountId = Trigger.newMap.get(event.Id).AccountId,
                //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
                StageName = 'Pre-Qualified',
                Referring_SDR__c= event.Assigned_AE__c,
                Contact_Name__c = event.Related_Contact_Name__c,
                Opportunity_Owner__c= event.Assigned_AE__c,
                Amt__c= event.Amount__c,
                Seat_Number__c= event.Seats__c,
                LeadSource= event.Lead_Source__c,
                New_Lead_Source__c= event.New_Lead_Source__c
            );
            opportunities.add(opp);
        }
    }

    if(!opportunities.isEmpty()){
        insert opportunities;
    }
}

Now my requirement is:
1. I want to display the Name of Oppty as:
Account Name | RP | X Demo

2. I want to automatically populate the Assigned To (its a standard Event Field) value from Event to the Referring SDR(Custom Lookup(User)) custom field on the Oppty when it gets created
Screen shot shwoing the Custom fields on the Event page with values
Field Name                                API Name
Assigned To(On Event)             Owner
Referring SDR(On Oppty)         Referring_SDR__c

3. I want to flow the DemoID (if availble) or the Activity ID/Event ID to the Opportunity when its gets automatically created.

Any inputs will be highly helpful.

Thanks!
Best Answer chosen by Tanmay Sahai
Tanmay SahaiTanmay Sahai
Hi Abdul,

I have changed the Type to Event_Type__c for in the code. But it is still not automating an oppty creation.
Below is the code for your reference:

trigger EventTrigger on Event (after update){

    List<Opportunity> opportunities = new List<Opportunity>();
    list<string> idAccountList = new list<string>();
    List<Event> eventToProcessList = new List<Event>();
    String eventType;
    
    for(Event event :Trigger.new) {
        eventType = event.Event_Type__c;
        if(event.Event_Type__c.containsIgnoreCase('Demo') && event.Demo_Status__c != 'Complete'){
        //if(eventType!=NULL && eventType.containsIgnoreCase('email') ){
            if(event.WhatId!=NULL && event.WhatId.getSObjectType() == Account.SObjectType ){
                eventToProcessList.add(event);
                if(!idAccountList.contains(event.whatid))
                    idAccountList.add(event.WhatId);
             }
        }
    }
    
    Account[] AccNames=[select name from account where id in :idAccountList];
    map<string,string> AcccountMap= new map<string,string>();
    for(integer i=0;i<AccNames.size();i++){
        AcccountMap.put(idAccountList[i],AccNames[i].name );
    }
    
    for(Event event : eventToProcessList){
        
        //system.debug(AcccountMap.get(event.whatid));
        //Creating Oppty
        Opportunity op = new Opportunity();
            op.Name =AcccountMap.get(event.whatid)+ '|RP|'+ event.Seats__c;
            //Name = 'Test Opp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
            op.CloseDate = Date.Today()+ 90;
            op.Type = 'New Business';
            //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
            op.Notes__c = event.Notes__c;
            //fill other mandatory fields comma seperated as shown above
            op.AccountId = event.WhatId;
            //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
            op.StageName = 'Pre-Qualified';
            op.Referring_SDR__c= event.OwnerId;
            //op.Contact_Name__c = event.Related_Contact_Name__c;
            op.Opportunity_Owner__c= event.Assigned_AE__c;
            op.Amt__c= event.Amount__c;
            op.Seat_Number__c= event.Seats__c;
            op.LeadSource= event.Lead_Source__c;
            op.New_Lead_Source__c= event.New_Lead_Source__c;
        
         opportunities.add(op);
    }
    //system.debug('List of Records to be Created: '+opportunities);
    
    if(opportunities.size()>0){
        insert opportunities;
    }
    }

Kindly let me know where or what I am missing. Await your response.

Thanks!

All Answers

Abdul KhatriAbdul Khatri
Here is the trigger that must fulfill you first 2 requirements. I am little not sure what you are asking in the 3rd, if you can clarify.
 
trigger EventTrigger on Event (after update){

    List<Opportunity> opportunities = new List<Opportunity>();
    List<Id> idAccountList = new List<Id>();
    List<Event> eventToProcessList = new List<Event>();
    String eventType;
    
    for(Event event :Trigger.new) {

        eventType = event.Type;
        if(!eventType.containsIgnoreCase('demo') || eventType.Demo_Status__c != 'Completed') continue;

		if(event.WhatId.getSObjectType() != Account.SObjectType) continue;
        
        idAccountList.add(event.WhatId);
        eventToProcessList.add(event);
        
    }
    
    if(idAccountList.isEmpty()) return;
    
    Map<Id, Account> accountMap = [SELECT Id, Name FROM Account WHERE Id IN :idAccountList];
    
    for(Event event : eventToProcessList){
        
        Opportunity opp = new Opportunity(
        	Name = accountMap.get(event.WhatId).Name + '|RP|' + event.Seats__c,
            //Name = 'Test Opp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
			CloseDate = Date.Today()+ 90,
        	Type = 'New Business',
            //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
       		Notes__c = event.Notes__c,
	        //fill other mandatory fields comma seperated as shown above
        	AccountId = event.WhatId,
            //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
            StageName = 'Pre-Qualified',
            Referring_SDR__c= event.OwnerId,
            Contact_Name__c = event.Related_Contact_Name__c,
            Opportunity_Owner__c= event.Assigned_AE__c,
            Amt__c= event.Amount__c,
            Seat_Number__c= event.Seats__c,
            LeadSource= event.Lead_Source__c,
            New_Lead_Source__c= event.New_Lead_Source__c
         );
         opportunities.add(opp);
    }

    if(!opportunities.isEmpty()){
        insert opportunities;
    }
}
Tanmay SahaiTanmay Sahai
Hello Abdul,

Thanks for the help. I have tried your code but somehow I am getting a compile error. May be I am missing some , or ;.
Also can we use this trigger to work for insert DML operation.

Coming back to my 3 requirement:
Basically, I want to map the eventID to the automatically generated Opportunity. This is needed to track and create a report for all the demo type events with complete status that got converted to opportunity because eventually only those events are triggering the oppty creation which fulfill the criteria: Status = Complete.

Await your response.
Thanks!
Abdul KhatriAbdul Khatri
Please try this one, this should compile

Yes you can try with after insert
trigger EventTrigger on Event (after update){

    List<Opportunity> opportunities = new List<Opportunity>();
    List<Id> idAccountList = new List<Id>();
    List<Event> eventToProcessList = new List<Event>();
    String eventType;
    
    for(Event event :Trigger.new) {

        eventType = event.Type;
        if(!eventType.containsIgnoreCase('demo') || eventType.Demo_Status__c != 'Completed') continue;

		if(event.WhatId.getSObjectType() != Account.SObjectType) continue;
        
        idAccountList.add(event.WhatId);
        eventToProcessList.add(event);
        
    }
    
    if(idAccountList.isEmpty()) return;
    
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :idAccountList]);
    
    for(Event event : eventToProcessList){
        
        Opportunity opp = new Opportunity(
        	Name = accountMap.get(event.WhatId).Name + '|RP|' + event.Seats__c,
            //Name = 'Test Opp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
			CloseDate = Date.Today()+ 90,
        	Type = 'New Business',
            //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
       		Notes__c = event.Notes__c,
	        //fill other mandatory fields comma seperated as shown above
        	AccountId = event.WhatId,
            //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
            StageName = 'Pre-Qualified',
            Referring_SDR__c= event.OwnerId,
            Contact_Name__c = event.Related_Contact_Name__c,
            Opportunity_Owner__c= event.Assigned_AE__c,
            Amt__c= event.Amount__c,
            Seat_Number__c= event.Seats__c,
            LeadSource= event.Lead_Source__c,
            New_Lead_Source__c= event.New_Lead_Source__c
         );
         opportunities.add(opp);
    }

    if(!opportunities.isEmpty()){
        insert opportunities;
    }
}

What is the name of the field on Opportunity you want to tag with EventId?
Tanmay SahaiTanmay Sahai
Hi Abdul,

Thanks for your response. This code works fine and I am not getting any compile and debug errors but strangely I am not able to automate the oppty creation i.e. the trigger is not creating the oppty from the event.
Secondly, there is no such field on the oppty. All I want to achieve is a connection between event and the oppty that gets created from this event whether it is using the eventid and any custom formula field on the oppty or the vice versa. So, when the oppty is created, it should have the event/eventID reference. Hope this helps.

Look forward for your response.

Thanks in advance!
Abdul KhatriAbdul Khatri
You need to make sure that the event fulfills your requirement in order to pass through trigger. Can you sent the screen shot of the event you created

Type = 'Demo'
Demo Status  = 'Completed'
Related to = 'Account'

If any of the above didn't meet it will not do anything. Let's have this worked and I will help you with the 3 point.
Tanmay SahaiTanmay Sahai
Hi Abdul,

Thanks again for your response. My event is qualifying all the requirements but still the oppty is not being automatically created. The code is correct as to my best of knowledge and I am not getting any errors. 
I am attaching the screen shot of the event for your reference:

Event with Status = Complete

Can we work on this together. Kindly share your availability for today morning/afternoon and your Skype Id so that we discuss it further and also the 3rd requirement.

Await your quick response.
Thanks in advance!
Abdul KhatriAbdul Khatri
Please share the exact api name of the
Event Type
Status

Since we are using
Type => Out of box field
Demo_Status__c => Custom field

I see some issue there
Tanmay SahaiTanmay Sahai
Hello Abdul,

Thanks again for your response. Yes we are using custom picklist feild for our Event Type on the Event object. 

The name of the field is: Event Type and API Name is: Event_Type__c

Please let me know if you need anything else.

Thanks!
Tanmay SahaiTanmay Sahai
Hi Abdul,

I have changed the Type to Event_Type__c for in the code. But it is still not automating an oppty creation.
Below is the code for your reference:

trigger EventTrigger on Event (after update){

    List<Opportunity> opportunities = new List<Opportunity>();
    list<string> idAccountList = new list<string>();
    List<Event> eventToProcessList = new List<Event>();
    String eventType;
    
    for(Event event :Trigger.new) {
        eventType = event.Event_Type__c;
        if(event.Event_Type__c.containsIgnoreCase('Demo') && event.Demo_Status__c != 'Complete'){
        //if(eventType!=NULL && eventType.containsIgnoreCase('email') ){
            if(event.WhatId!=NULL && event.WhatId.getSObjectType() == Account.SObjectType ){
                eventToProcessList.add(event);
                if(!idAccountList.contains(event.whatid))
                    idAccountList.add(event.WhatId);
             }
        }
    }
    
    Account[] AccNames=[select name from account where id in :idAccountList];
    map<string,string> AcccountMap= new map<string,string>();
    for(integer i=0;i<AccNames.size();i++){
        AcccountMap.put(idAccountList[i],AccNames[i].name );
    }
    
    for(Event event : eventToProcessList){
        
        //system.debug(AcccountMap.get(event.whatid));
        //Creating Oppty
        Opportunity op = new Opportunity();
            op.Name =AcccountMap.get(event.whatid)+ '|RP|'+ event.Seats__c;
            //Name = 'Test Opp'+ 'Amount'+'(event.Id).Contact_Lookup__c',
            op.CloseDate = Date.Today()+ 90;
            op.Type = 'New Business';
            //Notes__c = Trigger.newMap.get(event.Id).Notes__c,
            op.Notes__c = event.Notes__c;
            //fill other mandatory fields comma seperated as shown above
            op.AccountId = event.WhatId;
            //Contact_Lookup__c = Trigger.newMap.get(event.Id).Contact_Lookup__c,
            op.StageName = 'Pre-Qualified';
            op.Referring_SDR__c= event.OwnerId;
            //op.Contact_Name__c = event.Related_Contact_Name__c;
            op.Opportunity_Owner__c= event.Assigned_AE__c;
            op.Amt__c= event.Amount__c;
            op.Seat_Number__c= event.Seats__c;
            op.LeadSource= event.Lead_Source__c;
            op.New_Lead_Source__c= event.New_Lead_Source__c;
        
         opportunities.add(op);
    }
    //system.debug('List of Records to be Created: '+opportunities);
    
    if(opportunities.size()>0){
        insert opportunities;
    }
    }

Kindly let me know where or what I am missing. Await your response.

Thanks!
This was selected as the best answer
Abdul KhatriAbdul Khatri
How about the Status field?
Tanmay SahaiTanmay Sahai
Hi Abdul,

That is correct. Its again a cutom picklist field and I have used its API: Demo_Status__c in code and feild label is: Status.

Let me know if you any additional information.

Thanks!
Tanmay SahaiTanmay Sahai
Also Status = Complete and not Completed.
Abdul KhatriAbdul Khatri
What is the API Name Status or Demo_Status__c?
Tanmay SahaiTanmay Sahai
API NAME: Demo_Status__c
Abdul KhatriAbdul Khatri
I don't see the Related To field clearly in the screen shot you have provided. Can you please make it clear like this.

User-added image
Abdul KhatriAbdul Khatri
Please send me your skype Id on my email abdulazizkhatri@gmail.com
Abdul KhatriAbdul Khatri
I don't have much time so please join skype quicly.