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
StaciStaci 

Help me simplify this code please?

I have a button that should take most of the fields from the current case, pop up a box asking for new values for 5 fields and then create a new case with the previous case field values and the 5 new values.  It works this way, but this is SO convoluted.  Please help me simplify this.  There has to be a way to pass all the previous cases info without doing it one field at a time, I just have no idea how.  Any help is GRATELY appreciated

Button:
/apex/CW_Recurring_Incident?retURL={!Case.Id}&00N30000006aSCK={!Case.Product__c}&00N30000006aSCE={!Case.Application__c}&00N30000006aSCF={!Case.Component__c}&00N30000006aSCN={!Case.Subsystem__c}&00Na0000009yTvW={!Case.Additional_email_1__c}&00Na0000009yTvX={!Case.Additional_email_2__c}&00Na0000009zDPm={!Case.Impact__c}&00Na0000009zDPp={!Case.Urgency__c}&cas6={!Case.Reason}&cas14={!Case.Subject}&cas3={!Case.Contact}&00Na0000009zDPj={!Case.CW_Type__c}&cas15={!Case.Description}&cas4={!Case.Account}&00N30000007Pq9o={!Case.Machine_ID__c}&00Ne0000000illW={!Case.Machine_Model__c}&00Ne0000000j67X={!Case.Date_Code__c}&00Ne0000000im8Q={!Case.Onboard_Software_Version__c}&00Ne0000000im8V={!Case.Onboard_Hardware__c}&00Ne0000000j67T={!Case.Serial__c}&00Ne0000000j67S={!Case.Part__c}&cas28={!Case.CaseNumber}&00Ne0000000illM={!Case.Workaround_ICA__c}&00Ne0000000ilkx={!Case.Problem_Number__c}&00Na0000009zDPl={!Case.Declined_Reason__c}&00N30000007PqGR={!Case.Problem_Tracking_Link__c}&00Na0000009zDPo={!Case.Root_Cause_Description__c}&00Na0000009zDPi={!Case.CPI_Numbertext__c}&00Ne0000000ill2={!Case.CPI_Tracking_Link__c}&00Na0000009zDPn={!Case.NPI_Numbertext__c}&00Na000000A0KZY={!Case.NPI_Tracking_Link__c}&00Ne0000000ill7={!Case.Change_Number__c}&00Na0000009yPyY={!Case.Change_Tracking_Link__c}&00Ne0000000illb={!Case.Machine_Serial__c}&00Na0000009zDPk={!Case.Caused_by_Change__c}



VF Page:
<apex:page standardController="Case" extensions="CW_recurringIncident">
<apex:form id="frm">
<apex:detail subject="{!Case.Id}" relatedList="false" title="false"/>
<apex:outputPanel id="tstpopup" rendered="{!IF(isDisplayPopUp ==true,true,false)}" >
<apex:outputPanel styleClass="popupBackground" layout="block" />
<apex:outputPanel styleClass="custPopup" layout="block">
<apex:pageMessages >
</apex:pageMessages>
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField label="Incident First Response" value="{!Case.Incident_First_Response__c}"  required="true"/>
<apex:inputField label="Incident Start" value="{!Case.Incident_Start__c}" onclick="" required="true"/>
<apex:inputField label="Service Restored" value="{!Case.Incident_Resolved__c}" onclick="" required="true"/>
<apex:inputField label="Defect Type" value="{!Case.Defect_Type_Multi__c}" required="true"/>
<apex:inputField label="Service Restoration Description" value="{!Case.Service_Restoration__c}" onclick="" required="true"/>

<apex:outputPanel >
<apex:CommandButton action="{!Save}" value="Save"/>
<apex:CommandButton action="{!cancel}" value="Cancel"/>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>





</apex:form>



<style type="text/css"> .errorMsg{ width:159px; }
.custPopup{ background-color: white; border-width: 3px;
border-style: solid;
z-index: 9999;
left: 25%;
padding:10px;
position: absolute;
width: 1000px;
//margin-left: -80px; top:100px; margin-left: -170px;
//top:305px;
border-radius: 5px;
}

.datePicker{z-index:10000}



.popupBackground{ background-color:black; opacity: 0.20; filter: alpha(opacity = 20);
position: absolute; width: 100%; height: 100%; top: 0; left: 0;
z-index: 997 } a.actionlink:hover{ text-decoration:underline; }
.customactionLink { color: #015BA7; font-weight: normal; text-decoration: none; } </style>

<script>
      function setFocusOnLoad() { }
</script>
</apex:page>



Class:
public class CW_recurringIncident
{
public Boolean isDisplayPopUp {get; set;}

public string product,  Serial, Change_Tracking_Link, Change_Number, NPI_Tracking_Link, NPI_Numbertext, CPI_Tracking_Link, CPI_Numbertext, Root_Cause_Description, Problem_Tracking_Link, Problem_Number, Declined_Reason, Workaround_ICA, Parent_Case, Part, Machine_Serial, Date_Code, Onboard_Software_Version, Onboard_Hardware, Application, Account_Name, Machine_Model, Machine_ID, Implementation, Component, Contact, Type, Description, Reason, Subject, Subsystem, Additional_email_1, Additional_email_2, Impact, Urgency;
public CW_recurringIncident(ApexPages.StandardController controller)
{
if(System.currentPageReference().getParameters().get('00N30000006aSCK') <> null ){
product = System.currentPageReference().getParameters().get('00N30000006aSCK');
}
if(System.currentPageReference().getParameters().get('00N30000006aSCE') <> null ){
Application = System.currentPageReference().getParameters().get('00N30000006aSCE');
}
if(System.currentPageReference().getParameters().get('00N30000006aSCF') <> null ){
Component = System.currentPageReference().getParameters().get('00N30000006aSCF');
}
if(System.currentPageReference().getParameters().get('00N30000006aSCN') <> null ){
Subsystem = System.currentPageReference().getParameters().get('00N30000006aSCN');
}
if(System.currentPageReference().getParameters().get('00Na0000009yTvW') <> null ){
Additional_email_1 = System.currentPageReference().getParameters().get('00Na0000009yTvW');
}
if(System.currentPageReference().getParameters().get('00Na0000009yTvX') <> null ){
Additional_email_2 = System.currentPageReference().getParameters().get('00Na0000009yTvX');
}
if(System.currentPageReference().getParameters().get('00Na0000009zDPm') <> null ){
Impact = System.currentPageReference().getParameters().get('00Na0000009zDPm');
}
if(System.currentPageReference().getParameters().get('00Na0000009zDPp') <> null ){
Urgency = System.currentPageReference().getParameters().get('00Na0000009zDPp');
}
if(System.currentPageReference().getParameters().get('cas6') <> null ){
Reason= System.currentPageReference().getParameters().get('cas6');
}
if(System.currentPageReference().getParameters().get('cas14') <> null ){
Subject= System.currentPageReference().getParameters().get('cas14');
}
if(System.currentPageReference().getParameters().get('cas3') <> null ){
Contact= System.currentPageReference().getParameters().get('cas3');
}
if(System.currentPageReference().getParameters().get('00Na0000009zDPj') <> null ){
Type= System.currentPageReference().getParameters().get('00Na0000009zDPj');
}
if(System.currentPageReference().getParameters().get('cas15') <> null ){
Description= System.currentPageReference().getParameters().get('cas15');
}
if(System.currentPageReference().getParameters().get('00N30000006aSCH') <> null){
Implementation= System.currentPageReference().getParameters().get('00N30000006aSCH');
}
if(System.currentPageReference().getParameters().get('00N30000007Pq9o') <> null ){
Machine_ID= System.currentPageReference().getParameters().get('00N30000007Pq9o');
}
if(System.currentPageReference().getParameters().get('00Ne0000000illW') <> null ){
Machine_Model= System.currentPageReference().getParameters().get('00Ne0000000illW');
}
if(System.currentPageReference().getParameters().get('cas4') <> null ){
Account_Name= System.currentPageReference().getParameters().get('cas4');
}
if(System.currentPageReference().getParameters().get('00Ne0000000im8Q') <> null ){
Onboard_Software_Version= System.currentPageReference().getParameters().get('00Ne0000000im8Q');
}
if(System.currentPageReference().getParameters().get('00Ne0000000im8V') <> null ){
Onboard_Hardware= System.currentPageReference().getParameters().get('00Ne0000000im8V');
}
if(System.currentPageReference().getParameters().get('00Ne0000000j67S') <> null ){
Part= System.currentPageReference().getParameters().get('00Ne0000000j67S');
}
if(System.currentPageReference().getParameters().get('00Ne0000000j67T') <> null ){
Machine_Serial= System.currentPageReference().getParameters().get('00Ne0000000j67T');
}
if(System.currentPageReference().getParameters().get('00Ne0000000j67X') <> null ){
Date_Code= System.currentPageReference().getParameters().get('00Ne0000000j67X');
}

if(System.currentPageReference().getParameters().get('cas28') <> null ){
Parent_Case= System.currentPageReference().getParameters().get('cas28');
}

if(System.currentPageReference().getParameters().get('00Ne0000000illM') <> null ){
Workaround_ICA= System.currentPageReference().getParameters().get('00Ne0000000illM');
}

if(System.currentPageReference().getParameters().get('00Na0000009zDPl') <> null ){
Declined_Reason= System.currentPageReference().getParameters().get('00Na0000009zDPl');
}

if(System.currentPageReference().getParameters().get('00Ne0000000ilkx') <> null ){
Problem_Number= System.currentPageReference().getParameters().get('00Ne0000000ilkx');
}

if(System.currentPageReference().getParameters().get('00N30000007PqGR') <> null ){
Problem_Tracking_Link= System.currentPageReference().getParameters().get('00N30000007PqGR');
}

if(System.currentPageReference().getParameters().get('00Na0000009zDPo') <> null ){
Root_Cause_Description= System.currentPageReference().getParameters().get('00Na0000009zDPo');
}

if(System.currentPageReference().getParameters().get('00Na0000009zDPi') <> null ){
CPI_Numbertext= System.currentPageReference().getParameters().get('00Na0000009zDPi');
}

if(System.currentPageReference().getParameters().get('00Ne0000000ill2') <> null ){
CPI_Tracking_Link= System.currentPageReference().getParameters().get('00Ne0000000ill2');
}

if(System.currentPageReference().getParameters().get('00Na0000009zDPn') <> null ){
NPI_Numbertext= System.currentPageReference().getParameters().get('00Na0000009zDPn');
}

if(System.currentPageReference().getParameters().get('00Na000000A0KZY') <> null ){
NPI_Tracking_Link= System.currentPageReference().getParameters().get('00Na000000A0KZY');
}

if(System.currentPageReference().getParameters().get('00Ne0000000ill7') <> null ){
Change_Number= System.currentPageReference().getParameters().get('00Ne0000000ill7');
}

if(System.currentPageReference().getParameters().get('00Na0000009yPyY') <> null ){
Change_Tracking_Link= System.currentPageReference().getParameters().get('00Na0000009yPyY');
}

if(System.currentPageReference().getParameters().get('00Ne0000000j67T') <> null ){
Serial= System.currentPageReference().getParameters().get('00Ne0000000j67T');
}



isDisplayPopUp = true;
{Case c = (Case) controller.getRecord(); c.Serial__c=Serial; c.Change_Tracking_Link__c=Change_Tracking_Link; c.Change_Number__c=Change_Number; c.NPI_Tracking_Link__c=NPI_Tracking_Link; c.NPI_Numbertext__c=NPI_Numbertext; c.CPI_Tracking_Link__c=CPI_Tracking_Link; c.CPI_Numbertext__c=CPI_Numbertext; c.Root_Cause_Description__c=Root_Cause_Description; c.Problem_Tracking_Link__c=Problem_Tracking_Link; c.Problem_Number__c=Problem_Number; c.Declined_Reason__c=Declined_Reason; c.Workaround_ICA__c=Workaround_ICA; c.Onboard_Software_Version__c=Onboard_Software_Version; c.Onboard_Hardware__c=Onboard_Hardware; c.Part__c=Part; c.Machine_Serial__c=Machine_Serial; c.Date_Code__c=Date_Code; c.Machine_Model__c=Machine_Model; c.Machine_ID__c=Machine_ID; c.Description = Description; c.CW_Type__c = Type; c.Subject= Subject; c.Reason=Reason; c.Urgency__c=Urgency; c.Impact__c=Impact; c.Additional_email_2__c=Additional_email_2; c.Additional_email_1__c=Additional_email_1; c.Subsystem__c=Subsystem; c.Component__c=Component; c.Application__c=Application; c.Status = 'New'; c.RecordTypeId = '012e00000004RxSAAU';c.Product__c=product;}

}}


SeAlVaSeAlVa
Can't you just pass the ID and retrieve the values taking advantage of the standardController or through a SOQL query?

Regards
StaciStaci
Ok I've taken a stab at this.  Right now, the pop-up shows and asks for the 5 fields and a new case is created, but unfortunately nothing gets filled in.  Can you tell me what I'm missing please?

Button:
/apex/CW_Recurring_Incident?caseID={!Case.Id}&saveURL=caseID

Class:
public class CW_recurringIncident
{
public Boolean isDisplayPopUp {get; set;}
private final Case c;


    public CW_recurringIncident(ApexPages.StandardController controller) {
        this.c=(Case)Controller.getrecord();
        isDisplayPopUp = true;
    }
   
     public pageReference Save(){
          ID caseID =  ApexPages.CurrentPage().getParameters().get('caseID');
      
        //create a new Case
        Case newRI = new Case();
       
         // Fill in the values for the new record
         newRI.Status = 'New';
         newRI.RecordTypeId = '012e00000004RxS';
               
         //Insert the new Case record
          Database.insert(newRI);  
       
        PageReference retPage = new PageReference('/' + caseID);
        return retPage;
       }
    }

Page:
<apex:page standardController="Case" extensions="CW_recurringIncident" >
<apex:form id="frm">
<apex:detail subject="{!Case.Id}" relatedList="false" title="false"/>
<apex:outputPanel id="tstpopup" rendered="{!IF(isDisplayPopUp ==true,true,false)}" >
<apex:outputPanel styleClass="popupBackground" layout="block" />
<apex:outputPanel styleClass="custPopup" layout="block">
<apex:pageMessages >
</apex:pageMessages>
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField label="Incident First Response" value="{!Case.Incident_First_Response__c}"  required="true"/>
<apex:inputField label="Incident Start" value="{!Case.Incident_Start__c}" onclick="" required="true"/>
<apex:inputField label="Service Restored" value="{!Case.Incident_Resolved__c}" onclick="" required="true"/>
<apex:inputField label="Defect Type" value="{!Case.Defect_Type_Multi__c}" required="true"/>
<apex:inputField label="Service Restoration Description" value="{!Case.Service_Restoration__c}" onclick="" required="true"/>

<apex:outputPanel >
<apex:CommandButton action="{!Save}" value="Save"/>
<apex:CommandButton action="{!cancel}" value="Cancel"/>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>





</apex:form>



<style type="text/css"> .errorMsg{ width:159px; }
.custPopup{ background-color: white; border-width: 3px;
border-style: solid;
z-index: 9999;
left: 25%;
padding:10px;
position: absolute;
width: 1000px;
//margin-left: -80px; top:100px; margin-left: -170px;
//top:305px;
border-radius: 5px;
}

.datePicker{z-index:10000}



.popupBackground{ background-color:black; opacity: 0.20; filter: alpha(opacity = 20);
position: absolute; width: 100%; height: 100%; top: 0; left: 0;
z-index: 997 } a.actionlink:hover{ text-decoration:underline; }
.customactionLink { color: #015BA7; font-weight: normal; text-decoration: none; } </style>

<script>
      function setFocusOnLoad() { }
</script>
</apex:page>
StaciStaci
ok I think I'm getting closer, but its telling me I have 0 rows.  Can you please tell me what I'm missing?

public class CW_recurringIncident
{
public Boolean isDisplayPopUp {get; set;}
public final Case caseID;
public Case newRI{get;set;}

    public CW_recurringIncident(ApexPages.StandardController controller) {
    caseID = [SELECT product__c, Serial__c, Change_Tracking_Link__c, Change_Number__c, NPI_Tracking_Link__c, NPI_Numbertext__c, CPI_Tracking_Link__c, CPI_Numbertext__c, Root_Cause_Description__c, Problem_Tracking_Link__c, Problem_Number__c, Declined_Reason__c, Workaround_ICA__c, ParentId, Part__c, Machine_Serial__c, Date_Code__c, Onboard_Software_Version__c, Onboard_Hardware__c, Application__c, Account_Name__c, Machine_Model__c, Machine_ID__c, Component__c, CW_Type__c, Description, Reason, Subject, Subsystem__c, Additional_email_1__c, Additional_email_2__c, Impact__c, Urgency__c FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        isDisplayPopUp = true;
     }
     public pageReference Save(){
         //create a new Case
         Case newRI = new Case();
         // Fill in the values for the new record
         newRI.Status = 'New';
         newRI.RecordTypeId = '012e00000004RxS';
         newRI.Product__c = caseID.Product__c;
         newRI.Serial__c = caseID.Serial__c;
         newRI.Change_Tracking_Link__c = caseID.Change_Tracking_Link__c;
         newRI.Change_Number__c = caseID.Change_Number__c;
         newRI.NPI_Tracking_Link__c = caseID.NPI_Tracking_Link__c;
         newRI.NPI_Numbertext__c = caseID.NPI_Numbertext__c;
         newRI.CPI_Tracking_Link__c = caseID.CPI_Tracking_Link__c;
         newRI.CPI_Numbertext__c = caseID.CPI_Numbertext__c;
         newRI.Root_Cause_Description__c = caseID.Root_Cause_Description__c;
         newRI.Problem_Tracking_Link__c = caseID.Problem_Tracking_Link__c;
         newRI.Problem_Number__c = caseID.Problem_Number__c;
         newRI.Declined_Reason__c = caseID.Declined_Reason__c;
         newRI.Workaround_ICA__c = caseID.Workaround_ICA__c;
         newRI.ParentId = caseID.ParentId;
         newRI.Part__c = caseID.Part__c;
         newRI.Machine_Serial__c = caseID.Machine_Serial__c;
         newRI.Date_Code__c = caseID.Date_Code__c;
         newRI.Onboard_Software_Version__c = caseID.Onboard_Software_Version__c;
         newRI.Onboard_Hardware__c = caseID.Onboard_Hardware__c;
         newRI.Application__c = caseID.Application__c;
         newRI.Account_Name__c = caseID.Account_Name__c;
         newRI.Machine_Model__c = caseID.Machine_Model__c;
         newRI.Machine_ID__c = caseID.Machine_ID__c;
         newRI.Component__c = caseID.Component__c;
         newRI.CW_Type__c = caseID.CW_Type__c;
         newRI.Description = caseID.Description;
         newRI.Reason = caseID.Reason;
         newRI.Subject = caseID.Subject;
         newRI.Subsystem__c = caseID.Subsystem__c;
         newRI.Additional_email_1__c = caseID.Additional_email_1__c;
         newRI.Additional_email_2__c = caseID.Additional_email_2__c;
         newRI.Impact__c = caseID.Impact__c;
         newRI.Urgency__c = caseID.Urgency__c;
        
       //Insert the new Case record
        insert newRI;  
        PageReference retPage = new PageReference('/' + newRI);
        return retPage;
       }


    }
SeAlVaSeAlVa
Try something like ...

public class CW_recurringIncident
{
public Boolean isDisplayPopUp {get; set;}
public Case caseID;
public Case newRI{get;set;}

    public CW_recurringIncident(ApexPages.StandardController controller) {
    caseID = [SELECT product__c, Serial__c, Change_Tracking_Link__c, Change_Number__c, NPI_Tracking_Link__c, NPI_Numbertext__c, CPI_Tracking_Link__c, CPI_Numbertext__c, Root_Cause_Description__c, Problem_Tracking_Link__c, Problem_Number__c, Declined_Reason__c, Workaround_ICA__c, ParentId, Part__c, Machine_Serial__c, Date_Code__c, Onboard_Software_Version__c, Onboard_Hardware__c, Application__c, Account_Name__c, Machine_Model__c, Machine_ID__c, Component__c, CW_Type__c, Description, Reason, Subject, Subsystem__c, Additional_email_1__c, Additional_email_2__c, Impact__c, Urgency__c FROM Case WHERE Id = :controller.getRecord().ID];
        isDisplayPopUp = true;
     }
     public pageReference Save(){
         //create a new Case
         Case newRI = caseID.clone();

         // Fill in the values that should not be taken from the old Case
         newRI.Status = 'New';
         newRI.RecordTypeId = '012e00000004RxS';
  
       //Insert the new Case record
        insert newRI;  
        PageReference retPage = new PageReference('/' + newRI);
        return retPage;
       }
    }

In any case, take care of the assignment of caseID = [], if it returns no rows, it will throw an exception "List has no rows for assignment".

I'll suggest to change it to a List<Case> and then ask for the size of that list.

Kind regards.