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
raj jordanraj jordan 

All fields in a Custom metdata type should be required

hi all,

i want to create a custom metadata object then i want to add fields to it
the fields which are added to that object should become required 
can you please help me
Best Answer chosen by raj jordan
Dmitry OfitserovDmitry Ofitserov
Hi raj,
Sorry it took me some time to respond.
Please try the following code:
trigger AccountTrigger on Account (before insert, before update) {

  if(Trigger.isInsert){
      
    //Getting all the records from custom metadata
    Account_Custom_Metadata__mdt[] accountMetadataFields = [SELECT Account_Field_Name__c FROM       Account_Custom_Metadata__mdt];
	
    //Iterating throug all the accounts in the trigger 
    //and checking their corresponding field values against custom metadata entries.
    for(Account a : Trigger.new){
      Set<String> missingFields = new Set<String>();
      for(Account_Custom_Metadata__mdt acm : accountMetadataFields){
        if(a.get(acm.Account_Field_Name__c) == null){
          missingFields.add(acm.Account_Field_Name__c + ',');
        }
      }
      if(missingFields.size() > 0){
        String fields = '';
        for(String s : missingFields){
          fields += s;
        }
        a.addError('The following fields cannot be empty: ' + fields);
      }
    }
  }

}

 

All Answers

Dmitry OfitserovDmitry Ofitserov
Hi raj,
The field of Custom Metadata object can be set to required during the field creation (as it is possible when creating a custom field for any object).

Please let me know if you want me to send you a step-by-step instructions on Custom Metadata creation.
raj jordanraj jordan
thanks Dmitry for ur reply but i want to make it required using trigger 
can you help me through this
 
Dmitry OfitserovDmitry Ofitserov
Hi raj,
Unfortunately it is imposible to create a trigger for Custom Metadata object.
The only possible way to make fields required is to create those fields as required or use validation rules for those fields.
raj jordanraj jordan
thanks Dmitry
My requirement is i want to make some of the account fields mandatory by using custom metadata 

Can we do this
if so can u help me 
Dmitry OfitserovDmitry Ofitserov
Hi raj,

Can you please give me an example for such a case ? How is Custom Metadata used in this case ?

Thanks,
Dmitry
raj jordanraj jordan
i will create one custom metadata type and add one field with one name
and the fields which i want to make required in account will be added as records in custom metadata
now i have to write a trigger on account such that whenever the account is inserted the those fields should be mandatory
 
Dmitry OfitserovDmitry Ofitserov
Hi raj,
I see. So, before saving an Account you want to check that if one of the field names written in Custom Metadata is empty on Account record - you want to return an error message and not to save the Account record ? Is that right ?

Thanks,
Dmitry
raj jordanraj jordan
yes Dmitry
 
Dmitry OfitserovDmitry Ofitserov
Ok, that is possible.
Just give me a few minutes and I will send you a sample trigger code for this.
raj jordanraj jordan
ha sure 
 
Dmitry OfitserovDmitry Ofitserov
OK, let's say you called your Custom Metadata object "Account Custom Metadata" and created one field called "Account Field Name" and created a few entries of that metadata.
Now you need to create a trigger on Account object that looks like that:
trigger AccountTrigger on Account (before insert, before update) {

  if(Trigger.isInsert){
      
    //Getting all the records from custom metadata
    Account_Custom_Metadata__mdt[] accountMetadataFields = [SELECT Account_Field_Name__c FROM       Account_Custom_Metadata__mdt];
	
    //Iterating throug all the accounts in the trigger 
    //and checking their corresponding field values against custom metadata entries.
    for(Account a : Trigger.new){
      for(Account_Custom_Metadata__mdt acm : accountMetadataFields){
        if(a.get(acm.Account_Field_Name__c) == null){
          a.addError('Field ' + acm.Account_Field_Name__c + ' cannot have empty value.');
        }
      }
    }
  }

}
That should do the validation.
Please let me know if you have any questions.

Thanks,
Dmitry
 
raj jordanraj jordan
Thanks Dmitry its working fine for insert
but while updating it is not showing error
 
Dmitry OfitserovDmitry Ofitserov
Hi raj,
If you want it to work for update validations just remove the IF statement and it will work for both update and insert.
Your final version of trigger should look like this:
trigger AccountTrigger on Account (before insert, before update) {
      
    //Getting all the records from custom metadata
    Account_Custom_Metadata__mdt[] accountMetadataFields = [SELECT Account_Field_Name__c FROM       Account_Custom_Metadata__mdt];
	
    //Iterating throug all the accounts in the trigger 
    //and checking their corresponding field values against custom metadata entries.
    for(Account a : Trigger.new){
      for(Account_Custom_Metadata__mdt acm : accountMetadataFields){
        if(a.get(acm.Account_Field_Name__c) == null){
          a.addError('Field ' + acm.Account_Field_Name__c + ' cannot have empty value.');
        }
      }
    }
}

Regards,
Dmitry
 
raj jordanraj jordan
Thanks Dmitry it is working for update also
thank for ur time.
Dmitry OfitserovDmitry Ofitserov
You're welcome raj.
I'm happy it helped.
raj jordanraj jordan
hi Dmitry 
how can i display all errors at a time ?
the above code is just displaying one error at a time 
can you please look at this

Thanks in Advance
Dmitry OfitserovDmitry Ofitserov
Hi raj,
Sorry it took me some time to respond.
Please try the following code:
trigger AccountTrigger on Account (before insert, before update) {

  if(Trigger.isInsert){
      
    //Getting all the records from custom metadata
    Account_Custom_Metadata__mdt[] accountMetadataFields = [SELECT Account_Field_Name__c FROM       Account_Custom_Metadata__mdt];
	
    //Iterating throug all the accounts in the trigger 
    //and checking their corresponding field values against custom metadata entries.
    for(Account a : Trigger.new){
      Set<String> missingFields = new Set<String>();
      for(Account_Custom_Metadata__mdt acm : accountMetadataFields){
        if(a.get(acm.Account_Field_Name__c) == null){
          missingFields.add(acm.Account_Field_Name__c + ',');
        }
      }
      if(missingFields.size() > 0){
        String fields = '';
        for(String s : missingFields){
          fields += s;
        }
        a.addError('The following fields cannot be empty: ' + fields);
      }
    }
  }

}

 
This was selected as the best answer
raj jordanraj jordan
Thanks Dmitry its working fine