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
kishore 12kishore 12 

How can i write a trigger using custom setting in Account object when i enter Billing Zip/Postal Code that related fileds information filled automatically ?

Best Answer chosen by kishore 12
RKSalesforceRKSalesforce
Hi Kishore,

Create Custom Setting: Lets say Name  of Custom Setting is AddressDetails__c and Fields are Zip/Postal Code, Billing State / Province, Billing City, Billing State. Insert your records into Custom Setting either by going to Custom Setting then hit Manage and then enter records manually OR Data Loader.
Please use below code to access those Custom Setting Records:
Map<String, AddressDetails__c> zipAndRecordMap = New Map<String, AddressDetails__c>();
List<AddressDetails__c> ZipList = AddressDetails__c.getall().values();
for(AddressDetails__ckwCS: ZipList ){
            keywordMap.put(kwCS.name, kwobj);
}

Write your trigger in before insert. While assigning values  to the fields do like below:
for(Account acc: Trigger.New){
       acc.Afield = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).AField;
       acc.BillingState = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).BillingStateField;
       acc.BillingCity  = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).BillingCityField;
}
Please mark as best answer if helped.

Regards,
Ramakant
 

All Answers

RKSalesforceRKSalesforce
Hi Kishore,

More Information would help in solving your question. :Please post your exact requirement.

Regards,
Ramakant
kishore 12kishore 12
In Account object having field Billing Zip/Postal Code  entered that related fields (Billing State/Province , Billing City , Billing Street ) values populated Automatically using custom settingd
RKSalesforceRKSalesforce
Hi Kishore,

Create Custom Setting: Lets say Name  of Custom Setting is AddressDetails__c and Fields are Zip/Postal Code, Billing State / Province, Billing City, Billing State. Insert your records into Custom Setting either by going to Custom Setting then hit Manage and then enter records manually OR Data Loader.
Please use below code to access those Custom Setting Records:
Map<String, AddressDetails__c> zipAndRecordMap = New Map<String, AddressDetails__c>();
List<AddressDetails__c> ZipList = AddressDetails__c.getall().values();
for(AddressDetails__ckwCS: ZipList ){
            keywordMap.put(kwCS.name, kwobj);
}

Write your trigger in before insert. While assigning values  to the fields do like below:
for(Account acc: Trigger.New){
       acc.Afield = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).AField;
       acc.BillingState = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).BillingStateField;
       acc.BillingCity  = zipAndRecordMap .get(acc.ZipCodeFieldOnAccount).BillingCityField;
}
Please mark as best answer if helped.

Regards,
Ramakant
 
This was selected as the best answer
Maharajan CMaharajan C
Hi Kishore,


trigger GetAccountAdderss on Account (before insert,before update) {
Map<String,Address__c> AdMap = new Map<String,Address__c>();
// Get the address detail from Custom Setting
//Create the custom setting and name it as Address and create the fields for Street,State, Country in Address Custom setting.
//Create the records in Custom setting
List<Address__c> CustomSetting = Address__c.getall().values();
//Loop the CustomSetting and put the details in map
for(Address__c add : CustomSetting)
{
If(!AdMap.ContainsKey(add.Name))
{
//In custom setting store the Zip as Recore Name 
  AdMap.put(add.Name,add);
}
}

for(Account acc:trigger.new)
{
If(acc.BillingPostalCode != '' && AdMap.ContainsKey(acc.BillingPostalCode))
{
acc.BillingStreet = AdMap.get(acc.BillingPostalCode).Street__c;
acc.BillingCountry = AdMap.get(acc.BillingPostalCode).Country__c;
acc.BillingState = AdMap.get(acc.BillingPostalCode).State__c;
}
}
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj