• Sujitha Vedagiri
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
trigger maxVisitDate on Meeting_Plan__c(after insert, after update, after delete) {
    Set<Id> accIds = new Set<Id>();
    List<Account> listAccUpdate;

    if (!Trigger.isDelete) {
        for( Meeting_Plan__c newMeet : Trigger.new) {
            accIds.add(newMeet.Meeting_Plan_Related_to_Account__c);
        }
    }

    if (!Trigger.isInsert) {
        for (Meeting_Plan__c OldMeet : Trigger.oldMap.values()) {
            accIds.add(OldMeet.Meeting_Plan_Related_to_Account__c);
        }
    }

    accIds.remove(null);

    if (!accIds.isEmpty()) {
        listAccUpdate = new List<Account>();

        for (AggregateResult ar : [
            select MAX(Visit_Date__c) date,
                Meeting_Plan_Related_to_Account__c
            from  Meeting_Plan__c
            where Meeting_Plan_Related_to_Account__c in :accIds
            group by Meeting_Plan_Related_to_Account__c
        ]) {
            //vendorProductsToUpdate.add(new Vendor_Product__c(Id = ar.Vendor_Product__c,Current_MRR__c = ar.get('expr0')));
             Account acc=new Account();
                 acc.Id=(ID)ar.get('Meeting_Plan_Related_to_Account__c');
                acc.Last_Customer_Visit_RollUp__c=(Date)ar.get(String.valueOf('date'));
                
                 // acc.Last_Cust_Visit_Rollup_Helper__c=(Date)ar.get('Visit_Date__c') ar.get('date');;
                 listAccUpdate.add(acc);

        }

        if (!listAccUpdate.isEmpty()) {
            update listAccUpdate;
        }
    }
}
I have written a trigger on opportunity with close date as 31/5/2017 and in stage history it is showing one record as per the stage when i change the stage one more record is inserting. i want only one record to be inserted in stage history with 31/5/2017.
Here is my code.

trigger updateclosedate on Opportunity (before insert) 
{
List<id> ids=new List<id>();

for(Opportunity A : trigger.new)
ids.add(A.id);
//List<Opportunity> opupdate=new List<Opportunity>();
list<opportunity> opupdate=[SELECT Id,closedate FROM Opportunity WHERE CloseDate = NEXT_FISCAL_YEAR];
for(Opportunity o: opupdate)
{
 
 opupdate.add(o);
 system.debug('opupdate333333'+opupdate);
}
insert opupdate;

}

Can you please help on this.