0

I have a situation, where in a record if Name__c field is updated, delete the record, and insert a new one with Status__c = In Progress. Also when inserting the new record, all the values of the other fields from the old record should be retained in the new record! I have to handle this in the controller, and not in the trigger!

My questions: 1. How do I check if the value of Name__c changed? 2. How do I retain values from the old record in the new one?

Please help!

Thanks.

2
  • Any reason why do you not want to use a trigger in this case ? Before Update trigger is better suited for your scenario.
    – Dave
    May 29, 2015 at 1:11
  • Is this a single record detail page controller or is this a list page controller? It sounds like you're going to need to store the initial state of the Name__c variable every time it's loaded or reloaded following a Save action for comparison before writing another update/insert/save.
    – crmprogdev
    May 29, 2015 at 1:28

2 Answers 2

0

If I understood the problem correctly, you do not want to use triggers and want it to be done in controller class only.
Here is a way to do it without using triggers.

public with sharing myController {
      SObject[] oldRecords; // for initial/old data
      ApexPages.StandardController con;

      public SObject[] myRecords;  // for updated data

      public myController(Apexpages.standardcontroller controller) {
        // Make a copy of Original data
        oldRecords = myRecords.deepClone(true,true,true); 
      }


    // This method will be called from VF Page
      public ApexPages.StandardController updateMethod() {
        update myRecords;
        for(Integer i = 0;i < myRecords.size();i++)  
         if(myRecords[i].myField__c != oldRecords[i].myField__c) // Do a comparison of individual records for any required field and take action as needed
           { // take action }

      }
0
0

For the first Question "How do I check if the value of the Name__c changed" in Class and not in trigger. Here's a sample code.

Here's the code which goes into the trigger based on the event (after)

if (Trigger.isAfter){
APEXCLASSNAME.updateSomethingWhenNameChange(trigger.Old,trigger.newMap);
}

Here's the apex class which should go into the method "updateSomethingWhenNameChange"

Set<Id> ids = new Set<Id>();
      for(Object__c obj : triggerNew.values()) {
        Object__c oldObj = triggerOldMap.get(obj.Id);
        if(obj.Name__c != oldObj.Name__c) {
          ids.add(obj.Id);

For the second half of the question "How do I retain values from the old record in the new one?"

you need to do something similar as above by using the following function and put them in a variable so that you can easily recognize

string oldname = triggerOldMap.get(obj.Name__c)
string oldcity = triggerOldMap.get(obj.City__c)

Hope this helps.

5
  • How will the triggerNew be available in apex controller, if triggers are not used?
    – Dave
    May 29, 2015 at 1:32
  • Hi Dave, Just added trigger details too for your ease of understanding.
    – DJD
    May 29, 2015 at 1:32
  • Found the updated answer okie?
    – DJD
    May 29, 2015 at 1:33
  • Giene, I assume problem statement states they do not want to use triggers and this only needs to be managed in apex class ( without any invocation of controller method from trigger ). If we are using triggers, it can very well be handled in it.
    – Dave
    May 29, 2015 at 1:35
  • Hi Dave, you will have to go from trigger and I dont believe there is no another way. Also the above code is more optimized way.
    – DJD
    May 29, 2015 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .