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
roysmith601.3493067652527424E1roysmith601.3493067652527424E1 

Trigger to update the parent record based on child record update on opportunity object?

Hi,

I create trigger on Event Object and this will do the following: If user try to create new event on the related list on opportunity record then If user select anything on Primary focus(picklist) field in event while create new event then after user save the record then it will translate that picklist selected option to stage field in opportunity record.

For example: if user select primary focus = phone then opportuntiy stage hould be update with phone.I have different recprd type on event and opportunities and i want this trigger to be work on selected record types.

here is my trigger but it working but not updating the opportunity stage with what i select while creating event.

please help me figuring this out.

thanks

trigger eventafter on Event (after update) {
List<Opportunity> opps = new List<Opportunity>{};
 
for (Event  t : trigger.new)
{
 
// if the event is related to an Opportunity and the Stage has changed
if (t.Primary_Focus__c != null) {
      List<opportunity> o = [Select Id, StageName From Opportunity Where Id = :t.WhatId];
      if (o.size() >0 ) {
        o[0].StageName = t.Primary_Focus__c;
      }
      opps.add(o[0]);
   }
 
if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}
}
Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

Write one more trigger on Event like this:

 

trigger eventBefore on Event (before insert,before update) {
RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Opportunity Event' AND SobjectType = 'Event' LIMIT 1];
if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordTypeId != rt.Id)
{
t.addError('You can select only AF:Opportunity Event Record type');
}
}
}
}

 

Once it works club both the trigger code into one trigger.

All Answers

Laxman RaoLaxman Rao

try this :

 

trigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};

if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Primary_Focus__c != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Primary_Focus__c));
}
}
}
else
{
for(Event t : trigger.new)
{
if(t.Primary_Focus__c != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Primary_Focus__c != trigger.oldMap.get(t.Id).Primary_Focus__c)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Primary_Focus__c));
}

}
}

if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

Hi Laxman,

I try this and its working fine and now i have to add record type to my record. for example, if user click on event and we have 4 record type but we want to this functionlaity to work for only one event record.

Note: Instead of using rpimary focus field for now i m using subject option to be translate to opportunity stage field:

my new trigger:

rigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};

if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
else
{
for(Event t : trigger.new)
{
if(t.Subject != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Subject != trigger.oldMap.get(t.Id).Subject)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}

}
}

if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}

could you please help me

thanks

 

Laxman RaoLaxman Rao

I am confused, can you explain me in detail

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

Hi Rao,

Just add the condition to this if statement to only add the Opportunities for certain Event RecordTypes.

I enter the if statement but i am getting the following error:

ErrorError: Compile Error: Variable does not exist: t.Subject at line 4 column 5

 

my trigger is as follow:

 

trigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};

if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordType.Name = 'Opportunity Event')


if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
else
{
for(Event t : trigger.new)
{
if(t.Subject != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Subject != trigger.oldMap.get(t.Id).Subject)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}

}
}

if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}

Please help me.

thanks

 

 

Laxman RaoLaxman Rao

Try this:

 

trigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};
//if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordType.Name = 'Opportunity Event')


RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Opportunity Event' AND SobjectType = 'Event' LIMIT 1];

if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordTypeId == rt.Id)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
else
{
for(Event t : trigger.new)
{
system.debug('t.RecordTypeID' + t.RecordTypeID);
if(t.Subject != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Subject != trigger.oldMap.get(t.Id).Subject
&& t.RecordTypeId == rt.Id)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

I enter the trigger and i am able to save it but when i testing this trigger and try to add new event in the existing opportunity record then i am getting error:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger eventafter caused an unexpected exception, contact your administrator: eventafter: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.eventafter: line 6, column 1

 

my current trigger:

trigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};
//if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordType.Name = 'Opportunity Event')


RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'AF:Opportunity Event' AND SobjectType = 'Event' LIMIT 1];

if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordTypeId == rt.Id)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
else
{
for(Event t : trigger.new)
{
system.debug('t.RecordTypeID' + t.RecordTypeID);
if(t.Subject != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Subject != trigger.oldMap.get(t.Id).Subject
&& t.RecordTypeId == rt.Id)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}
please help me 

thanks

 

 

Laxman RaoLaxman Rao

Probem is due to this line :

 

RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'AF:Opportunity Event' AND SobjectType = 'Event' LIMIT 1];

 

It says not records were retrieved.Might be you are giving the wrong record type name

 

AF:Opportunity Event should be your record type name.

Run a query in workbech and ensure you are getting the row.

Workbench link is https://workbench.developerforce.com/query.php

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

I check and its working fine now for  Event record type = AF: Opportunity Event but also this trigger also let me add add other event record type but infact, i want this trigger to be work on only event record type =AF: Opportunity Event.

can you please  help me out.

 

Laxman RaoLaxman Rao

This trigger will only fire when the record type is AF: Opportunity Event.

 

It does not fire for other recordtypes because Code say:

t.RecordTypeId == rt.Id(which meand if recordtype ==AF: Opportunity Event )

 

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

this is what my understadning too but i am able to add record type other then AF: Opportunity Event record type.

please help me

 

Laxman RaoLaxman Rao

Do you want the trigger to fire for other record types also?

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

No RAO. BUT I AM still able to add other record type and the value update the opportunity stage name with other other record type.Infact,I want the ability to only been able to add record type AF: Opportunity Event and update teh stage name on opportunity.

I hope that this make sense to you and my concern.

thanks

 

Laxman RaoLaxman Rao

You mean to say, if user selects other than record type AF: Opportunity Event, then it should not allow to create the task???

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

Yes, This is right. I mean to say, if user selects other than record type AF: Opportunity Event, then it should not allow to create the event.

please help me

Laxman RaoLaxman Rao

Write one more trigger on Event like this:

 

trigger eventBefore on Event (before insert,before update) {
RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Opportunity Event' AND SobjectType = 'Event' LIMIT 1];
if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordTypeId != rt.Id)
{
t.addError('You can select only AF:Opportunity Event Record type');
}
}
}
}

 

Once it works club both the trigger code into one trigger.

This was selected as the best answer
roysmith601.3493067652527424E1roysmith601.3493067652527424E1

thanks