+ Start a Discussion
StaciStaci 

button to create a new alert, with previous alert info, update one field

I have a button on a custom object called Alerts.  When the button is clicked we basically want it to clone all information, but I want a Master Alert field filled in with the original alert's ID:

So for instance
An Initial Alert is created - Alert 110
I click the Update button on Alert 110 and it clones it (creating Alert 111 and opening an edit of the new alert) I can edit all I want and on save I want Alert 110 to be in the Master Alert field.  
So Now if I click Update on Alert 111 (creating Alert 112 and opening an edit of the new alert) I can edit all I want and on Save I want Alert 110 to be in the Master Alert field.

Here is my mess of code Class:
public class CW_alertUpdate{

public final Alerts__c alertID;
public Alerts__c newAlert{get;set;}
  
    public CW_alertUpdate(ApexPages.StandardController controller) {
        //gets fields from previous case
        alertID = [SELECT Id FROM Alerts__c WHERE Id = :ApexPages.currentPage().getParameters().get('alertID')];
        //isDisplayPopUp = true;
        //gets fields from pop-up
        //this.formFields = (Case)Controller.getRecord();
     }
     public pageReference autorun(){
         //create a new Case
         Alerts__c newAlert = new Alerts__c();
         
          // Fill in the values for the new record from previous case
       
         newAlert.Master_Alert_Lookup__c = alertID.Id;
        
        //Insert the new Case record
        insert newAlert;
        system.debug(newAlert);
        PageReference retPage = new PageReference('/' + newAlert.id);
        return retPage;
       }
    }

Functionally it works, BUT it creates one Alert with all the previous alert's info and then creates a 2nd Alert with just the updated Master Alert field.  How do I make it work and just create 1 Alert?



Update button:
/a0H/e?retURL={!Alerts__c.Id}&00Ne0000000jigO={!Alerts__c.Incident_Start__c}&00Ne0000000jifu={!Alerts__c.Product__c}&00Ne0000000jigd={!Alerts__c.Team_List__c}&CF00Ne0000000jigi={!Alerts__c.Incident_Manager__c}&CF00Ne0000000jigi_lkid={!Alerts__c.Incident_ManagerId__c}&00Ne0000000jigT={!Alerts__c.Incident_Reported__c}&00Ne0000000jifk={!Alerts__c.Subject__c}&CF00Ne0000000jigs={!Alerts__c.Account_Name__c}&CF00Ne0000000jigs_lkid={!Alerts__c.Account_NameId__c}&
00Ne0000000jifz={!Alerts__c.Customer_Impact__c}&00Ne0000000jig4={!Alerts__c.Completed_Actions__c}&00Ne0000000jig9={!Alerts__c.Next_Actions__c}&CF00Ne0000000jigE={!Alerts__c.Incident_Ticket_Number__c}&CF00Ne0000000jigE_lkid={!Alerts__c.Incident_Ticket_NumberId__c}&00Ne0000000dZZQ={!Alerts__c.CGM_P1_AMERICAS_MANNED__c}&00Ne0000000dZZL={!Alerts__c.CGM_P1_APD_MANNED__c}&00Ne0000000dZZH={!Alerts__c.CGM_P1_Command__c}&00Ne0000000dZZG={!Alerts__c.CGM_P1_EAME_MANNED__c}&saveURL=/apex/CW_AlertUpdate?alertID={!Alerts__c.Id}
Best Answer chosen by Staci
Swati GSwati G
Hi,

You can get the new record id with newid param and use that to use the new created instance and update that instance. Change you method code to as shown below.

public pageReference autorun(){
         //create a new Case
         Alerts__c newAlert = new Alerts__c(Id = ApexPages.currentPage().getParameters().get('newid'));
        
          // Fill in the values for the new record from previous case
      
         newAlert.Master_Alert_Lookup__c = alertID.Id;
       
        //Insert the new Case record
        update newAlert;
        system.debug(newAlert);
        PageReference retPage = new PageReference('/' + newAlert.id);
        return retPage;
       }


All Answers

Swati GSwati G
Hi,

You can get the new record id with newid param and use that to use the new created instance and update that instance. Change you method code to as shown below.

public pageReference autorun(){
         //create a new Case
         Alerts__c newAlert = new Alerts__c(Id = ApexPages.currentPage().getParameters().get('newid'));
        
          // Fill in the values for the new record from previous case
      
         newAlert.Master_Alert_Lookup__c = alertID.Id;
       
        //Insert the new Case record
        update newAlert;
        system.debug(newAlert);
        PageReference retPage = new PageReference('/' + newAlert.id);
        return retPage;
       }


This was selected as the best answer
StaciStaci
Thank you Swati!