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
sushmaiyer2@gmail.comsushmaiyer2@gmail.com 

Need a Trigger for Update of populating value in a Standard field.

Hi All,

 

I have two objects,Accounts and Territory Object :-

 

Account has following Standard fields:-

 

Billing State/Province

Billing Zip/Postal Code

Billing Country

 

Territory Object has Standard field :-

 

Territory Code

 

Territory Object has Standard field

 

Territory Name

 

 

Accounts Object is Standard Object and have created Territory Object to store Territory Code and 
Territory Name.

 

So my requirement is when I enter some Code in Billing Zip/Postal Code and Country in Billing Country,it should check for whether it is India and then check for whether the code exists in Territory Object and save the corresponding Territory Name in Billing State/Province.

 

 

Please kindly help and Guide...

 

Best Answer chosen by Admin (Salesforce Developers) 
sushmaiyer2@gmail.comsushmaiyer2@gmail.com

Working Trigger :-

 

trigger updateBillingStateProvince on Account (before insert, before update)
{
       
    Map<String, String> territoryMap = new Map<String, String> { 'AB' => 'Scotland', 'AL' => 'East of England'.toUpperCase() };
    
    Set<String> accountPostalCodeSet = new Set<String>();
 
    for(Account acc : trigger.new)
        if((!String.IsBlank(acc.BillingPostalCode))&& (acc.BillingCountry=='UK'))
            accountPostalCodeSet.add(acc.BillingPostalCode);
 
    if(accountPostalCodeSet.size()>0){
        for(Territory__c territory : [SELECT Territory_Name__c, Name FROM Territory__c WHERE Name IN:accountPostalCodeSet])
            if(!territoryMap.containsKey(territory.Name))
                territoryMap.put(territory.Name, territory.Territory_Name__c);
 
        for(Account acc : trigger.new)
            if(
                territoryMap.containsKey(acc.BillingPostalCode) &&
                String.IsBlank(acc.BillingState)
            )
                acc.BillingState = territoryMap.get(acc.BillingPostalCode);
            else if ((territoryMap.containsKey(acc.BillingPostalCode)))
                acc.BillingState = territoryMap.get(acc.BillingPostalCode);
    }
}

 

Got the Solution from Customer Community from Deepak K Anand...

All Answers

Avidev9Avidev9
sushma what you have tried till now ?
I hope you tried to write something on your own.
sushmaiyer2@gmail.comsushmaiyer2@gmail.com

trigger mappostcodetoterritoryname on Account (after insert,after update)
{

SObject s1 = [SELECT BillingPostalCode FROM Account LIMIT 1];

 

Object o1 = s1.get('BillingPostalCode');

 

SObject s2 = [SELECT Territory_Name__c
FROM Territory__c
WHERE BillingPostalCode = Name
LIMIT 1];

 

Object o2 = s2.get('Territory_Name__c');

 

s2.put('BillingState');

 

}

 

bujjibujji
Hi,

Try this code.

trigger C_Field_Update on Account(after insert, after update)

{
for(Account c:Trigger.new)

{
//*********Here Check the whether the territory name is exist or not
Territory__c tfield = [select Id, Territory_Name__c From Territory__c where c.BillingPostalCode = Name Limit 1];

//Update the field here
c.BillingState = tfield.Territory_Name__c;

}
}

Thanks,
Bujji
Bhawani SharmaBhawani Sharma
You should have a before update trigger.
This trigger will check using SOQL if there is already a territory record with specific conditions, update account appropriately.
make your trigger bulkify
sushant sussushant sus

try this it will work

trigger C_Field_Update on Account(before insert, before update)

{
map<string,string>maping=new map<string,string>();

list<string>ss=newlist<string>()://please check data type
for(account acc: trigger.new)
{

ss.add(acc.billingcode);

}
list<Territory__c> tfield = [select Id, Territory_Name__c,Territory_code__c From Territory__c where Territory_code__c in:ss];
for(Territory__c:tfield)
{
maping.put(Territory_code__c,Territory_Name__c);
}
for(account ac: trigger.new)
{

//here check it is null or not maping.get(ac.BillingPostalCode);
ac.billing state=maping.get(ac.BillingPostalCode);

}

sushmaiyer2@gmail.comsushmaiyer2@gmail.com

Working Trigger :-

 

trigger updateBillingStateProvince on Account (before insert, before update)
{
       
    Map<String, String> territoryMap = new Map<String, String> { 'AB' => 'Scotland', 'AL' => 'East of England'.toUpperCase() };
    
    Set<String> accountPostalCodeSet = new Set<String>();
 
    for(Account acc : trigger.new)
        if((!String.IsBlank(acc.BillingPostalCode))&& (acc.BillingCountry=='UK'))
            accountPostalCodeSet.add(acc.BillingPostalCode);
 
    if(accountPostalCodeSet.size()>0){
        for(Territory__c territory : [SELECT Territory_Name__c, Name FROM Territory__c WHERE Name IN:accountPostalCodeSet])
            if(!territoryMap.containsKey(territory.Name))
                territoryMap.put(territory.Name, territory.Territory_Name__c);
 
        for(Account acc : trigger.new)
            if(
                territoryMap.containsKey(acc.BillingPostalCode) &&
                String.IsBlank(acc.BillingState)
            )
                acc.BillingState = territoryMap.get(acc.BillingPostalCode);
            else if ((territoryMap.containsKey(acc.BillingPostalCode)))
                acc.BillingState = territoryMap.get(acc.BillingPostalCode);
    }
}

 

Got the Solution from Customer Community from Deepak K Anand...

This was selected as the best answer