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
Prema -Prema - 

Hello Friends please help me how can i get my country value from custom settings inside Leads field called Territory.

trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
   if(Trigger.isInsert||Trigger.isUpdate)
   {
     
    for(Lead leadc:Trigger.new)
    {
        
        if(leadc.States__c!=null)
        {
        
           
           for (Countries_List__c cl: Countries_List__c.getAll().values()) 
           {

               Countries_List__c.add(cl.Country__c);

           }
    }   }
    
      for(Lead AllLeads:trigger.new)
      { 

           if(Countries_List__c.contains(AllLeads.States__c))
           {

         //AllLeads.adderror('Oops!!! All routing numbers must go to real banks');
           
             AllLeads.Territory__c=Countries_List__c.Country__c.;    

           }
        }    
             
 }
         
}

My requirement is whenever a field on Lead called State(Picklist field) has any value, if it gets saved then a Territory field should be auto populated(Value should be saved automatically for example State=Gujrat then Territory=India). I have a custom settings called Country_List and there is a field called Country__c. Inside Country I have defined States and it's country. How can i achieve this, Please help me.Thanks in advance
Best Answer chosen by Prema -
Raj VakatiRaj Vakati
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
	
	Map<String, Countries_List__c> mcs = Countries_List__c.getAll();

	
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States__c)!=null)
           {

         //AllLeads.adderror('Oops!!! All routing numbers must go to real banks');
           
             AllLeads.Territory__c=mcs.get(AllLeads.States__c).Country__c.;    

           }
        }    
             
 }
         
}

 

All Answers

Prema -Prema -
User-added image
User-added imageUser-added image
Raj VakatiRaj Vakati
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
	
	Map<String, Countries_List__c> mcs = Countries_List__c.getAll();

	
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States__c)!=null)
           {

         //AllLeads.adderror('Oops!!! All routing numbers must go to real banks');
           
             AllLeads.Territory__c=mcs.get(AllLeads.States__c).Country__c.;    

           }
        }    
             
 }
         
}

 
This was selected as the best answer
Prema -Prema -
Hello Raj, Thanks for your help but i am getting error. Please check.
User-added image
Raj VakatiRaj Vakati
This is the correct code
 
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
	
	Map<String, Countries_List__c> mcs = Countries_List__c.getAll();

	
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States__c)!=null)
           {

         //AllLeads.adderror('Oops!!! All routing numbers must go to real banks');
           
             AllLeads.Territory__c=mcs.get(AllLeads.States__c).Country__c;    

           }
        }    
             
 }
         
}

 
Prema -Prema -
I tried to make it somehow but it's only getting executed in specific value as i have hardcode as Gujrat but how to run this dynamically?
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
    
    Map<String, Countries_List__c> mcs =new map<String,Countries_List__c>();
        
        mcs= Countries_List__c.getAll();
    
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States__c)!=null)
           {
               AllLeads.Territory__c=(mcs.get('Gujrat').Country__c);
               
           }
        }    
             
 }
         
}

 
Raj VakatiRaj Vakati
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
    
    Map<String, Countries_List__c> mcs =new map<String,Countries_List__c>();
        
        mcs= Countries_List__c.getAll();
    
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States__c)!=null)
           {
               AllLeads.Territory__c=(mcs.get(AllLeads.States__c).Country__c);
               
           }
        }    
             
 }
         
}

 
Prema -Prema -
Thank you so much it's working now but could you please help me with the standard field STate which is already in Address field, How can i get value in that field?
Raj VakatiRaj Vakati

Replace State__c filed with States which is standard filed  . Complete code is here
 
trigger GetTerritoryIfStateSaved on Lead (before insert,before update) 
{
    List<Lead> lead1=new List<Lead>();
    public string stateget;
    
    Map<String, Countries_List__c> mcs =new map<String,Countries_List__c>();
        
        mcs= Countries_List__c.getAll();
    
   if(Trigger.isInsert||Trigger.isUpdate)
   {
   
      for(Lead AllLeads:trigger.new)
      { 

           if(mcs.get(AllLeads.States)!=null)
           {
               AllLeads.Territory__c=(mcs.get(AllLeads.States).Country__c);
               
           }
        }    
             
 }
         
}


 
Prema -Prema -
Thank you so much , YOu havebeen very helpful. :)
Raj VakatiRaj Vakati
Cool  .. Mark is solved!