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
Abhishek Thakur 17Abhishek Thakur 17 

Closed won opp

I want to calculate Roll up summary for Closed Won Opportunities for the current year. How do achieve this?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Do you want it on the Account field?

Thanks,
 
Abhishek Thakur 17Abhishek Thakur 17
Hi Yes I want it on the account field
mukesh guptamukesh gupta
Hi Abhi,


It's Not feasible by RollUp Summary field, So you need to go with Workflow, Flow o rTrigger.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Abhishek Thakur 17Abhishek Thakur 17
Hi Thank you. Can you please help me on how I could archive this using flow?
sai shanmukh vishnubhatlasai shanmukh vishnubhatla
Hi Abhishek,

This is not as easy as it sounds like. 

1. You will have to set the conditions for the Opportunity with Stage as 'Closed Won' and run the flow 'only when a record is updated to meet the condition requirements'.
2. Choose 'Action and related records'.
3. Assign the values for the fields on the Order object. You might need to map it from the Oppty object itself. Use the assignment element here.
4. Then use the Create record element to create the Order record.
5. After that, use a decision element to check if the oppty has line items.
6. If so, then get the oppty line items using a 'get records' element and store it in a collection variable
7. Using loop element, loop through the collection variable and assign the values to another OrderItem type variable.
8. Within the loop, keep assigning this OrderItem type variable to another OrderItem type collection variable.
9. At the end, after the last item, use the create record element to create the orderitem records using the OrderItem type collection variable created in previous step.
Thanks,
Sai shanmukh.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

I have written the trigger for the same. Let me know if this works. If you still need flow I can develop and let you know it.
 
trigger calAmount on Opportunity (after insert, after update, after delete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate || trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds and StageName='Closed Won' and CloseDate= THIS_YEAR];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
acctList = [SELECT Closed_This_Year__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double OppAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
OppAmount += oppty.Amount;
}
}
acct.Closed_This_Year__c = OppAmount;
}
update acctList;
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Abhishek Thakur 17Abhishek Thakur 17
Hi sai Thanks alot. Will check and get back to you.
Abhishek Thakur 17Abhishek Thakur 17
Hi there is a small catch here.
let's say I have an account called TEST and it has 6 closed won opportunities in 2022. I want the sum of those closed won opportunities. 
Now as the year changes and 2023 year has 3 opportunities, I want the sum of that three opportunities. How can we achieve this using a formula field? The output needs to be refreshed every year according to its closed won opportunities.