+ Start a Discussion
Devendra Hirulkar 3Devendra Hirulkar 3 

how to return name instated of id

i write a trigger that copy one obj to another
but when it copy it return a id instated of name
how to solve this
by using formula or some changes in my trigger
(i want name instated of id)
For example, the Owner of an Opportunity record is Jim Smith.  When I use the formula (trigger)to pass his name to the associated new object, I only get his User ID. 
 
John PipkinJohn Pipkin
Devendra, 

Lookup fields, like Opportunity Owner, only store Ids of the associated object. Only in the UI does the name show. If you try updating a lookup field with a name (String), you will get an error. 
sfdc_ninjasfdc_ninja
When you are trying to set an ID for a lookup field, you would pass the Id, such as 
 
Your_Lookup_Field__c = Opportunity.Id;

If you are trying to get the name of a field already connected through a lookup field, then you can use the relationship name, which would be like below
 
Your_Text_Field__c = Opportunity__r.Name;

This does assume that you already have the lookup field already populated with the opportunity Id.
Devendra Hirulkar 3Devendra Hirulkar 3
Hello John
so how i put an name instated of id
in my task i loolup product name and copy to another obj but it can shows its id instated name
so in which way i put name can it possible through the formula
thanks,
devendra
John PipkinJohn Pipkin
Is the field you are updating a lookup field or text field? 

If you get getting the Product via a lookup field on the original object and then trying to copy the name over to a text field then you will need to get the Product name through the relationship. Just like sfdc_ninja said:

"Select Id, Product__r.Name from Opportunity". You can access fields on the related object in a lookup relationship by using __r notation. Then use that in your update call. 

If this still doesn't answer your question, please post a snippet of your code so I can get a better understanding of what is going on. 

Thanks
Devendra Hirulkar 3Devendra Hirulkar 3
here is my trigger john
(so what changes i needed)
trigger copypro on Subsc__c (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Subsc__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
  }

  //Populate the map. Also make sure you select the field you want to update, amount
  //The child relationship is more likely called Quotes__r (not Quote__r) but check
  //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
  Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c  FROM Subscs__r) FROM Account WHERE ID IN :listIds]);

  for (Subsc__c sub : Trigger.new)
  {
     Account ac = Acc.get(sub.Company_Name__c);
     ac.Product_Name__c=sub.Product__c;
  }

  update Acc.values();
}
John PipkinJohn Pipkin
Try this:
 
trigger copypro on Subsc__c (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Subsc__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
  }

  //Populate the map. Also make sure you select the field you want to update, amount
  //The child relationship is more likely called Quotes__r (not Quote__r) but check
  //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
  if(!listIds.isEmpty())
  	Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__r.Name  FROM Subscs__r) FROM Account WHERE ID IN :listIds]);

  for (Subsc__c sub : Trigger.new)
  {
     Account ac = Acc.get(sub.Company_Name__c);
     ac.Product_Name__c=sub.Product__r.Name;
  }

  update Acc.values();
}