+ Start a Discussion
Rupesh A 8Rupesh A 8 

How to write validation rule for making field is required

Hi,

Scenario is, In Account object I have "Legal Entity" field, as we all know that there must be a relationship between Oppurtunity and Account objects.
So in Oppurtunity I have created one formula field like "Account Legal Entity" (formula is TEXT(Account.Legal_Entity__c)). Here I need to make this field as mandatory by using Validation rule. can anyone help me on this?
Thanks.
Swaraj Behera 7Swaraj Behera 7
You can not make a formula field required.What you can do is make the input field in the formula field as required so formula field gets created all the time if it has the value.

Thanks
Rupesh A 8Rupesh A 8
Hi Swaraj,

Actually I should not keep input field as required like that as per my requirement. ok let's try another one, if we dont have any formula field like Account Legal entity. As usual there must be a relationship b/w Account and Oppurtunity right if I create oppurtunity record then in any Account record is having the Legal Entity then the Oppurtunity record should create otherwise it should throw the error.
How can we acheive this? Thanks.
sushant sussushant sus
Hi Rupesh ,
for this you have to write trigger  create a field in opportunity "Account Legal Entity" in below trigger i am expecting api name as "Account_Legal_Entity__c " i am writting logic in trigger you can move it to class also .

Trigger Account_Legal_Entity on Opportunity(before insert){
 set<id> accid=new set<id>();
    if(Trigger.isbefore&&Trigger>isInsert){
             for(Opportunity op:Trigger.new){
                    accid.add(op.accountid);
             } 
            Map<id,Account> maaccount=new Map<id,Account>([select id,Account_Legal_Entity__c from Account where id IN:accid]);  
            for(opportunity op:Trigger.new){
                Account ac=maaccount.get(op.accountid);
                    if(ac.Account_Legal_Entity__c!=null){
                       op.Account_Legal_Entity__c=ac.Account_Legal_Entity__c ;
                    }
                    else{
                      op.addError('Opportunity's Account don't have Account_Legal_Entity__c ');
                    }    
               }  
    }

}

If its helps ples like this :)