There are two object Contract and Subscription.There is Lookup relationship between both. Contract is parent and Subscription is child object.
The child-relationship name between two is: Subscriptions
I have written Trigger on Subscription object to Count number of subscriptions and display that to Contract. Below is my trigger:
trigger CountContract on SBQQ__Subscription__c (after INSERT, after UPDATE,
after DELETE ) {
Set <Id> contractIds = new Set <Id>();
List <Contract> lstContractsToUpdate = new List <Contract>();
if(Trigger.isInsert){
for(SBQQ__Subscription__c s:trigger.new){
contractIds.add(s.SBQQ__Contract__r.id);
}
}
if(Trigger.isUpdate|| Trigger.isDelete){
for(SBQQ__Subscription__c s:trigger.old){
contractIds.add(s.SBQQ__Contract__r.id);
}
}
system.debug(contractIds);
for(Contract con:[SELECT Id,Number_of_Subscriptions__c,(Select Id from
SBQQ__Subscriptions__r) from Contract where Id IN: contractIds]){
Contract conObj = new Contract ();
conObj.Id = con.Id;
conObj.Number_of_Subscriptions__c = con.SBQQ__Subscriptions__r.size();
lstContractsToUpdate.add(conObj);
}
UPDATE lstContractsToUpdate;
}
The code is getting saved without any error.But the field on Contract called: Number of Subscription is not getting any updated.
trigger.new
. If you update the ContractId on a given subcription, then the new contract will not get updated.