We have a community-based application on Salesforce App Exchange. We have a new requirement from one of our clients that they want to use the same application for 2 different departments which are using the same Org.
These departments have different use cases and they must not be able to share data or be able to access each others data (Multi tenant feature ). So we are looking to implement this feature without code change or minimal code change in our product.
We are trying to use record type. According to my understanding, following approach should work.
- Create record type on the custom object
- Define its value as "Tenant1", "Tenant2", "Tenant3"...
- Define a record type default value for different profiles (In our application there are 4 types of users or profiles, and the same page have different data/feature according to profiles).
i.e. Profile11 -> Tenant1; Profile12 -> Tenant2; Profile13 -> Tenant3;...
- So when we insert any data in our custom object, then it uses its defined default value and populate it without explicitly defining in code.
- Query custom object without explicitly defining record type, The APEX query should return only record which is mapped with record type.
Problem which I am facing:
I am able to insert data without changing code using above approach but when I am trying to get data using APEX code then I am getting all data(which are not mapped with user profile. i.e. If I query with Tenant1 profile user then I am getting all data belongs to Tenant1, Tenant2, Tenant3, ....)
So I have two questions:
1st - Does my approach/understanding correct ?
2nd - Can we restrict data query to retrieve only data with defined record type using APEX code without defining in query ?
If my approach is incorrent then please suggest me possible solution.
Edit : My approach to retrive data via Record Type and Profile is not feasible in salesforce Environment. I found one 11 year old unimplemented same feature request on Saleforce site. If other people also like this feature in salesforce then please upvote this feature.