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

Opportunity and Contract Object relationship - can't be done.

Can anyone help me with this frustrating issue. I am developing an application where I want to relate a number of opportunities with a contract and have the abilitiy to total sum the opportunity amounts and count the opportunities against this contract. I can't believe this can't be done. Am I the first person that's tried to do something like this?

Dirk GronertDirk Gronert

You have to use a Lookup-Relationship and write a trigger to do the job of sum the opp amount and the counting! It's easy to achieve that!



I tried every which way to do this and even with a trigger(see below). I think the main problem is the MD relationship issue with Contract and Opportunity. I did create a lookup relationship with the trigger. The problem I am having with this trigger is that when one of the opportunities changes or deleted the Total Opp Amount in Contract Object does not update. I would be ecstatic if you can help me on this. As I have spent too much time and $ on this project.





trigger updateContract on Opportunity (before Update) {

set<id> contIds = new set<id>();
list<contract> listcon = new list<contract>();
set<id> oppIds = new set<id>();
Double InitialSum ;

For(Opportunity o

Map<id,contract> MapofCons = new Map<id,contract>([select id, name,Total_Opportunity_Amount__c from contract where id in :contIds]);
Map<id, opportunity> MapOfOpps = new Map<id,opportunity>([select id,name, amount from opportunity where id in :oppIds]);
For(opportunity o:

If( MapofCons.get(o.contract__c).Total_Opportunity_Amount__c ==null)
InitialSum = 0 ;
InitialSum = MapofCons.get(o.contract__c).Total_Opportunity_Amount__c;

If((MapOfOpps.get( (o.amount!=null))
{ MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum + o.amount;
else If((MapOfOpps.get(!=null)&& (o.amount==null))
{ MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum ;
if(mapofOpps.get( != o.amount)
{ MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum + o.amount- mapofOpps.get( ;
update Listcon;






I think this could be solved with a simple relationship & trigger as Dirk mentioned above.


Here is the approach in wannabe code to give you a head start:

trigger on Opportunity{
	Set<Id> opportunityIds = new Set<Id>();
	for(Opportunity thisOpportunity :{

	for(Contract thisContract : [SELECT Amount, 
(SELECT Amount FROM Opportunity__r)
FROM Contract
WHERE OpportunityId IN: opportunityIds]){ Integer totalAmount = thisContract.Amount; for(Opportunity thisContractOpportunity : thisContract.Opportunity__r){ totalAmount = totalAmount + thisContractOpportunity.Amount; } } }

 This example is missing a few bits and bobs here and there but should give you an idea how you can achieve the totalSum.


Hope this helps!




Could you help me finish this? And how much would it cost. I'm not a APEX programmer.