-1

need assistance regarding how to automatically populate the values of Opportunities from Opportunity object to custom number field in Account object? I have number_of_Opportunities, number_of_open_Opportunities, and number_of_closed_Opportunities created in Account object. so need to get the number of opportunities from Opportunity and populate in number_of_Opportunities field in Account. tried to create trigger for number of opportunities as a start but seems not working. thanks

trigger PopulateAccountOpportunities on Opportunity (after insert, after update) {

    Set<Id> accountIds = new Set<Id>();
    for(Opportunity opp : Trigger.new) {
         accountIds.add(opp.AccountId);
    }
    //Update Accounts
   Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Number_of_Opportunities__c
                             FROM Account where Id IN :accountIds]);
    List<Account> lstAccOpp = new List<Account>();
    for(Opportunity opp : Trigger.new) {
       Account acc = mapAccounts.get(opp.AccountId);
       acc.Number_of_Opportunities__c = (opp.StageName);
       lstAccOpp.add(acc);
    }
  update lstAccOpp;
    Map<Id, Account> mapUpdatedAccounts = new Map<Id,Account>();
    for(Account acc : lstAccOpp) {
        mapUpdatedAccounts.put(acc.Id,acc);
        }
}
  • What do you mean by not working ? Did you get any error while trigger fire? Please add relevant details to clarify your issue. – Reshma Jan 10 at 6:31
  • Hi Reshma, yes error msg is "Illegal assignment from String to Decimal" on line 13 – vlink Jan 10 at 6:34
  • The error is straight forward. How can you copy text value to a number field. Stagename is text and you want to copy it to Number_of_Opportunities__c. Strange!! – Reshma Jan 10 at 6:37
  • yes. just trying to use the StageName field since the number of opportunities are there. so not sure how to get the values to populate in Number_of_Opportunities__c field in Account – vlink Jan 10 at 6:44
  • You need to query opportunities of each account and add logic to get count. I would recommend to use roll-up summary fields which much easier and efficient. – Reshma Jan 10 at 6:55
0

You can give a try with below code if you need to implement by trigger without using roll-up summary fields.

trigger PopulateAccountOpportunities on Opportunity (after insert, after update) {

    Set<Id> accountIds = new Set<Id>();
    for(Opportunity opp : Trigger.new) {
        accountIds.add(opp.AccountId);
    }

    List<Account> lstAccOpp = new List<Account>();
    Map<ID, Account> mapAccounts = new Map<ID, Account>(
                            [SELECT Id, Number_of_Opportunities__c, Number_of_Closed_Opportunities__c, Number_of_Open_Opportunities__c,
                                (SELECT Id, IsClosed FROM Opportunities)
                             FROM Account 
                             WHERE Id IN :accountIds]);

    for(Account acc :mapAccounts.values()){
        Integer closedCount = 0;
        Integer openCount = 0;
        for(Opportunity opp :acc.Opportunities){
            if(opp.IsClosed){
                closedCount = closedCount + 1;
            }
            else{
                openCount = openCount + 1;
            }
        }
        acc.Number_of_Open_Opportunities__c = openCount;
        acc.Number_of_Closed_Opportunities__c = closedCount;
        acc.Number_of_Opportunities__c = openCount + closedCount;
        lstAccOpp.add(acc);
    }
    update lstAccOpp;
}

Here I used IsClosed field of opportunity to check the status. You can add criteria as per your requirement.

  • Hi Reshma sir,I tried and It works!!!! many thanks for your help.. I learned a lot from u. I will monitor the behavior of this trigger and let you know. thanks again. – vlink Jan 10 at 12:22
1

Why are you doing this with code? Since Oppty has a Master Detail Relationship with Account, you can just create three Rollup Summary fields on the Account and set the appropriate filters to return the number of records based if the Oppty is closed/open, etc....

There is no need for coding at all.

Please refer to documentation here: https://help.salesforce.com/articleView?id=fields_about_roll_up_summary_fields.htm&type=0

  • Hi Rodrigo, thanks for the comments, Im trying to create a trigger to auto populate it without using rolupsummary field. I also created formula field which if needed for this trigger. thank u. from that trigger I got error msg "Illegal assignment from String to Decimal" – vlink Jan 10 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.