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
DoondiDoondi 

Trigger is giving me Id instead of Name?

Hi,
Below is my simple Trigger on two custom objects with MD relationship,
For Name I am getting Id, where as for Property__c I am getting correct Value.
below is my trigger
trigger CreateNewRecord on Deal__c (after insert, after update) 
    {
    set<id> triggerIds = trigger.newMap.keyset();
	List <Project__c> Project = [select id, Name, Deal__r.Property__r.Name from Project__c where id in :triggerIds];
    for(Deal__c deal : Trigger.new)
    {
        if( deal.Type__c == 'New' ) 
            
            Project.add(new Project__c
                              (
								Name = deal.Property__r.Name, // this field is giving ID instead of Name
								Property__c = deal.Property__c // This field is giving correct update.
                              )
                        );
    }
        insert Project;
    }

 
Best Answer chosen by Doondi
Jolly_BirdiJolly_Birdi
Hello @Doondi,

Please check the below Code:
trigger CreateNewRecord on Deal__c (after insert, after update) 
{
	set<id> triggerIds = trigger.newMap.keyset();
	List <Project__c> Project = [select id, Name, Deal__r.Property__r.Name from Project__c where id in :triggerIds];

	List <Project__c> projectsForInsert = new List<Project__c>() ;


	for(Deal__c deal : [Select id, Type__c, Property__r.Name, Property__c from Deal__c where id in :triggerIds])
	{
		if( deal.Type__c == 'New' ) {
			projectsForInsert.add(new Project__c
							  (
								Name = deal.Property__r.Name, // this field is giving ID instead of Name
								Property__c = deal.Property__c // This field is giving correct update.
							  )
			);
		}
	}
	insert projectsForInsert;
}

Thanks
Jolly Birdi

All Answers

Raj VakatiRaj Vakati
I think
trigger CreateNewRecord on Deal__c (after insert, after update) 
    {
    set<id> triggerIds = trigger.newMap.keyset();
	List <Project__c> Project = [select id, Name, Deal__r.Property__r.Name from Project__c where id in :triggerIds];
	
	List <Project__c> projectsForInsert = new List<Project__c>() ; 
	
    for(Deal__c deal : Trigger.new)
    {
        if( deal.Type__c == 'New' ) 
            
            projectsForInsert.add(new Project__c
                              (
								Name = deal.Property__r.Name, // this field is giving ID instead of Name
								Property__c = deal.Property__c // This field is giving correct update.
                              )
                        );
    }
        insert projectsForInsert;
    }

 your code should be like this

 
DoondiDoondi
I changed the code as you said, but still I am getting ID only. 
Raj VakatiRaj Vakati
If my guess is correct you are storing ID in the name field .. check your data once .. 
DoondiDoondi
Yes at present ID is getting saved in Standard Name field, But I want Name instead of Id. 
Jolly_BirdiJolly_Birdi
Hello @Doondi,

Please check the below Code:
trigger CreateNewRecord on Deal__c (after insert, after update) 
{
	set<id> triggerIds = trigger.newMap.keyset();
	List <Project__c> Project = [select id, Name, Deal__r.Property__r.Name from Project__c where id in :triggerIds];

	List <Project__c> projectsForInsert = new List<Project__c>() ;


	for(Deal__c deal : [Select id, Type__c, Property__r.Name, Property__c from Deal__c where id in :triggerIds])
	{
		if( deal.Type__c == 'New' ) {
			projectsForInsert.add(new Project__c
							  (
								Name = deal.Property__r.Name, // this field is giving ID instead of Name
								Property__c = deal.Property__c // This field is giving correct update.
							  )
			);
		}
	}
	insert projectsForInsert;
}

Thanks
Jolly Birdi
This was selected as the best answer
DoondiDoondi
@Jolly Birdi Thanks for your edits, it's working as desired. {Thanks a ton for educating me }
@Raj Vakati ThankYou too!! for your time. 
 
Jolly_BirdiJolly_Birdi
Hello @Doondi

please mark it as best answer and make it resolved.

Thanks,
Jolly Birdi
DoondiDoondi
before insert projectsForInsert, I wanted to know if duplicate Name exists. How do I do that?