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
adi salesforceadi salesforce 

Trigger to update account information section in new record with old record values

I have a record in account with billing city pune. Now I want to create a record with billing city pune but account information section values should be updated with previous record values whose billing city is pune. how I can achieve it
 
Sonali_takkeSonali_takke
Hi Adi,

As per your requirement ,You have one record with billing city pune.
You are creating new record is it also an account?
If so then when you crete one more record there will be duplicate records. So their must be some unique criteria 
other than billingcity pune to decide from which old record to update  account information section values .


If you are sure only one record  will return then plese update below sample code as per your logic
 
trigger AccTrigger on Account(Before Insert){
    List<Account> accList = new List<Account>([SELECT id,billingcity,billingstate  from account where billingcity ='pune' limit 1]);    //Query fields which you need for assignment

    for(Account accObj:Trigger.new){

        if(accObj.billingcity =='Pune'){
            accObj.billingstate = accList[0].billingstate;
            //ASSIGN VALUES IN OLD ACCOUNT TO NEW ACCOUNT
        }
    }
}


Thank You
adi salesforceadi salesforce
Hi sonali,
Thank you for the solution but actual requirement is I've two record types in contact one is new contact and another is renewal contact. I've created a contact whose record type is new contact and again I've created a record with record type renewal contact. the common field linking these two records is account. If the account is same for two records then I want to get same values of certain section in  record with recordtype in record which of record type renewal same as values of record type with new contact. please write the trigger helper and handler I need the solution quickly it is urgent.