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 

Help: custom field on activity object update the opportunity object

Hi,
I have create custom picklist field:"stage" on activity object and requirment is that when user update the activity then the field "Stage" on opportunity object should update on the user selection in activity object. for example: if user select stage = Negotiation in activity object then opportunity object  stage will update with Negotiation.
i try to create workflow but i wont help.
please help me
thanks

Ritesh AswaneyRitesh Aswaney

I think you need a trigger

 

trigger TaskAfter on task (after update){

 

List<Opportunity> opps = new List<Opportunity>{};

 

for (Task t : trigger.new)

{

 

// if the task is related to an Opportunity and the Stage has changed

if (t.WhatId != null && ((String)t.WhatId).startsWith('006') && t.Stage__c != null && t.Stage__c != trigger.oldMap.get(t.Id).Stage__c

opps.add(new Opportunity(Id = t.WhatId, StageName = t.Stage__c);

}

 

if(opps != null && !opps.isEmpty())

Database.update(opps);

}

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

thanks for your help then i have to create trigger on the opportunity object or activity object? if i implement this then from opportunity record related list "open activity" , when i click on "New Event" then if i create new event with custom field = negotitate then this will update the opportnity satge = negotitate?

p[lease advise and help me.I am new to salesforce

thanks

Ritesh AswaneyRitesh Aswaney

This trigger is on Task.

If you need it to be on Event, then just replace Task with Event.

Yes it will run when you create new Event from the related List on Opportunity.

 

If you need for both Task and Event, you will need two triggers, one each on Task and Event.

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

I need this on related list "Event" on opportunity object and i change the task with Event but i am getting this error below:

ErrorError: Compile Error: expecting a right parentheses, found 'opps.add' at line 10 column 0

 

here is the trigger:

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.WhatId != null && ((String)t.WhatId).startsWith('006') && t.Stage__c != null && t.Stage__c != trigger.oldMap.get(t.Id).Stage__c
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Stage__c));
}

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

 

please help me why i am getting this error also where in your trigger, you are taking in consideration that if stage = Negoatiation  on event then update the opportunity stage = Negoatiation

I am new to salesforce so please help me 

thanks

 

Ritesh AswaneyRitesh Aswaney

It was missing the right paranthesis, I've added it. The trigger copies across Stage__c from Event to Opportunity, no matter what the value. Do you want to copy only if Stage__c is  Negotation ?

 

 

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.WhatId != null && ((String)t.WhatId).startsWith('006') && t.Stage__c != null && t.Stage__c != trigger.oldMap.get(t.Id).Stage__c ) // RIGHT PARANTHESIS WAS MISSING HERE
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Stage__c));
}

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

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

thanks for your reply , i need your help again for record type.

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);
}
}
Ritesh AswaneyRitesh Aswaney

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

 

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


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

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

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);
}
}