You need to sign in to do that
Don't have an account?
Tanmay 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
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!
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
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!
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
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!
Yes you can try with after insert
What is the name of the field on Opportunity you want to tag with EventId?
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!
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.
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:
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!
Event Type
Status
Since we are using
Type => Out of box field
Demo_Status__c => Custom field
I see some issue there
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!
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!
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!