• akash
  • NEWBIE
  • 20 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 0
    Replies
Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'


trigger ClosedOpportunityTrigger on Opportunity(after insert, after update) {
List<Task> oppList = new List<Task>();
for (Opportunity a : [SELECT Id,StageName,(SELECT WhatId,Subject FROM Tasks) FROM Opportunity
WHERE Id IN :Trigger.New AND StageName LIKE '%Closed Won%']) {
oppList.add(new Task( WhatId=a.Id, Subject='Follow Up Test Task'));
}
if (oppList.size() > 0) {
insert oppList;
}
}
  • March 16, 2023
  • Like
  • 0
when the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won


trigger OppoStageUpdate on Account (after update){
Set<Id> accountIds = new Set<Id>();
for(Account a:Trigger.new){
accountIds.add(a.Id);
}
//day30 is the date which is 30 days less than today
DateTime day30=system.now()-30;
List<Opportunity> oppListToUpdate=new List<Opportunity>();
//getting the opportunities whose account has been updated
List<Opportunity> oppList = [Select Id, AccountId, StageName, CreatedDate, CloseDate from Opportunity where AccountId in :accountIds];
if(oppList.size()>0){
for(Opportunity opp : oppList){
//checking for condition if created date is greater than 30 days from today and stage not equal to close won
if(opp.CreatedDate<day30 && opp.StageName!='Closed Won'){
opp.StageName='Closed Lost';    //This is a mandatory field when we update the CloseDate
opp.CloseDate=system.today();
oppListToUpdate.add(opp);  //putting the changed opportunity to separate list to update later
}
}
}
//checking if the opportunity list which has changed is having records or not
if(oppListToUpdate.size()>0){
update oppListToUpdate;
}
}
  • March 16, 2023
  • Like
  • 0
Create a field called 'Latest Opportunity Amount' on Account.and then Auto Populate the above field  with the Newley created Opportunities’ Amount. How to do this.
  • March 14, 2023
  • Like
  • 0
Create an email alert to be sent to user who created record whenever a new record is added to account object? Plz solve this
  • August 31, 2022
  • Like
  • 0