You need to sign in to do that
Don't have an account?
Otto2020
Apex Class to look up a related list on a custom object and return an error if there's more than one record
I have a custom agreement object that is associated to a custom order object which shows up as a related list on the agreement.
I would like to write a line of apex code that: upon save ie before update of the agreement , check the order related list, and if there is more than one order in it, then, throw an error asking the user to remove all other oder records, except for the latest one.
Only allow the user to save the record after this action is taken
Any ideas on how I can write this line of code?
I would like to write a line of apex code that: upon save ie before update of the agreement , check the order related list, and if there is more than one order in it, then, throw an error asking the user to remove all other oder records, except for the latest one.
Only allow the user to save the record after this action is taken
Any ideas on how I can write this line of code?
Hi Otto. What the above does is pass the newMap (Trigger context variable) into a helper class which then fetches the Order Counts using a SOQL query and determines if any of them are too large. Feel free to contact me directly if I can help clarify.
Now 'm getting this error:
I think if I can solve for this then the above code is good?
You are adding the error message on the Apttus_Config2__Order__c object which is not a valid case. You can only add the error message on the trigger context objects. You can use the below code for your requirement:
Please let me know if you need any other help on this.
Thanks,
Abhishek Bansal.
<pre>
trigger validateOrders on Apttus_CMConfig__AgreementId__c( before update )
{
for ( AggregateResult result :
[ SELECT COUNT(Id), Apttus_CMConfig__AgreementId__c
FROM Apttus_Config2__Order__c
WHERE Apttus_CMConfig__AgreementId__c IN :Trigger.new
GROUP BY Apttus_CMConfig__AgreementId__c
HAVING COUNT(Id) > 1
]
)
{
Trigger.newMap.get( (Id) result.get( 'Apttus_CMConfig__AgreementId__c' ) )
.addError( 'Your error message here.' );
}
}
</pre>