You need to sign in to do that
Don't have an account?
chandra prakash 58
validate sum of child record based upon parent record
I have two custom objecrt 1. stock_register__c 2. stock_regirser_line__c
i have master detail relation field quantity__c with stock_register__c(parent object) and stock_regirser_line__c (chield)
I need to write a before insert and before update trigger to restrict the sum of quantiy__in stock_regirser_line__c can not be more that allocated_quanity__c in the parent object(stock_register__c)
Please help!!
thanks in advance!!
i have master detail relation field quantity__c with stock_register__c(parent object) and stock_regirser_line__c (chield)
I need to write a before insert and before update trigger to restrict the sum of quantiy__in stock_regirser_line__c can not be more that allocated_quanity__c in the parent object(stock_register__c)
Please help!!
thanks in advance!!
Please let me know if the below code fulfill your requirements.
Apex Trigger
Test Class with !00% coverage
All Answers
For reference, you can check below,
https://developer.salesforce.com/forums/?id=9060G000000UaBLQA0
https://salesforce.stackexchange.com/questions/211883/validation-on-the-cumulative-sum-of-a-field-on-child-records#:~:text=Probably%20the%20easiest%20way%20to,the%20update%20to%20the%20parent.
If it helps you and closes your query by marking it as the best answer so that it can help others in the future.
Thanks.
Please let me know if the below code fulfill your requirements.
Apex Trigger
Test Class with !00% coverage
Map<Id, Stock_Register_ine__c> currentQuantityMap = new Map<Id, Stock_Register_ine__c>();
Map<Id, Decimal> totalQuantityMap = new Map<Id, Decimal>();
Map<Id, Decimal> incomingQuantityMap = new Map<Id, Decimal>();
for(Stock_Register_ine__c srl : trigger.new)
{
if(incomingQuantityMap.containsKey(srl.Stock_Register1__c)){
Decimal tempQuantity = incomingQuantityMap.get(srl.Stock_Register1__c) + (srl.Allocation_quantity_for_this_month__c == null ? 0 : srl.Allocation_quantity_for_this_month__c);
incomingQuantityMap.put(srl.Stock_Register1__c, tempQuantity);
}else{
incomingQuantityMap.put(srl.Stock_Register1__c, srl.Quantity__c == null ? 0 : srl.Quantity__c);
}
currentQuantityMap.put(srl.Stock_Register1__c, srl);
}
for (Stock_Register__c stockReg : [SELECT Id, Actual_Quantity_For_Allocation__c, (SELECT Allocation_quantity_for_this_month__c FROM Stock_Register_ine__r)
FROM Stock_Register__c
WHERE Id = :currentQuantityMap.keySet()])
{
decimal currentQuantity = 0.0;
decimal allocatedQuantity = stockReg.allocated_quanity__c == null ? 0 : stockReg.allocated_quanity__c;
decimal incomingQuantity = incomingQuantityMap.get(stockReg.Id);
if(stockReg.Stock_Register_ine__r != null && !stockReg.Stock_Register_ine__r.isEmpty())
{
for(Stock_Register_ine__c srlRec : stockReg.Stock_Register_ine__c)
{
if(srlRec.Id == null || srlRec.Id != currentQuantityMap.get(stockReg.Id).Id)
currentQuantity =+ (srlRec.Quantity__c == null ? 0 : srlRec.Quantity__c);
}
} system.debug('currentQuantity : ' + currentQuantity);
if(allocatedQuantity < (currentQuantity + incomingQuantity))
currentQuantityMap.get(stockReg.Id).addError('Total stock register line quantity cannot be greater than allocated quatity');
}
}
**********************************************************************************************************
for (Stock_Register__c stockReg : [SELECT Id, Actual_Quantity_For_Allocation__c, (SELECT Allocation_quantity_for_this_month__c FROM Stock_Register_ine__r)
FROM Stock_Register__c
WHERE Id = :currentQuantityMap.keySet()])
i am getting the error in this line :
error:
Allocation_quantity_for_this_month__c FROM Stock_Register_ine__r)
^
ERROR at Row:1:Column:98
Didn't understand relationship 'Stock_Register_ine__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Please check the master detail field on stock_register_line sobject, what is the name? replace that here with __r
code successfully saved but still, I am getting some error.
If possible please connect 8840330440. .
It will greater full for me .
Will call you.
You need to change fields with your names. Anyway, I have done for you, hope the below code will work
Change the test class accordingly