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
ExecbizExecbiz 

Max Date function (on detail page or in report)

We have contacts with multiple ongoing and closed sales opportunities.  I would like a way for either the contact detail page (preferred) or a report to show the opportunity with the latest invoice date.  Am I going to need to do an Apex class for this?  I figured it might me a custom formula field on a report, but that doesn't apply to date fields...

 

Thanks a ton for any insight

JimRaeJimRae

You could use a trigger that updates a custom field on your contact detail page.

Whenever the contact is edited, you could fire a trigger that looks for all Opportunities related to the contact.  use a SOQL query that sorts the results in descending order by date, then limit the results to 1 "LIMIT 1" this will be highest date.

Then update the custom field with a link or some reference to the Opportunity.

Only issue would be, if you don't update the contact, this trigger would not fire.


That is one idea, YMMV, maybe someone else will have something better?

infoweldernickinfoweldernick

Here an example of a similar trigger that we have developed.  Hope this helps.

 

 

trigger IW_updateTotalDonations on Opportunity (after insert, after update, after delete, 

after undelete) 

{

if(Trigger.isInsert || Trigger.isUpdate || Trigger.isundelete)

{

for(Opportunity oppNew:Trigger.New)

{

if(oppNew.IsClosed && oppNew.IsWon)

{

List<Contact> objContact = [SELECT Id, Total_Donations__c, Is_A_Donor__c, Date_of_First_Donation__c, Date_of_Last_Donation__c FROM Contact WHERE Id = :oppNew.Contact__c];

if(objContact[0].Total_Donations__c > 0)

{

List<Opportunity> objAllOpps = [SELECT Id, Amount FROM Opportunity WHERE Contact__c = :objContact[0].Id AND IsClosed = true AND IsWon = true];

Decimal totalSum = 0;

for(Integer ctr = 0; ctr < objAllOpps.size(); ctr++)

{

totalSum += objAllOpps[ctr].Amount;

}

//objContact[0].Total_Donations__c = objContact[0].Total_Donations__c + oppNew.Amount;

objContact[0].Total_Donations__c = totalSum; 

}

else

{

objContact[0].Total_Donations__c = oppNew.Amount;

}

objContact[0].Is_A_Donor__c = true;

                                    

                Date refDate = date.newInstance(1900,01,01);

                if(objContact[0].Date_of_First_Donation__c > refDate)

                {

                }

                else

                {

                 List<Opportunity> objOpp = [SELECT Id, CloseDate FROM Opportunity WHERE Contact__c = :oppNew.Contact__c AND IsClosed = true AND IsWon = true ORDER BY CloseDate ASC LIMIT 1];

                 objContact[0].Date_of_First_Donation__c = objOpp[0].CloseDate;

                }

                

                //Integer day = System.now().Day();

    //Integer month = System.now().Month();

  //Integer year = System.now().Year(); 

  //objContact[0].Date_of_Last_Donation__c = date.newInstance(year,month,day); 

  objContact[0].Date_of_Last_Donation__c = oppNew.CloseDate;

                

                update objContact;

}

}

}

if(Trigger.isDelete)

{

for(Opportunity oppOld:Trigger.Old)

{

if(oppOld.IsClosed && oppOld.IsWon)

{

List<Contact> objContact = [SELECT Id, Total_Donations__c, Is_A_Donor__c, Date_of_First_Donation__c, Date_of_Last_Donation__c FROM Contact WHERE Id = :oppOld.Contact__c];

if(objContact[0].Total_Donations__c > 0)

{

List<Opportunity> objAllOpps = [SELECT Id, Amount FROM Opportunity WHERE Contact__c = :objContact[0].Id AND IsClosed = true AND IsWon = true];

Decimal totalSum = 0;

for(Integer ctr = 0; ctr < objAllOpps.size(); ctr++)

{

totalSum += objAllOpps[ctr].Amount;

}

objContact[0].Total_Donations__c = totalSum;

//objContact[0].Total_Donations__c = objContact[0].Total_Donations__c - oppOld.Amount;

}

update objContact;

}

}

}

} 

 

 

Message Edited by infoweldernick on 06-23-2009 10:22 AM
tawfik-haitawfik-hai

I need to do a similar function, did you figure this out?

 

thanks