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
R557R557 

Custom Metadata

Hi , I am trying to write a trigger for the following scenario :

I have a custom metadata set named xyz_mdt which has custom field Hub__c(custom field for hubs like state names) and the MasterLabel field contains the corresponding cities.

There is picklist Origin__c field on OpportunityLineItem object which will contain the name of the Hub to be selected by the user. A text field OriginCIty__c  will be entered by the user, and based on the corresponding Hub name from the custom metadata, if the entered Origin City doesn't matches Hub, trigger will stop the user to do so, by prompting an error.

Please help!
Best Answer chosen by R557
chanchal_:)chanchal_:)
Or you can use this,
Let me know  if it helps.
trigger OpportunityTrigger on Opportunity (before insert) {
    List<string> ListOliOrigins = new List<string>();
    for(Opportunity opp: trigger.new){
        ListOliOrigins.add(opp.Origin__c);
    }
    List<string> ListCities = new   List<string>();
     for(StateWiseCities__mdt oswc :  [SELECT masterlabel, hub__c  FROM StateWiseCities__mdt 
                                                  WHERE hub__c = :ListOliOrigins]){
        ListCities.add(oswc.MasterLabel);
    }
   
    for(Opportunity opp: trigger.new){
       
            if(!ListCities.contains(opp.OriginCIty__c)){
                opp.addError('city is not related to respective state');
            }
        }
    }


 

All Answers

chanchal_:)chanchal_:)
Hey Does your metadata has multiple records? Like same hub__c  for different masterLabels. i.e. same state name for multiple cities. ?
R557R557
Metadata has different hub names and a set of corresponding unique masterlabels . For e.g. Delhi/NCR will have a set of 5-7 masterlabels , Punjab will have different 5-7 masterlabels , so on and so forth.
chanchal_:)chanchal_:)
can I see a snapshot of your metadata records ?
 
R557R557
This is for one of the Hubs
This is for one of the Hubs.
 
chanchal_:)chanchal_:)
Is it correct ??

User-added image
R557R557
Yes
chanchal_:)chanchal_:)
I created this on the opportunity object
 
trigger OpportunityTrigger on Opportunity (before insert) {
    List<string> ListOliOrigins = new List<string>();
    for(Opportunity opp: trigger.new){
        ListOliOrigins.add(opp.Origin__c);
    }
    List<StateWiseCities__mdt> ListMasterLabels= [SELECT masterlabel, hub__c 
                                                  FROM StateWiseCities__mdt 
                                                  WHERE hub__c = :ListOliOrigins];
    List<string> ListCities = new   List<string>();
     for(StateWiseCities__mdt oswc :  ListMasterLabels){
        ListCities.add(oswc.MasterLabel);
    }
   
    for(Opportunity opp: trigger.new){
       
            if(!ListCities.contains(opp.OriginCIty__c)){
                opp.addError('city is not related to respective state');
            }
        }
    }

 
chanchal_:)chanchal_:)
Or you can use this,
Let me know  if it helps.
trigger OpportunityTrigger on Opportunity (before insert) {
    List<string> ListOliOrigins = new List<string>();
    for(Opportunity opp: trigger.new){
        ListOliOrigins.add(opp.Origin__c);
    }
    List<string> ListCities = new   List<string>();
     for(StateWiseCities__mdt oswc :  [SELECT masterlabel, hub__c  FROM StateWiseCities__mdt 
                                                  WHERE hub__c = :ListOliOrigins]){
        ListCities.add(oswc.MasterLabel);
    }
   
    for(Opportunity opp: trigger.new){
       
            if(!ListCities.contains(opp.OriginCIty__c)){
                opp.addError('city is not related to respective state');
            }
        }
    }


 
This was selected as the best answer
chanchal_:)chanchal_:)
Values of OriginCIty are case sensitive 
R557R557
Thanks a lot!