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
sclosesclose 

How to Combined fields using Apex Class

Hi Team,

 

I have an object called "Projects" and fields on the project record. What I would like to attempt to acheive is when I create an Opp from the Projects object by clicking a create opp button, is to populate two or more fieldsfrom project into one field in an opp. Is this possible? Example below.

 

Projects Object 

 

Fields

Project Name: Freedom Middle School Gym

Project Owner: City of Bakersfield

City: Bakersfield

State: CA

 

The above fields to populate on field on an Opp below. See Example:

 

Opp Object

 

Field

Opportunity Name: Freedom Middle School Gym

                                   City of Bakersfield

                                   Bakersfield, CA   

Best Answer chosen by Admin (Salesforce Developers) 
amarcuteamarcute

Here is an example:

 

opp.Name = Proj.Name + ',' + Proj.Project_Owner__c ;  < -------- Like this

 

 

All Answers

ryanjuptonryanjupton

Yes, concatenate the fields together from project when assigning them to the opportunity name.

sclosesclose

Thank you for taking a look at my post. See below:

 

public with sharing class CreateOppController {

private Project__c Proj ;
public CreateOppController(ApexPages.standardController std)
{
Proj = (Project__c)std.getRecord();
Proj = [select id , Name__c, Project_Owner__c, Bid_Date__c, City__c, State__c , Low_Bidder__c from Project__c where id =: Proj.Id limit 1];


}


public PageReference doCreateOpportunity(){

Opportunity opp = new Opportunity();
opp.Name = Proj.Name &","& Proj.Project_Owner__c ;  < -------- Like this?
opp.BidDate__c = Proj.Bid_Date__c ;
opp.City__c = Proj.City__c;
opp.CloseDate = Date.today();
opp.State__c = Proj.State__c ;
opp.StageName = 'Bid Phase';
opp.Project__c = Proj.id;

sclosesclose
Can you show an example using the code provided?
amarcuteamarcute

Here is an example:

 

opp.Name = Proj.Name + ',' + Proj.Project_Owner__c ;  < -------- Like this

 

 

This was selected as the best answer
sclosesclose
That worked but is there any way to get the fields to stack on top of each other. See below.

As of right now it is separated by a comma. Like this: Freedom Middle School Gym,City of Bakersfield

Example:
Opportunity Name: Freedom Middle School Gym
City of Bakersfield
Bakersfield, CA
crop1645crop1645

In Apex, you would concatenate by using '\n' instead of ','

 

However, opportunity.name field is a text field, not a textarea field so it won't display 'stacked'. That is, the '\n' is not converted to <br/> by SFDC. If your application permits it, use a textarea field instead