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
Abilash.SAbilash.S 

Restrict user to create record when user selects particular value in picklist.

There is an account and contact which have the relationship. In contact there is a picklist which is mandatory. Say for eg picklist contains states of India. If user selects Maharashtra state, then that user should not allow to create a record in contact. How could I accomplish this. Please provide required coding part.
Best Answer chosen by Abilash.S
CharuDuttCharuDutt
Hii Pavushetti Abhilash
Try The Below Trigger
1. pickval__c = Is Picklist Field
2. val2 Picklist Value When User Select This Value It Restricts User To Create Record

trigger restrict on Contact (before insert) {
    if(trigger.IsBefore && trigger.IsInsert){
        for(Contact con:trigger.new){
            if(con.pickval__c == 'val2'){
                con.pickval__c.addError('Cant select this picklist value');
            }
        }
    }
}

Please Mark it As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Pavushetti Abhilash
Try The Below Trigger
1. pickval__c = Is Picklist Field
2. val2 Picklist Value When User Select This Value It Restricts User To Create Record

trigger restrict on Contact (before insert) {
    if(trigger.IsBefore && trigger.IsInsert){
        for(Contact con:trigger.new){
            if(con.pickval__c == 'val2'){
                con.pickval__c.addError('Cant select this picklist value');
            }
        }
    }
}

Please Mark it As Best Answer If It Helps
Thank You!
This was selected as the best answer
Abilash.SAbilash.S
Excellent Charu soni, I implemented. Its working. Thanks a lot.
CharuDuttCharuDutt
Hii Pavushetti
Please Mark it As Best Answer If It Helps So It Can Helps Others 
Thank You!
Abilash.SAbilash.S
Marked as best answer. Thankyou
Suraj Tripathi 47Suraj Tripathi 47

Hi Pavushetti,

Please go through the code.

trigger ForContact on Contact (before insert) {
    if(trigger.IsBefore){
     if(trigger.IsInsert){
  for(Contact con:trigger.new){
            if(con.Salutation== 'Dr'){
                con.Salutation.addError('You can not select Dr');
                                                  }
                                                }

                                       }  
                                  }
}

Here Salutation is a PickList field on Contact.

Thank You!