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
Rachel Linder 8Rachel Linder 8 

How can i count the number of opportunity team members

Hello,
I need a way to create a field on the opporutnity level that looks at the opportunity team members and counts of there are or are not any team members. I can't do this via a rollup field. I tried a formula field but can't find a way to get to the opportunity team level.

Any suggestions?
Thank you.

Pradeep SinghPradeep Singh
Hi,
You can write trigger and use aggregate query to count the number of opportunity team member and update the field of opportunity.
nazim zmirlinazim zmirli
After splits are enabled, you can make the splits workflow faster for users as well, letting them add team members while editing split amounts and from one click you can disable Opportunity Split. It means you can add add any user in Opportunity splits Salesforce will automatically add those user under Opportunity team.
https://showbox.onl/ (https://vidmate.onl/)


 
Rajesh3699Rajesh3699
Hey

hope this helps,

Use process builder to call the apex class and update the field on the opportunity, make use of the below query to update the field, 
FYI : Update opportunity only when there is a change in the TeamMember size.

Integer x = select Count() from OpportunityTeamMember where OpportunityId = 'OppId', make sure if you are querying using dev console, don't forget to uncheck "USE Tooling API"

Thank You,
Rajesh.
Alain CabonAlain Cabon
Hi,

Andrew Fawcett has created a generator of trigger (just point-and-click) for this need.

Declarative Rollups for Lookups!​ Latest Release Version 2.11
Package tp install:

Production URL:  https://login.salesforce.com/packaging/installPackage.apexp?p0=04t0N000000IyYr
Sandbox URL:  https://test.salesforce.com/packaging/installPackage.apexp?p0=04t0N000000IyYr

https://github.com/afawcett/declarative-lookup-rollup-summaries

1) Create a new number field : TeamMemberCount on Opportunity.

User-added image


2) Create a new Lookup Rollup Summary field:

User-added image



User-added image

3) Activate the generated trigger (important)

4) It is "realtime".

User-added image

And that's all. The counter will be always up-to-date.

Zero code written by yourself and the generated code is "perfect" ( Andrew Fawcett  is well-known for his top level Apex code  )

Just re-create the same lookup summary field on each org.

Alain
 
Alain CabonAlain Cabon
3) Activate the generated trigger (important) is the deployment of the generated trigger (button at the button at the end of the page).
/**
 * Auto Generated and Deployed by the Declarative Lookup Rollup Summaries Tool package (dlrs)
 **/
trigger dlrs_OpportunityTeamMemberTrigger on OpportunityTeamMember
    (before delete, before insert, before update, after delete, after insert, after undelete, after update)
{
    dlrs.RollupService.triggerHandler(OpportunityTeamMember.SObjectType);
}

This trigger generated in your org is very simple (just one line) because all the logic is into the RollupService class which can access the generic Trigger.oldMap, Trigger.newMap of the current changed opportunity (context of the trigger) and will use the List<RollupSummaries.CalculationMode> put into the  Lookup Rollup Summary ​edit page above (dynamic).

https://github.com/afawcett/declarative-lookup-rollup-summaries/blob/master/force-app/main/classes/RollupService.cls
 
/**
     * Apex Trigger helper, automatically resolves child records to process via LREngine and lookups described in RollupSummary
     *    also determines if based on the old trigger records if the rollup processing needs to occur
     * @param childObjectType This can be used in cases where the prior overload was used an the Id.getSobjectType method fails to return the SOBjectType (see Issue 17 in the repo)
     **/
    global static void triggerHandler(SObjectType childObjectType)
    {
        triggerHandleInvoked = true;

        // Currently no processing in the before phase
        if(Trigger.isBefore)
            return;     
            
        // Anything to rollup?
        handleRollups(Trigger.oldMap, Trigger.newMap, childObjectType, new List<RollupSummaries.CalculationMode> { RollupSummaries.CalculationMode.Realtime, RollupSummaries.CalculationMode.Scheduled });
    }

	/**
	 * Apex Trigger helper, automatically resolves child records to process via LREngine and lookups described in RollupSummary
	 *    also determines if based on the old trigger records if the rollup processing needs to occur
	 **/
	global static void triggerHandler()
	{
		triggerHandleInvoked = true;

		// Currently no processing in the before phase
		if(Trigger.isBefore)
			return;		
			
		// Anything to rollup?
		List<SObject> childRecords = Trigger.isDelete ? Trigger.old : Trigger.new;
		SObjectType childObjectType = childRecords[0].Id.getSObjectType();		
		handleRollups(Trigger.oldMap, Trigger.newMap, childObjectType, new List<RollupSummaries.CalculationMode> { RollupSummaries.CalculationMode.Realtime, RollupSummaries.CalculationMode.Scheduled });
	}

Alain
Alain CabonAlain Cabon
This trigger generated in your org is very simple (just one line) because all the logic is into the RollupService class which can access the generic Trigger.oldMap, Trigger.newMap of the current changed Opportunity Team Member (a child)(not the opportunity, the parent).
Rachel Linder 8Rachel Linder 8
@Alain -  We have the declarative lookup summaries app added to our production org. I am creating a new Lookup Rollup Summary but when I hit save i get the following error:

User-added image

Where do I click Manage Child Trigger and Try Again.
Alain CabonAlain Cabon
@Rachel: I have had the same error. You must not check "Active" at first but save and "deploy" the generated trigger (button at the bottom of the page) then after, you can check "Active".

Alain
Rachel Linder 8Rachel Linder 8
@Alain - thank you for your help. That did the trick. One other question - is there a way tohave it go back and look at opportunities and do a count. I activated the field and checked some opportunities I know has team members and it is shwing as a blank field.
Alain CabonAlain Cabon
@Rachel: you should use the button "Calculate" (bottom of the page). I have used this button but I didn't remember it.

Just calling the trigger with update [SELECT Id FROM OpportunityTeamMember]; is not enough (?) even if the trigger is clearly called viewing the log in the developer console but the button "Calculate" works fine.