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
Susanta ChakrabortySusanta Chakraborty 

How to restrict record type in list view through apex triggers or class

Hi Team

Could anyone write the codes which help me to restrict record type in list view in one object.
I have one object called "Person" which has 4-5 record types and 4-5 page layout assign with particular record type. There are 2 record types, which are "Staff" & "Limited Staff". Staff record type has similar called page layout with so many fields and Limited staff record type also has similar page layout which restrict with some fields.

Now I want if User ID <> Owner ID then Limited staff record type and page layout should open from list view, other-wise Staff record type and page layout should open for the  true critiria.... User ID=Owner ID.

Please let me know if anyone can help me out. I am not very familiar with apex codes
GauravGargGauravGarg

Hi Susanta,

We cannnot manage ListView of an object through apex, i.e. directly handled by Salesforce. 

What we can do to achieve this is to create Custom VF page for List view that will check the possible condition and display data accordingly. 

Thanks,

Gaurav
Skype: gaurav62990

Susanta ChakrabortySusanta Chakraborty
Hi Gaurav

Thanks for prompt reply. Could you please provide me the Custom VF page code so that I can impelment this to our Production.

Thanks.
GauravGargGauravGarg
Hi Susanta,

Please go through this link:

https://salesforce.stackexchange.com/questions/32067/custom-list-view-to-display-vf-page.

Hope this helps!!!

Thanks,
Gaurav
Susanta ChakrabortySusanta Chakraborty
This is much more complicated for me. Could you please provide me some codes, usign this, where I can restrict at least some fields in same record type and page layout if UserID <> Owner ID

Thanks.
GauravGargGauravGarg
Hi Susanta,

This will be complicated, as you need to prepare the VF page and its controller class to manage the requirements. 

Or you can prepare the Details / edit page layout with the above condition. 

Thanks,
Gaurav
Skype: gaurav62990 
Susanta ChakrabortySusanta Chakraborty
Ok let me know one thing, Can I restrict some fields in the pagelayout using simple validation if above condition is true. If yes then which function should work for this.

Thanks.
GauravGargGauravGarg
You can, add validation rule so that Use would not be allowed to edit those fields, 

Visibility of the fields are defined based on following:
1. Profile
2. Page Layout assignment. 
3. Record Type Access. 
4. Object Level Permission.
5. Field Level Permission. 
6. Custom VF page / Controller class. 

THanks,
Gaurav
Susanta ChakrabortySusanta Chakraborty
I actually know the process for field editing restriction. But I want to make invisible some fields If UserID<> Owner ID which cannot do through simple validation.

Thanks.
GauravGargGauravGarg
You need to write apex code to achieve this. 

Let me know if you need more help on this. 

Thanks,
Gaurav
Skype: gaurav62990
Susanta ChakrabortySusanta Chakraborty
Yes I also thought so but problem is I am not familiar with these codes, therefore, I can't write new codes and can't implement it as well. That's why I need the help who can provide me at least the basic code and I can modify this as per my object name and fields name....

If you can help me out then please let me know.

Thanks.
GauravGargGauravGarg
Hi Susanta,

This is not that easy, and would require some time to develop this code.  Please provide some time to me to help you in this. 

THanks,
Gaurav
Susanta ChakrabortySusanta Chakraborty
That's not a problem, I am not in hurry. If you can do this then it will be great help for me.

For your help I am providing some details...
Object name: Staff__c
Restrited field names are: Account_Savings_Current__c (picklist field), Age__c (formula number field), Bank_Name__c (Text field) and so on.... which I can modify to see any one or two restriction.

Logic will be, If UserID <> Owner ID then these fields should not visible to others.

Let me know if you need anything further.

Thanks.
Susanta ChakrabortySusanta Chakraborty
Hi Gaurav

Any update which we have discussed couple of days ago.....

Thanks.
GauravGargGauravGarg
Hi Susanta,

I have made a small list view on Account object. Please modify it as per your requirement. 
VF page:
<apex:page standardController="Account" extensions="accountListView" sidebar="false" showHeader="false">
  <apex:form >
      <apex:messages />
      <apex:pageBlock >
          <apex:pageblockButtons >
              <apex:commandButton action="{!save}" value="Save"/>
              <apex:commandButton action="{!cancel}" value="Cancel"/>
          </apex:pageblockButtons>
          <apex:pageBlockTable value="{!Accounts}" var="acc" title="Account List">
              <apex:column value="{!acc.name}"/>
              <apex:column value="{!acc.accountId}" rendered="{!acc.isOwnerView}"/>
          </apex:pageBlockTable>
              
          
      </apex:pageBlock>
  </apex:form>
</apex:page>
 
Controller:

public with sharing class accountListView {
    public List<AccountWrapper> accountList;
    
    public accountListView(ApexPages.StandardController controller) {
           accountList = new List<AccountWrapper>();
    }
    public List<AccountWrapper> getAccounts(){
        String userId = UserInfo.getUserId();
        accountList = new List<AccountWrapper>();
        for(Account acc: [SELECT id, name, owner.id FROM Account limit 100]){
            AccountWrapper aw = new AccountWrapper();
            if(acc.owner.id == userId){
                aw.name = acc.name;
                aw.accountid = acc.id;
                aw.isOwnerView = true;
            }else{
                aw.name = acc.name;
                aw.isOwnerView = false;
            }
            accountList.add(aw);
        }
        
        return accountList ;
    }
    
    public class AccountWrapper{
        public String name{get;set;}
        public String accountId{get;set;}
        public Boolean isOwnerView{get;set;}
        public AccountWrapper(){
            name = 'firstrecord';
            accountId = '001';
            isOwnerView = true;
        }
    }
    
}
Wrapper is the list which we are displaying over Page. You can add multiple field which need to be displayed on page.

"isOwnerView = true"  will show all the fields and make it "False" to hide field value.

Hope this helps!!!

Thanks,
Gaurav
Skype: gaurav62990
 
Susanta ChakrabortySusanta Chakraborty
Sorry Gaurav, its very complicated for me to modify, I am not the developer as I said. I even can't understand how to modify object name. using the codes. Could you please help me out some other way. We have reached out to one salesforce developer concern to implement it. They actually design our Organization at the very beginning but now they charge heavy amount to imeplement this. We are the non profit organization and can't afford too much money for this.

Thanks.
GauravGargGauravGarg

Hi Susanta,

Yes, you can reach out to me on Skype: gaurav62990. 

I will help you build this. 

Thanks,

Gaurav