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
Carrlos BoydCarrlos Boyd 

Prevent opening a new record on a custom object depending on "status" field

I have a custom object (object1) that has multiple objects from another custom object (object2). There are three statuses for records object2: In Progress, Cancelled, and Completed. As long as there is an In Progress or Cancelled record, I want to avoid the creation of a new record related to a single object1 record. The idea is for there to only be one step/process "In Progress" at any given time until the object1 record is "completed".
Best Answer chosen by Carrlos Boyd
Aamir KhanAamir Khan
You can do it with validation rules. Create a validation rule on child objcet with below formula:
If(
OR(
 ISPICKVAL( <yourfieldname>, "In Progress" ),
 ISPICKVAL( <yourfieldname>, "Cancelled" )
),
ISNEW(),
false
)

Hope it helps.

Please like if it helps, .
Please mark solved if it works.

Mahalo,
Aamir AH Khan,
Salesforce Expert

All Answers

Aamir KhanAamir Khan
Aloha Carrlos,

As I understood you have 2 objects, Object 1 and Object 2. Which relationship is between both objects?

As I assum that Object 1 is parent and Object 2 is child, so you want to avoid creation of parent until the status of child equals "Completed".

If you want to achive this with configuration for that you need to make sure that the realtionship shpuld be Master-Details relationship between both objects.

Then create a Rollup Summary formula on Object 1 (Parent) which count the child records with filter criteria i.e. Status equals Completed or not equals "Completed" (Based on your criteria). 

Hope it helps.

Please like and mark solved if it works.

Mahalo,
Aamir AH Khan,
Salesforce Expert
Carrlos BoydCarrlos Boyd
Object1 is the parent and Object2 is the child. There are multiple child records but I want to avoid having more than one "In Progress" at the same time. Also, I don't want anymore child records created once one of them is status "Cancelled". Hope this helps.
Aamir KhanAamir Khan
You can do it with validation rules. Create a validation rule on child objcet with below formula:
If(
OR(
 ISPICKVAL( <yourfieldname>, "In Progress" ),
 ISPICKVAL( <yourfieldname>, "Cancelled" )
),
ISNEW(),
false
)

Hope it helps.

Please like if it helps, .
Please mark solved if it works.

Mahalo,
Aamir AH Khan,
Salesforce Expert
This was selected as the best answer
Carrlos BoydCarrlos Boyd
Works perfectly, thanks!
Carrlos BoydCarrlos Boyd
Upon further testing, this method conflicts with another Process Builder process I have created. As each child record is completed, the next child record in my process is created. Since the current child record is "In Progress", the above solution does not allow me to create a new child record. Before my change from "In Progress" to "Completed" the above rule is checking to see if there is a current "In Progress" child record, which there is. So basically I cannot change the status to "Completed" so the next child record is created. How can I avoid this?
Aamir KhanAamir Khan
Ohh, okay so you can go with this validation rule for Cancelled value.
If(
 ISPICKVAL( <yourfieldname>, "Cancelled" ),
ISNEW(),
false
)

For "In Progress" you can go with Duplicate Rule.

Go to Setup> Quick Find Box> Type "Duplicate Rules" > Select your Object and Create New Rule>

Select you picklist field and the value (In Progress).

Hope it helps.
Carrlos BoydCarrlos Boyd
So if I allow duplicate records, won't this allow someone to create a child record as "In Progress"?
Aamir KhanAamir Khan
You can add more criteria along with that to refine the duplicate rule which will restrict only that record, which is not eligible as per the business.

OR
You can go with Rollup Summary which I described in first comment.

OR
We need to write a trigger to get all those records which have status = In Progress and then show the error message usin addError method.

You can go with anyone which is good according to your business process.

Mahalo.
Carrlos BoydCarrlos Boyd
Is there anyway to delay the validation rule until I can have Process Builder set the status as "Completed"?
Aamir KhanAamir Khan
No we can't as per the order of execution Validation rules will fire first then Process Builder. Please check the link below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
 
Carrlos BoydCarrlos Boyd
I'm still confused on how to allow the current 'In Progress' record be changed and saved as 'Completed' before the validation rule checks it before creating the next child record. Can you give me a screenshot of what the extra duplicate rules criteria would look like?
Carrlos BoydCarrlos Boyd
In other word, I don't want the validation rule that prevents duplicate child records in status 'In Progress' to prevent creating the next step that I have configured in Process Builder. It appears that there is a fraction of a moment when there would technically be two In Progress records.