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
Jay Parikh 36Jay Parikh 36 

Apex trigger Urgent

Hi all---
I have reqierement where there are two object : account is parents and product is child 
now i want to update two fields on account 1. early purchased 2. latest purchased 
Early purchased will update based upon the product which is bought first one and latest purchased will update on same account where product bought latest one 

how i will achieve this trigger?
Best Answer chosen by Jay Parikh 36
HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following trigger. I have tested in my org and it is working fine.
 
Trigger EarlyLatestDates on Product__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Product__c prod : Trigger.New)
        {
            If(Prod.Account_Name__c != null)
            {
                accountIds.add(Prod.Account_Name__c);
            }
        }
    }
    If(Trigger.IsDelete)
    {
        For(Product__c Prod : Trigger.Old)
        {
            If(Prod.Account_Name__c != null)
            {
                accountIds.add(Prod.Account_Name__c);
            }
        }
    }
    
    List<Account> accountFinalList = New List<Account>();
    
    For(Account act : [Select Id, Early_Purchased__c, Latest_Purchased__c,
                                (Select Id, Purchase_Date__c FROM Products__r WHERE Purchase_Date__c != null ORDER BY Purchase_Date__c DESC)
                                        FROM Account WHERE Id =:accountIds ])
    {
        List<Date> datesPerAccount = New List<Date>();
        Integer I = 0;
        For(Product__c EveryProd : act.Products__r){
            datesPerAccount.add(EveryProd.Purchase_Date__c );
            I += 1;
        }
        
        act.Latest_Purchased__c = datesPerAccount[0];
        act.Early_Purchased__c  = datesPerAccount[I - 1];
        
        accountFinalList.add(act);
       
    }
    
    try{
        If(!accountFinalList.IsEmpty()){
            update accountFinalList;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Exception for EarlyLatestDates Trigger Is:: ' + e.getMessage());
    }
    
}
Hope this helps and mark it best answer if it solves the question!
Thank You!
 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hello Jay,

Do your company sell more than one products? If yes, then one account can have many opportunities and one opportunity can have many products so which one should be valid for Early Purchased and Latest Purchased?

Do you use opportunitiy and attach products to it?
Jay Parikh 36Jay Parikh 36
Hello Govind,
Our company is not using opp we are only using product and one account has many product how will achieve by trigger??
HARSHIL U PARIKHHARSHIL U PARIKH
Alright so this is can be the data Model:

Accoun Name: Acme, INC.
Products
            Product-1 Sold on Jan - 1 - 2017
            Product-2 Sold on Dec- 31-2016
            Product-3 Sold on Aug-1-2017
            Product-4 Sold on Dec-31- 2017

At this point for an Account, early purchased = Dec- 31-2016 and latest purchased = Dec-31- 2017

Is this the requirement?
Jay Parikh 36Jay Parikh 36
Hi Govind --- Yes .
HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following trigger. I have tested in my org and it is working fine.
 
Trigger EarlyLatestDates on Product__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Product__c prod : Trigger.New)
        {
            If(Prod.Account_Name__c != null)
            {
                accountIds.add(Prod.Account_Name__c);
            }
        }
    }
    If(Trigger.IsDelete)
    {
        For(Product__c Prod : Trigger.Old)
        {
            If(Prod.Account_Name__c != null)
            {
                accountIds.add(Prod.Account_Name__c);
            }
        }
    }
    
    List<Account> accountFinalList = New List<Account>();
    
    For(Account act : [Select Id, Early_Purchased__c, Latest_Purchased__c,
                                (Select Id, Purchase_Date__c FROM Products__r WHERE Purchase_Date__c != null ORDER BY Purchase_Date__c DESC)
                                        FROM Account WHERE Id =:accountIds ])
    {
        List<Date> datesPerAccount = New List<Date>();
        Integer I = 0;
        For(Product__c EveryProd : act.Products__r){
            datesPerAccount.add(EveryProd.Purchase_Date__c );
            I += 1;
        }
        
        act.Latest_Purchased__c = datesPerAccount[0];
        act.Early_Purchased__c  = datesPerAccount[I - 1];
        
        accountFinalList.add(act);
       
    }
    
    try{
        If(!accountFinalList.IsEmpty()){
            update accountFinalList;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Exception for EarlyLatestDates Trigger Is:: ' + e.getMessage());
    }
    
}
Hope this helps and mark it best answer if it solves the question!
Thank You!
 
This was selected as the best answer