+ Start a Discussion
SUMANTH ELLURUSUMANTH ELLURU 

Ability to have a new field on Opportunity that shows the total number of training days which is a total from the “Number of Training Days” field on the related Training Sessionrecords. here Oppurtunity is standardobject & Trainingsession is custom object

i want to relate Oppurtunity and Training session objects and the field No of Training Days in Training session record should be auto populated on No of training days field on oppurtunity standard object
LBKLBK
Hi Sumanth,

If the relationship between Opportunity and TrainingSession objects is Master - Detail, you just need to add a Roll-Up Summary type field in Opportunity that can sum up the days from TrainingSession object.

If the relationship is Lookup, you need to add a Number type field in Opportunity and add AFTER INSERT , AFTER UPDATE trigger in TrainingSession object to populate the field in Opportunity.

Hope this helps.
SUMANTH ELLURUSUMANTH ELLURU
can we solve this problem using process builder?
if yes can any one help me
and i am not able to create master detainl relationship between oppurtunity and training session objects
 
LBKLBK
Process builder may not be a good place for this because one TrainingSession record cannot access it's peers.

I am not sure if there is another configuration option, but if you are interested in going with Lookup Relationship and Trigger way, here is a sample code.
 
trigger UpdateTotalAmount on Opportunity (after insert,after update) {
     List<Id> lstAccountIDs = new List<Id>();
     for(Opportunity opp: Trigger.new) {
       lstAccountIDs.add(opp.AccountId);
     }
     
     Map<Id, Account> mapAccount = new Map<Id, Account>([SELECT Id, Name, Sum_of_Opportunity_Amount__c FROM Account WHERE Id =:lstAccountIDs]);

     List<Account> lstAccounts = new List<Account>();
     for(AggregateResult result: [SELECT SUM(Amount) Amt, AccountId FROM Opportunity GROUP BY AccountId, StageName Having AccountId IN :mapAccount.keyset() AND StageName = 'Closed Won' ]) { 
        Account acc = mapAccount.get((Id)result.get('AccountID'));
        acc.Sum_of_Opportunity_Amount__c = (Decimal)result.get('Amt');
        lstAccounts.add(acc);
     }
     if(lstAccounts.size() > 0){
        update lstAccounts;
     }
}
I have used this trigger to update a Total Amount field in Account object from the Amount field in Opportunity object using the Lookup relationship.

You may need to replace the Opportunity references with TrainingSession and Account references with Opportunity before using it.

Hope this helps.