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
twrig15twrig15 

How to calculate a field based on a picklist value

Hello,

I'm trying to calculate a field based on a specific picklist value. Not sure if I need a Apex trigger or a formula.

If a picklist value is changed to a specfic value and then count all the related list records custom hours field from that point. 

Example: Pick Name: Type of Training, Value = Online 
At this one whenever a record meets this criteria. All training hours created should be counted. But should only be counted when the above value is true.
Best Answer chosen by twrig15
twrig15twrig15
Solved with a formula. See: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DEYbIAO

All Answers

Matthew Kowalski 3Matthew Kowalski 3
I think this must be done in an Apex Trigger. To my knowledge, you can't set conditional Rollup Summary fields.
Mahesh DMahesh D
Hi Twrig,

Yes we can achieve this using the Trigger. Please provide the Object and Field API Names along with an example so that it will be easy to help you.

Regards,
Mahesh
twrig15twrig15
Thank you Matthew and Mahesh for your help.

Mahesh,
Below is the validation rule created but only works after the record is created. Is as all the Object and API inform required.

Example:
Account__r.Total_Of_Hours_Purchased__C = 10 
Account__r.Total_of_Hours_Used__c = 8
A rep goes to create a training for this customer for 3 hours. The rep should not be able to save the record because the training hours will exceed the 10 hours purchased.

- Account__r.Total_of_Hours_Used__c is a roll up summary field that sums up all the training hours from the Training related list on the Account.

This trigger would created on the Training Custom object (API Name:Training__c).

Current Validation Rule:
AND(Account__r.Total_of_Hours_Used__c > Account__r.Total_Of_Hours_Purchased__c, ISPICKVAL( Account__r.Training_Billing_Type__c, "Prepaid"),
AND( ISNEW() 
)
)


Thank you!
 
twrig15twrig15
Solved with a formula. See: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DEYbIAO
This was selected as the best answer