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
gaurav.sfdcgaurav.sfdc 

copy look-up value to text field

I have two objects A and B. A has a text filed (contact_name__c) and B has a look-up field(contact_name__c). Now I want to copy the look-up value (contact name not Id) to object A's text field

objectA.contact_name__c = ObjectB.Contact_name__c.Name;

but this give foreign key error
I also tried this
objectA.contact_name__c = ObjectB.Contact_name__r.Name;
this compiles but gives run-time error that sObject retrieved via SOQL without retrieving requested field..
I dont want to use formula field. i found some workaround that first fire select query to get contact name than assign to it. Is there some other way?
Best Answer chosen by gaurav.sfdc
Akshay DeshmukhAkshay Deshmukh
try this code.

public Pagereference copyAllFields(){
    
     list<Opportunity> oppList = [Select o.Name, o.Id, o.CreatedBy.Name, o.CreatedById, o.Amount, o.Account.Name, o.Account.Id, o.AccountId
             From Opportunity o
             Where o.Amount < 10000];
     list<merchandise__c> merchandiseList = new list<merchandise__c>();
    
     for(Opportunity opp : oppList){
      merchandise__c  merchan= new merchandise__c();
      merchan.Name = 'test';
      merchan.text1__c = opp.CreatedBy.Name;
      merchan.text2__c = opp.Account.Name;
      merchandiseList.add(merchan);
     
     }
    
     if(merchandiseList.isEMpty() == false)
      insert merchandiseList;
   
     return null;
   }

this code is tested and working fine..

here i am copying lookup fields of opportunity to merchandise object's text fields

All Answers

Akshay DeshmukhAkshay Deshmukh
Hi gaurav,
If you are copying ObjectsB.Contact_name__c.name in a code then you have to query this field. and assign it to the ObjectA's field as below:
objectA.contact_name__c = ObjectB.Contact_name__r.Name;

but if you want to do it without code, then you have to write formula to copy from ObjectB to ObjectA.

Thanks.
gaurav.sfdcgaurav.sfdc
Thanks Akshay, Could you please elaborate more.. on "Query this filed ..". See on a button click I am calling a function in Apex class. As parameter a i m sending list of opportunities. My objective is to create a new objects (similar to opp object) and copy all its fileds to that object. That object should have not have look-up fields but text fields. I only got struck while coying look-up values. Can you please give a short example how to query and use it like above. 
As an alternative I am querying name from Contact object, creating a new contact object and then using that object to copy value..?
Akshay DeshmukhAkshay Deshmukh
try this code.

public Pagereference copyAllFields(){
    
     list<Opportunity> oppList = [Select o.Name, o.Id, o.CreatedBy.Name, o.CreatedById, o.Amount, o.Account.Name, o.Account.Id, o.AccountId
             From Opportunity o
             Where o.Amount < 10000];
     list<merchandise__c> merchandiseList = new list<merchandise__c>();
    
     for(Opportunity opp : oppList){
      merchandise__c  merchan= new merchandise__c();
      merchan.Name = 'test';
      merchan.text1__c = opp.CreatedBy.Name;
      merchan.text2__c = opp.Account.Name;
      merchandiseList.add(merchan);
     
     }
    
     if(merchandiseList.isEMpty() == false)
      insert merchandiseList;
   
     return null;
   }

this code is tested and working fine..

here i am copying lookup fields of opportunity to merchandise object's text fields
This was selected as the best answer
gaurav.sfdcgaurav.sfdc
Make sense.. Akshay.. but the only problem here is I have 40-50 fields on opp and I can't use "select contact.name, * from opp... " ... :) but what I did , I for-looped opp
for (opp in lstOpp)
{
  lstContact.add (new Contact(Name=opp.contact.name)
}
the create map of it and then 
for (opp: lstOpp)
{
customobj= new CustomObj();
customobj.Name = map.get(opp.contact__c)
.....
}

yours is bit simpler.. but still I would give a best answer to you..