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
Anirban Saha 8Anirban Saha 8 

Updating an existing record while create a new record in same object

Hi
I am new in apex coading and currently looking for a help on a particular scenario. I am trying to update an existing record when I am creating a record on that same object.
Example: I have a custom object associated with an account and only one active record can be present in that custom object. Let say there is an active record present in that object already and now I am creating another record in that particular object. On the creation of the new record will automatically deactivate the previous record.

Thanks in advance
ANUTEJANUTEJ (Salesforce Developers) 
Hi Anirban,

I believe the scenario you have is that you have a checkbox which you want to deactivate on the records that are previously existing system and activate on the current record.

There could be multiple ways to check the above scenario one way is you could wirite a trigger on before inserting the record check if there are any records with the checkbox to true if so uncheck the checkbox and update them and insert the new record with checkbox set to true.

And please do remember to follow the best practices while writing the trigger:
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm
>> https://developer.salesforce.com/page/Apex_Code_Best_Practices

Please let me know if this was helpful and I would request you to close the thread by marking this as best answer as it would help others in the future and also keeps the community clean,

Regards,
Anutej Poddaturi
Anirban Saha 8Anirban Saha 8
Hi Anutez,

Thanks for providng the way. I tried to pull the record but now facing the challange to update the existing record and keeping the new record as active. Can you please help. I created the below code to pull the existing account information,

trigger Deactivate_EP on Account_Plan__c(before insert) {

    Set<Id> AccountID = new Set<Id>();
    for(Account_Plan__cAP: Trigger.new)
    {
        AccountID.add(AP.Account_vod__c);
    }
     
   List<Account_Plan_vod__c> APLIST = new List<Account_Plan_vod__c>([Select ID, Active__c,Account__c,Recordtype.Name from Account_Plan__c where Active__c = TRUE and Account__c IN:AccountID]);
}

After this how can I update this record present in the list.

Thanks in advance,
Anirban
ANUTEJANUTEJ (Salesforce Developers) 
Hi Anirban,

One of the possibility is you can run a for each loop to update all the account fields to false add them to a list and update the existing records.

This above way is one of the way that I could think of and you can implement it according to your use case.

Please let me know if this helps and in case if it does as mentioned earlier please mark a best answer and close the thread to keep the community clean.

Do keep in mond to follow all the best practices so that your limits are not hit and to ensure it works properly even in the future with the development.

Regards,
Anutej Poddaturi