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
gregj777gregj777 

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!

 

gregj777gregj777

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.

 

Thanks,

Greg

 

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 :trigger.new)
{
contIds.add(o.contract__c);
oppIds.add(o.id);
}

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: trigger.new)
{


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

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

}

 

florianhoehnflorianhoehn

Hi,

 

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 : trigger.new){
		opportunityIds.add(thisOpportunity.Id);
	}

	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!

 

Florian

gregj777gregj777

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