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
Sunny SohailSunny Sohail 

Cross Object Trigger to make a filed Mandatory


Hello,

im fairy new to triggers and im having some issues creating a trigger that will make a field mandatory. I tried to do this with a basic Validation rule however i beleive this exceeds salesforce point and click capabilities.

Case:
Im trying to create a validation rule where a user would be required to fill out the "Distributor_Name__c" name on the quotes object if "silver" is slected from the "Proofpoint_Advantage_Program_Level__c" field on the accounts object.

"Distributor_Name__c"  is a lookup field that looks up accounts and "Proofpoint_Advantage_Program_Level__c"  is a picklist.

i have tried creating a cross object validation rule however on the quotes object account fields do not show up. Is this possible? if not would this be done via trigger? This is what i attempted however iv had little success. Any help would be appreciated.

trigger Requiredistyfield on Quotes (Before insert,before update) {
    for (account quoteloop: Trigger.New){
        if (quoteinloop.Proofpoint_Advantage_Program_Level__c== 'silver'l)
            accountinloop.Distributor_Name__c!=NULL;
            }
        }


 
Best Answer chosen by Sunny Sohail
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello ,

Please find the below code sample, 
 
Trigger RequiredFieldTrigger on Quote (before insert,before update) {
    Map<ID, Account> parentAccs = new Map<ID, Account>();
    
    List<Id> listIds = new List<Id>();   
   
   for (Quote childObj : Trigger.new) {
        listIds.add(childObj.Account);      
    }
    
    parentAccs = new Map<Id, Account>([SELECT id, Proofpoint_Advantage_Program_Level__c FROM Account WHERE ID IN :listIds]);
    
    for (Quote child : Trigger.new){
     Account parentAcc = parentAccs.get(child.Account);
     if(parentAcc.Proofpoint_Advantage_Program_Level__c == "silver" && (child.Distributor_Name__c == NULL || 
child.Distributor_Name__c == '')){	
	child.addError('Please Choose Distributor Name');
     }
  }

}

Thanks,
Vinoth

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello ,

Please find the below code sample, 
 
Trigger RequiredFieldTrigger on Quote (before insert,before update) {
    Map<ID, Account> parentAccs = new Map<ID, Account>();
    
    List<Id> listIds = new List<Id>();   
   
   for (Quote childObj : Trigger.new) {
        listIds.add(childObj.Account);      
    }
    
    parentAccs = new Map<Id, Account>([SELECT id, Proofpoint_Advantage_Program_Level__c FROM Account WHERE ID IN :listIds]);
    
    for (Quote child : Trigger.new){
     Account parentAcc = parentAccs.get(child.Account);
     if(parentAcc.Proofpoint_Advantage_Program_Level__c == "silver" && (child.Distributor_Name__c == NULL || 
child.Distributor_Name__c == '')){	
	child.addError('Please Choose Distributor Name');
     }
  }

}

Thanks,
Vinoth
This was selected as the best answer
Sunny SohailSunny Sohail
Hi Vinoth,

this code actually gave me some syntax errors however i was able to do this via validation rule.

thank you so much for your help.