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
James S.ax1698James S.ax1698 

Newby code help please

Hi everyone, whilst I have some background in coding in visual basic, I am brand new to apex triggers, but I am having a go. What I'm trying to do is say populate field 'Next Activity Date' on opportunities, based on an open activity linked to the opportunity. The code below is a combination of what I have been able to piece together from websites and this message board so it is obviously a bit rubbish and doesnt work, but hopefully you can see where I'm going. I'd really really appreciate any help anyone can provide me :-)

 

trigger LastActivity on Opportunity (after insert, after update) {


For (Opportunity opp : Trigger.new)

 

List<Opportunity> OpsToUpdate = [SELECT (SELECT ActivityDate,
                                                                                                  Description,
                                                                                                   Subject
                                                                                                FROM OpenActivities)
                                                                                                 FROM Opportunity];


System.debug(opp.OpenActivities);
}


Opportunity.Next_Step_Date__c = OpenActivities.ActivityDate

OpsToUpdate.add(opp);
}

Bhawani SharmaBhawani Sharma
1. You do not need trigger on insert event as at the time of opportunity insert, there can not be any activity associated with it.

2. I think, you should have this trigger on Event instead of opportunity. So if any activity is associated with the Opportunity, it will automatically update the Opportunity's Next Step Date.

3. If you still require trigger on Opportunity, then use before update trigger, put your query outside the for loop to prevent from governor limit.
James S.ax1698James S.ax1698

Many thanks Bhawani - I will make the trigger fire on events/tasks instead as you suggest

 

How to I get it to referenece the relavant opportunity and the opportunity field please? I am getting a bit stuck here

Bhawani SharmaBhawani Sharma

In Event object, you can use WhatId(Related To) field to find out the parent opportunity object.