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
JNicJNic 

Get values from a record and put them into a new record

Hey guys,

 

I've got a record ID in my Requirement__c obj that has some values in various fields. This record id is treated like a template, so I've got users going to a new page where they can create a new record for Reqiurement__c that has preset values set by this record ID.

 

How do I query that record ID, get the values, then put them on my custom page?

 

Here is my controller:

 

public with sharing class RequirementNewController {

//Set the string variables for each parameter
public string trmInAction {get;set;}
Public string rqmInAction {get;set;}
Public string trmprpInAction {get;set;}

//Make sure we can use this page as an extension on and above the standard controller
private ApexPages.StandardController controller;
public RequirementNewController(ApexPages.StandardController controller)
{
this.controller = controller;
//Get the parameters from the page so we can map them back
PageReference pageRef = ApexPages.CurrentPage();
Map<String, String> pageParameters = pageRef.getParameters();
//Set each parameter - Name these intuitively so we know where to put them.
trmInAction = pageParameters.get('trmInAction');
trmprpInAction = pageParameters.get('trmprpInAction');
rqmInAction = pageParameters.get('rqmInAction'); <--- This is the ID I need to get the values of

//Map the parameters on fields on IMAC ticket record.
//Checks to see what link was clicked - IMAC_ticket__c

Requirement__c RqmRecord = (Requirement__c) controller.getRecord();
RqmRecord.Terminal__c = trmInAction;
RqmRecord.Terminal_prep_order_ID__c = trmprpInAction;
RqmRecord.name = rqmInAction.Name; <--- This is where I want to set the values
}
}

 So when users load this page, its all prefilled with values from "rqmInAction"

 

See what I mean?

 

I'm missing something stupid and obvious, please help me out.

 

 

Thanks!

JN

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Cool_DevloperCool_Devloper

JN,

You can query the record and get all the fields in your controller constructor as per the RecordID you are passing to the VF page.

Then, you can instantiate a new object of Requirement__c and set the field values as per the ones fetched above.

Once you do this, they will appear pre-populated on your VF page!

Cool_D

All Answers

Cool_DevloperCool_Devloper

JN,

You can query the record and get all the fields in your controller constructor as per the RecordID you are passing to the VF page.

Then, you can instantiate a new object of Requirement__c and set the field values as per the ones fetched above.

Once you do this, they will appear pre-populated on your VF page!

Cool_D

This was selected as the best answer
JNicJNic

I see... I figured that's what I would have to do, but I can't quite piece it together.

 

 

I've got this:

 

//Set the string variables for each parameter
public string trmInAction {get;set;} //The Terminal from an IMACtion
Public string rqmInAction {get;set;}//This is for variable IMAC__r.Requirement__c
Public string trmprpInAction {get;set;}
public List<Requirement__c> masterRqm = [select Id, Name from Requirement__c where Id = :rqmInAction limit 1];

//Make sure we can use this page as an extension on and above the standard controller
private ApexPages.StandardController controller;
public RequirementNewController(ApexPages.StandardController controller)
{
this.controller = controller;
//Get the parameters from the page so we can map them back
PageReference pageRef = ApexPages.CurrentPage();
Map<String, String> pageParameters = pageRef.getParameters();
//Set each parameter - Name these intuitively so we know where to put them.
trmInAction = pageParameters.get('trmInAction');
trmprpInAction = pageParameters.get('trmprpInAction');
rqmInAction = pageParameters.get('rqmInAction');

Requirement__c RqmRecord = (Requirement__c) controller.getRecord();
RqmRecord.Terminal__c = trmInAction;
RqmRecord.Terminal_prep_order_ID__c = trmprpInAction;
RqmRecord.name = masterRqm.Name;
}

 But now it give me this error:

Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Requirement__c

 

And I have no idea what that means...

 

:smileysad:

 

 

WesNolte__cWesNolte__c

Hey

 

You're almost there. You need to expose the new object, put this at the top of your class:

 

public Requirement__c RqmRecord{get;set;}

 

An change this:

 

 Requirement__c RqmRecord = (Requirement__c) controller.getRecord();

 

to This

 

RqmRecord = (Requirement__c) controller.getRecord(); 

 

And then somewhere in your page:

 

<apex:outputText value="{!RqmRecord.Terminal__c}" />

 

You can do that for each of your fields.

 

Just a quick question.. are you performing a 'clone the record, but let the user override values' sort of operation?

 

Wes 

 

 

JNicJNic

yes, wes.

It's a clone with the ability to override.

WesNolte__cWesNolte__c
Cool.. I thought it might be. The above should get your code working..
JNicJNic

Hey wez,

 

I've tried what you've said, but it still gives me the same error:

 

 

 Compile Error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Requirement__c

 

 

public with sharing class RequirementNewController {

//Set the string variables for each parameter
Public string trmInAction {get;set;} //The Terminal from an IMACtion
Public string rqmInAction {get;set;}//This is for variable IMAC__r.Requirement__c
Public string trmprpInAction {get;set;}
//Expose the Requirement object
Public Requirement__c RqmRecord{get;set;}
//Get the master requirement record whos ID we passed from the IMACtion
public List<Requirement__c> masterRqm = [select Id, Name from Requirement__c where Id = :rqmInAction limit 1];

//Make sure we can use this page as an extension on and above the standard controller
private ApexPages.StandardController controller;

public RequirementNewController(ApexPages.StandardController controller)
{
this.controller = controller;
//Get the parameters from the page so we can map them back
PageReference pageRef = ApexPages.CurrentPage();
Map<String, String> pageParameters = pageRef.getParameters();
//Set each parameter - Name these intuitively so we know where to put them.
trmInAction = pageParameters.get('trmInAction');
trmprpInAction = pageParameters.get('trmprpInAction');
rqmInAction = pageParameters.get('rqmInAction');

//Map the parameters on fields on the record
RqmRecord = (Requirement__c) controller.getRecord();
RqmRecord.Terminal__c = trmInAction;
RqmRecord.Terminal_prep_order_ID__c = trmprpInAction;
RqmRecord.name = masterRqm.Name;
}
}

 

 

 

JNicJNic

Hey guys,

 

I've figured out an alternate way to do this. And it's much more elegant than my previous attempt:

 

 

public with sharing class IMACtionNewParentController {

//Set the string variables for each parameter
public string trmInAction {get;set;} //The Terminal from IMACtion
public string trmLocInAction {get;set;} //The Origin_location__c from IMACtion
public string destInAction {get;set;} //The Destination__c (Location) from IMACtion
public string thisAction {get;set;} //The IMACtion Actual from IMACtion
public string clickedLinkInAction {get;set;} //What link was clicked from IMACion?
Public string rqmInAction {get;set;} //Requirements master ID passed from IMACtion
// Public string trmprpInAction {get;set;} //I don't think I need this... I've commented it out for now.

//Make sure we can use this page as an extension on and above the standard controller
private ApexPages.StandardController controller;

public IMACtionNewParentController(ApexPages.StandardController controller)
{
this.controller = controller;
//Get the parameters from the page so we can map them back
PageReference pageRef = ApexPages.CurrentPage();
Map<String, String> pageParameters = pageRef.getParameters();
//Set each parameter - Name these intuitively so we know where to put them.
trmInAction = pageParameters.get('trmInAction');
trmLocInAction = pageParameters.get('trmLocInAction');
destInAction = pageParameters.get('destInAction');
thisAction = pageParameters.get('thisAction');
clickedLinkInAction = pageParameters.get('clickedLinkInAction');
rqmInAction = pageParameters.get('rqmInAction');

//Map the parameters on fields on Terminal prep order record.
//Checks to see what link was clicked - Terminal_prep_order__c
//Keep in mind, this might have a trmInAction and it might not.
if(clickedLinkInAction == 'Terminal_prep_order__c'){
Terminal_prep_order__c Trmprp = (Terminal_prep_order__c) controller.getRecord();
if(trmInAction != ''){
Trmprp.Terminal__c = trmInAction;
}
Trmprp.Location__c = destInAction;
Trmprp.IMACtion__c = thisAction;
Trmprp.Requested_terminal_model__c = 'Not required/requested';
}
}
//Use this save function for TRMPRPs that need requirements.
public PageReference saveTrmPrp() {
//Get the master requirements record we passed earlier
Requirement__c masterRqm =[select Id, Name, Terminal_condition__c from Requirement__c where id=:rqmInAction];
//Clone the master - name it with clone (for now) and insert
Requirement__c newRqm = masterRqm.clone(false, true);
newRqm.Name = masterRqm.Name + ' CLONE';
newRqm.Terminal_condition__c = masterRqm.Terminal_condition__c;
insert newRqm;

//Get the Terminal Prep we just made
Terminal_prep_order__c trmprp = (Terminal_prep_order__c) controller.getRecord();
//Set the Requirements as the cloned requirements we just inserted and insert
trmprp.Requirements__c = newRqm.Id;
insert trmPrp;

//Send the user to the new requirements clone
PageReference toRequirements = new PageReference('/apex/TrmPrpWithRqm?id='+newRqm.Id);
toRequirements.setRedirect(true);
return toRequirements;
}
}

 

 And it works beautifully!

Thanks for all your help.

 

pmozz01pmozz01
I have been experiencing a similar issue where I have a customer controller for an Invoice object, but want values from the parent object - Workorder.  To address the problem, did you replace your Requirements controller with one for the IMACtion controller, or does that IMACtion controller just set up the Requirements controller?  Forgive me if this is a really dumb question, but I am extremely new at this :)
JNicJNic

I've got one controller per object... So my IMACtion controller creates the RQM record, then sends the user to the RQM edit page for customization. I then have a method on the RQM controller to send the user to the finally completed page... which uses yet another controller for the Terminal prep order.

 

Typically, a more experienced developer wouldn't need to use so many controllers in an app that relates all these record together, but I did it so it was easier for me to understand and work with.