+ Start a Discussion
Mark VermaatMark Vermaat 

How To: Create an input field in a visual force page to change the Case Owner for a cloned case

I have created a clone case option which clones everything in the case, however my Team is asking for a field in the visual force page to set the case owner so that they dont have to open the cloned case, they can just assign it to the person or queue.... Here is what I have so far:

 

<apex:page standardController="Case"
extensions="CaseClone"
action="{!testthis}">
<apex:pageBlock title="Hello {!$User.FirstName}!"><br />


Your support case has been cloned!<br /><br />
<i><b>Warning! Do not hit "refresh" on your browser or your case will be cloned AGAIN</b></i><br />
<br /><br />
<table cellpadding="10">
<tr>
<td>Original Case Number:</td>
<td>{!CaseNumberOld}</td>
<td><a href="/{!CaseIdOld}">Go to this case...</a></td></tr>
<tr>
<td>New Case Number:</td>
<td>{!CaseNumberNew}</td>
<td> <a href="/{!CaseIdNew}">Go to this case...</a>:</td>
</tr>
</table>
<br/><br />
</apex:pageBlock>
</apex:page>

 

The issue is that the action taken is what clones the case, and if I were to setup something to input the case owner and set it, it would have to go before the action wouldnt it?  I was thinking it could just modify the record after it is created but to do that from VF I dont know.  

 

Any help would be appreciated....

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Saurabh DhobleSaurabh Dhoble

Was busy all day, finally got a chance to dig into your issue.

Modify your constructor like this :

 

public final Case currentCaseObject;
//The general constructor...
public CaseClone (ApexPages.Standardcontroller controller){

this.controller = controller;
CaseIdOld = controller.getId();

// Now this is the current case object.
currentCaseObject = (Case) controller.getRecord();
}

 With this, the "currentCaseObject" object will be the available to you throughout the class. So, you don't need to run the SOQL query in the testthis() method to get the object based on ID.

 

For your problem, you can use this :

 

cNew.OwnerId = currentCaseObject.OwnerId;

 Essentially, what you are doing is this :-

* Exposing a field on your VF page where the user can set the Owner ID for the current Case object.

* Grabbing the Owner Id that the user has selected in the testThis() method, and assigning it to the cloned Case object.

* Keep in mind - when the user picks a new owner from the lookup, he is actually picking an owner for the current case. Since you are not saving the current case back to the database (via an Update statement), you are fine. If you ever do this :

update currentCaseObject

 it will persist even your current case object to salesforce. This will result in both your current object and the cloned object show up the owner that the user has chosen for the cloned object.

 

Hope that makes sense. If this answered your question, pls make sure to mark it as answer.

All Answers

Saurabh DhobleSaurabh Dhoble

You can modify your page to let the user first select the person who should be assigned the case. Once the person is selected, they click an <apex:commandButton action="{!testthis}" /> button, that in turn calls the "testthis" method to clone the case and assign the person.

 

Let me know if this makes sense.

Mark VermaatMark Vermaat

Yes it makes sense, the only thing I have having a problem with is when I try to assign a Case Owner, it gives me an error about the sobject not being able to set a polymorphic field?  Maybe I did something wrong, I will try again and get back to you if I get it to work.  

 

 

Saurabh DhobleSaurabh Dhoble

Right, pls post your code in case of issues.

Also, for the polymorphic field error, you can check this post to see if it helps.

Mark VermaatMark Vermaat

I am working on this a bit more, then I will post the code when I have something going.  Thanks for the guidence.  

Mark VermaatMark Vermaat

OK well, I have hit another wall, here is the code:

 

<apex:page standardController="Case"
extensions="CaseClone">
<apex:pageBlock title="Hello {!$User.FirstName}!"><br />
<apex:form>
Please enter a Case Owner for the New Case: <apex:inputText value="{!cNew.Owner}"/>
<apex:commandButton action="{!testthis}" />
</apex:form>
Your support case has been cloned!<br /><br />
<i><b>Warning! Do not hit "refresh" on your browser or your case will be cloned AGAIN</b></i><br />
<br /><br />
<table cellpadding="10">
<tr>
<td>Original Case Number:</td>
<td>{!CaseNumberOld}</td>
<td><a href="/{!CaseIdOld}">Go to this case...</a></td></tr>
<tr>
<td>New Case Number:</td>
<td>{!CaseNumberNew}</td>
<td> <a href="/{!CaseIdNew}">Go to this case...</a>:</td>
</tr>
</table>
<br /><br />
</apex:pageBlock>
</apex:page>

 

The part in red is what is giving me the issue, I am getting an Save error saying: DescriptionResourcePathLocationType

Save error: only aggregate expressions use field aliasingCaseClone.cls/Test CaseClone/src/classesline 24Force.com save problem

 

I am completely lost now.  :(  Everything I have read on the forums and online say that you cannot assign the case owner value because it is from a picklist (either a queue or a person)  Do you know if this is true?  And if it is true I guess I am back to square one. :(  

 

Saurabh DhobleSaurabh Dhoble

Yeah, well, so after about 30 minutes digging around and writing some code, I'm happy to say that it works :). Here's the code (make sure you browse the page passing in an existing case Id, e.g. https://c.na9.visual.force.com/apex/CaseCloneExample?id=500E0000002O6kZ) :-

 

<apex:page standardController="Case" extensions="CaseCloneExampleController">
    <apex:form >
    	<apex:pageBlock id="myPageBlock">
        	<apex:outputLabel >Select the User : </apex:outputLabel>
            <apex:inputField value="{!Case.OwnerId}" />
            <apex:commandButton value="Clone Case" action="{!CloneCase}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

And here's the Controller :-

 

public class CaseCloneExampleController {
    public final Case caseObject;
    
    public CaseCloneExampleController(Apexpages.StandardController con)
    {
        caseObject = (Case) con.getRecord();
    }
    public void CloneCase()
    {
        Case newCase = new Case();
        /******************************/
        /* CLONING LOGIC GOES HERE
        /******************************/
        newCase.OwnerId = caseObject.OwnerId;
        insert newcase;
    }
}

 

I have highlighted the important part above. The page will ask the user to choose a new case owner, and then clone the existing case with the new owner.

If this answers your question, please mark it as answer.

Mark VermaatMark Vermaat

OK, well its almost good, however its not changing the CaseOwner name from the controller:  Here is the part where the issue is:

 

public with sharing class CaseClone {

//Adding Instance variable for standard controller.
public ApexPages.Standardcontroller controller {get; set;}

//Trial Class for cloning cases...
public String CaseNumberOld {get; set;}
public String CaseNumberNew {get; set;}

public Id CaseIdOld {get; set;}
public Id CaseIdNew {get; set;}

//The general constructor...
public CaseClone (ApexPages.Standardcontroller controller){

this.controller = controller;
CaseIdOld = controller.getId();
}

//this will be called from the VisualForce Page - In this kind of VForce page / controller - constructor cannot do the work

public void testthis(){
//this gets the case
Case cOld = [
Select c.AccountId,
c.CaseNumber,
c.ContactId,
c.Description,
c.Origin,
c.Priority,
c.Reason,
c.Status,
c.Subject,
c.Type,
c.SuppliedEmail
FROM Case c WHERE id = :CaseIdOld ];

//Grad old case number
CaseNumberOld = cOld.CaseNumber;

//Clone original (resets id)
Case cNew = cOld.clone(false, false);

//override some fields
cNew.Subject = cOld.Subject + '[Cloned Copy]';
cNew.Status = 'New';
cNew.ParentId = cOld.Id;

//instert cloned case
insert cNew;
CaseIdNew = cNew.Id;
cNew.OwnerId = case.OwnerId;  <---(not sure how to pull value from VF page)

//get caseNumber
Case cTemp = [SELECT id, CaseNumber FROM Case WHERE id = :cNew.Id];
CaseNumberNew = cTemp.CaseNumber;

 

Again I really appreciate your help, if you could see where I am going wrong and suggest a fix that would be great.

 

Here is the VF page as well:

 

<apex:page standardController="Case"
extensions="CaseClone">
<apex:pageBlock title="Hello {!$User.FirstName}!"><br />
<apex:form >
<apex:pageBlock id="myPageBlock">
<apex:outputLabel >Select the User : </apex:outputLabel>
<apex:inputField value="{!Case.OwnerId}" />
<apex:commandButton value="Clone Case" action="{!testthis}"/>
</apex:pageBlock>
<br /><br />
<table cellpadding="10">
<tr>

 

Thanks for everything I have learnt alot.  

 

 

Saurabh DhobleSaurabh Dhoble

Was busy all day, finally got a chance to dig into your issue.

Modify your constructor like this :

 

public final Case currentCaseObject;
//The general constructor...
public CaseClone (ApexPages.Standardcontroller controller){

this.controller = controller;
CaseIdOld = controller.getId();

// Now this is the current case object.
currentCaseObject = (Case) controller.getRecord();
}

 With this, the "currentCaseObject" object will be the available to you throughout the class. So, you don't need to run the SOQL query in the testthis() method to get the object based on ID.

 

For your problem, you can use this :

 

cNew.OwnerId = currentCaseObject.OwnerId;

 Essentially, what you are doing is this :-

* Exposing a field on your VF page where the user can set the Owner ID for the current Case object.

* Grabbing the Owner Id that the user has selected in the testThis() method, and assigning it to the cloned Case object.

* Keep in mind - when the user picks a new owner from the lookup, he is actually picking an owner for the current case. Since you are not saving the current case back to the database (via an Update statement), you are fine. If you ever do this :

update currentCaseObject

 it will persist even your current case object to salesforce. This will result in both your current object and the cloned object show up the owner that the user has chosen for the cloned object.

 

Hope that makes sense. If this answered your question, pls make sure to mark it as answer.

This was selected as the best answer
Mark VermaatMark Vermaat

Thank you, that cleared up everything. Everything seems to be working great now.  Again thank you for all your help.

 

Cheers.

Mark VermaatMark Vermaat

Thanks again for your help.  I have run into one last snag in my VF Page.  The cloning and Adding the New case owner works as planned, again thanks to your help.   

 

The one issue I am having now is I have linked that once the new case is created, I get the new case number and I want to give the user the ability to click on it to open the case in the active Iframe.   This is what I have, and it seems like it is trying to do something but it just hangs....

 

<apex:pageBlock title="body">
<table cellpadding="10">
<tr><td>Original Case Number: </td> <td>{!CaseNumberOld}</td> <td><a href="/{!CaseIdOld}">Click here to go to Original Case.</a></td></tr>
<tr><td>New Case Number: </td><td>{!CaseNumberNew}</td><td><a href="/{!CaseIdNew}">Click here to go to New Case.</a></td></tr>

</table>
</apex:pageBlock>

 

Any ideas would help....

 

Thanks again.