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
The new LearnerThe new Learner 

Need to calculate the Opportunity Amount based on the Record type--very urgent

Hi Experts

I need to calculate the Opportunity amount sum based on the Recod type Eg: i have A, B, C three Record types , Now i will create record with the record type B by selecting the parent opportunity Record of Record type A. Now i need to calculate sum of all the records of Record type B which is assicated to the Opportunity Record type of Record type A. simily amount of C record type records on B record type record.

Thanks in advance
KapilCKapilC
Hi

Is your opportunity always created with the right record type e.g. B will always have  A as a parent and C will always have B as a parent opportunity?

Rehards,
Kapil
The new LearnerThe new Learner
Hi Kapil,

Thanks for the reply i am desperately waiting for the responce, yes it will as you said it will create like that.

 
KapilCKapilC
Hi Aditya,

In this please find the code below. Hopefully this will work for you.
trigger OpportunityTrigger on Opportunity (after insert) {
	set<id> parentOppIdSet = new set<id>();
	for(Opportunity opp : Trigger.new){
		if(opp.Parent_Opportunity__c != null){
			parentOppIdSet.add(opp.Parent_Opportunity__c);
		}
	}
	if(!parentOppIdSet.isEmpty()){
		map<id,opportunity> parentOppIdMap = new map<id,opportunity>();
		for(opportunity parentOpp : [select id,ChildRecords_Amount__c,(select id,Amount from Opportunities__r) from opportunity where id in : parentOppIdSet]){
			for(Opportunity childOpp: parentOpp.Opportunities__r){
				if(!parentOppIdMap.containsKey(parentOpp.id)){
					parentOppIdMap.put(parentOpp.id,parentOpp);
				}
				system.debug('parentOppIdMap::::'+parentOppIdMap);
				if(parentOppIdMap.get(parentOpp.id).ChildRecords_Amount__c == null) {
					parentOppIdMap.get(parentOpp.id).ChildRecords_Amount__c = 0;
				} 
				parentOppIdMap.get(parentOpp.id).ChildRecords_Amount__c = parentOppIdMap.get(parentOpp.id).ChildRecords_Amount__c  + childOpp.Amount;
			}
			
		}
		if(!parentOppIdMap.isEmpty()){
			update parentOppIdMap.values();
		}
	}    
}

ChildRecords_Amount__c is a custom field on opportunity of type currency. Parent_Opportunity__c is a lookup on opportunity.

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
The new LearnerThe new Learner
Hi Kapil,

Thanks for the code but i have one doubt , dont we need to consider the Record types here becasuse we need to sum the amount based on record type only
KapilCKapilC
Aditya,

If there is always a certain type of recordtype will insert for child then no need to check this. if Parent A will have B and C both then you have to check this and there will be 2 fields on opportunity 1. For B childTotal and For c ChildTotal.

Regards,
Kapil
The new LearnerThe new Learner
apologies for that Kapil, on Record type A user can able to create record type b as well as record type c opportunity records. i am extremly sorry not to intimate you before
The new LearnerThe new Learner
Hi Kapil,

COde is working fine i need for the , for the B and C record type as well. Kindly help me out please
The new LearnerThe new Learner
Hi Kapil, Requirement little changed now, I need total C record type opportunities amount have to populate on parent of that B record type opportunities and populated amount on B record type plus also sum of all the B record type opportunities amount need to populate on record type A opportunity record. Kindly help me