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
suresh143suresh143 

Trigger: update child object field with parent's value (it updates via formula)

Hi swetha so you great i think so and one more is there
write trigger account and opp then once created opp how many number of opp is displyed in account object in number of opps field in acc object

GlynAGlynA

@suresh143,

 

The trigger below will run whenever an Opportunity (or Opportunities) is inserted, updated, deleted or undeleted.  It finds all the related Account records, and then counts how many Opportunities are on each of those Accounts.  It modifies a custom field on Account, called "Num_Opportunities__c", and updates the Account records.

 

trigger OpportunityAfterTrigger on Opportunity ( after insert, after update, after delete, after undelete )
{
    Set<Id> set_AccountIDs = new Set<Id>();

    for ( Opportunity theOpportunity : Trigger.isDelete ? Trigger.old : Trigger.new )
    {
        set_AccountIDs.add( theOpportunity.AccountId );
    }

    Map<Id,Account> map_Accounts = new Map<Id,Account>
    (   [   SELECT  Id, Num_Opportunities__c
            FROM    Account
            WHERE   Id IN :set_AccountIDs
        ]
    );

    for ( AggregateResult result :
        [   SELECT  COUNT(Id) numOpportunities, AccountId
            FROM    Opportunity
            WHERE   AccountId IN :set_AccountIDs
            GROUP BY AccountId
        ]
        )
    {
        map_Accounts.get( (Id) result.get( 'AccountId' ) ).Num_Opportunities__c = (Integer) result.get( 'numOpportunities' );
    }

    update map_Accounts.values();
}

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

suresh143suresh143
Hi thanks for giving this one its not working properly
suresh143suresh143
Hi GlynA Iam asking num of opp in displayed in account object releated opps in account it can be through roll up summary my business requirement in through trigger pls give that one
GlynAGlynA

I had an error in the code I provided.  I have edited my previous post to fix the problem.  I have compiled and tested this trigger in my DE org and it does exactly what I described:  Whenever an Opportunity is created, modified, deleted or undeleted, it will update the Account with the correct number of related Opportunities.  The field will not be initialized, however, until a related Opportunity is triggered.

 

To load all of the Account records with the correct counts, you could run the following in the Execute Anonymous window:

 

Map<Id,Opportunity> map_OpportunitiesToUpdate = new Map<Id,Opportunity>();

for ( Opportunity opp : [SELECT Id, AccountId FROM Opportunity] )
{
    if ( map_OpportunitiesToUpdate.containsKey( AccountId ) ) continue;

    map_OpportunitiesToUpdate.put( AccountId, opp );
}

update map_OpportunitiesToUpdate.values();

This should work, unless your org has more than 50,000 Opportunity records.

 

If this is not your requirement, my apologies.  I am having difficulty understanding.

 

-Glyn

suresh143suresh143
Ihave an account object there is one field no of opportunitys.once i had created new record with account and no of opportunitys count displayed in Account object field like no of opportunitys in detail page
GlynAGlynA

You will have to replace "Num_Opportunities__c" in my code with the developer name of your custom field - "No_of_Opportunitys__c", or whatever it is...

 

-Glyn