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

Need a Trigger to update a field value

In my app the project__c is the parent and Opportunity is the child.Iam having a lookup relationship between  project__c and opportunity. 


 There is number field called units on both objects. The project__c has total units. When an opportunity is created for certain number of units, the total units on the project__c has to be updated by deducting the opportunity units.


Will be waiting for a response.


Thanks in advance!

Baktash H.Baktash H.

Create a after insert trigger on opportunity which gets the parent-project and updates its unit field.


If only on opportunity creation the units field will be populated then create trigger is fine, but in case if the units will change on opportunity update than update trigger will be required.


Sorry but i have 1 questions 

 1. I think opprtunity is the standard object and you can not create master detail relationship on the Project__c is not the parent of the Opprtunity... So definitely there will be lookup (you have mentioned)

  2. You need to write a trigger on the Opprtunity Object After create and After Update. 


   OnOpprtunityInsertUpdate Opprtunity (after Insert, after Update)


// get the map it will give you the Opportunity (which is inserted or updated Currnetly)

               // get all the id's of the procuct__c object related with the current opprtunity

              // fire the SOQL query on the Product__c and create the Map of <ID,List<Opprtunity>> ( Product__c is, and the list of the opprtuntiy)

     // fire the loop on the map and find the list of the opprtunity ..calculate the each point according to the Product__c and update the product__c



 Thanks and Regards



Hi Niku...

  Thanks for ur valuable suggestion.The following is the code that i have written.iam having trouble in getting the ids of the project__c as I am new to coding,can u please make the necessary changes. 


thanks in advance...


trigger childnum on opportunity (after insert,after update) {

opportunity c =[0];

list<project__c> pl = new list<project__c>();

for(parent__c p :[select id,num__c from parent__c where id])


p.num__c=p.num__c - c.num__c;



update pl;





You should query on opportunity to get the parent id rather than making on parent. for example-

Select Id, parent__r.Id from Opporutnity where Id in:trigger.newmap.keyset();


Hi UV...


 can u please send me the total code.I dont no much coding.


thanks in advance.


You can do it in this way..

trigger UpdateUnitOnProject on Opportunity (before insert,before update)
    List<Project__c> projectList=new List<Project__c>([Select p.Id,p.TotalUnits__c,(Select Id From Opportunities__r) From Project__c p]);
    List<Opportunity> OppList=new List<Opportunity>([select id,project__c,Units__c from Opportunity where Id in:trigger.newMap.keyset()]);
    Map<Project__c,List<Opportunity>> oppmap=new Map<Project__c,List<Opportunity>>();
    List<Opportunity> newList=new List<Opportunity>();
    List<Project__c>updatedList=new List<Project__c>();
    for(Project__c p:projectList)
        for(Opportunity o:OppList)
    for(Project__c p:oppmap.keyset())
        for(List<Opportunity> o:oppmap.values())
            for(Opportunity opp:o)
    update updatedList;


This is just a Sample code..Make changes to it as per your need and you can optimize it also.


Thanks for sending code i will try and update u...