You need to sign in to do that
Don't have an account?
gaurav.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?
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?
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
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.
As an alternative I am querying name from Contact object, creating a new contact object and then using that object to copy value..?
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
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..