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
agambeer singhagambeer singh 

How to write trigger on Standard object opportunity to calculate number of opportunities connected to particular standard object contact ?

Whole Scenario is, i have to take one Custom field in standard object CONTACT named "NumberOfOpportunities". And need to display number of opportunities related to particular contact in this custom field.
agambeer singhagambeer singh
trigger MappingOnOpportunity on Opportunity ( after insert,after update, after delete )
{
    Set <ID>conid = new Set <ID>();
    
    if (trigger.isinsert)
    {
        for (Opportunity opp : trigger.new ) 
        {
            conid.add(opp.contactid);
        } 
    }
    
    if (trigger.isupdate)
    {
        for (Opportunity opp : trigger.old) 
        {
            conid.add (opp.contactid);
        }
    }
    
    if (trigger.isdelete)
    {
        for ( Opportunity opp : trigger.old )
        {
            conid.add (opp.contactid);
        }
    }
    List <Contact> con = new List <Contact>([Select id, NumberofOpportunities__c, (Select id from Opportunities) from Contact Where ID IN : conid]);
    
    for (Contact a : con)
    {
        a.NumberofOpportunities__c = a.Opportunities.size();
    }
    update con;
}




................................X................................................x............................................................X.......................................................................................

i have tried by writing above code and this  is not  working...........please help me.........................i am new to salesforce.
Tushar KhorateTushar Khorate
Have you tried this If not then try this trigger for example then you will do your requirement also...!
Prework: Make 'Count of Contacts' field with type as Number on Account Object.
trigger CountOfContactsRelatedToAccount on Contact (after insert, after delete, after Undelete) {
    Set<Id> accId = new Set<Id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        for(Contact con : Trigger.new){
            accId.add(con.AccountId);
        }
    }
    if(Trigger.isDelete){
        for(Contact con : Trigger.old){
            accId.add(con.AccountId);
        }
    }
    List<Account> accList = [Select Id,Name,Count_of_Contacts__c,(Select id from contacts) from Account where Id IN : accId];
    for(Account acc :accList){
        acc.Count_of_Contacts__c = acc.contacts.size();
    }
    update accList;
}

Your Problem is similar like this.
If you found this helpful, Kindly mark it as best answer.
Thanks,
Tushar
CharuDuttCharuDutt
Hii Agambeer
Try below trigger
trigger NumberOfChild on Opportunity (After Insert,After Update,After Delete) {
    List<Contact> ConList=new List<Contact>();

    Set<Id> setConIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Opportunity Opp : Trigger.new){
            if(Opp.ContactId != null){
            setConIds.add(Opp.ContactId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Opportunity Opp : Trigger.new){ 
            if(Opp.ContactId!=Trigger.oldMap.get(Opp.Id).ContactId){
               	setConIds.add(Opp.ContactId);
                setConIds.add(Trigger.oldMap.get(Opp.Id).ContactId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Opportunity Opp : Trigger.old) { 
            if(Opp.ContactId != null){
            setConIds.add(Opp.ContactId);
            	}
        	}
        }
    }    
    for(Contact Con :[SELECT Id, Name, (SELECT Id, Name FROM Opportunities) FROM Contact where Id in : setConIds]){
        Con.Total_Opportunities__c = Con.Opportunities.size();
        ConList.add(Con);
    }
    if(ConList.size()>0){
        update ConList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
agambeer singhagambeer singh
Tushar, 
I have done this code on account and contact before putting this question, it went well.

It is not working for contact and opportunity. If you see the code i provide is similar to one you providing.
 
agambeer singhagambeer singh
@charuDutt it didnt work for me.
AbhinavAbhinav (Salesforce Developers) 
@agambeer Charu code seems close is any error you are getting?
 
agambeer singhagambeer singh
what do mean abhinav ?