You need to sign in to do that
Don't have an account?

Apex Trigger to copy lookup field name instead of id
Hi,
I am actually trying to build a very basic trigger on opportunities.
The field country_object__c is a lookup field to an object with country information.
The selection he makes there (country name) should be filled into the country__c field when the entry is saved.
The code below however only puts in the id of the country object record.
Background is that the field country__c is used in many reports and sharing rules which I do not want to rework.
Many thanks!!!!
Trigger Update_Country on Opportunity (before update, before insert) { // Fill the Country__c field with the lookup name for (Opportunity o : Trigger.new) { o.Country__c = o.Country_Object__c; } }
Hi,
Just to clarify
1. There is a Country_Object__c with fields Name, Country__c
2. There is a Lookup relation between Opportunity and Country_Object__c
3. When you associate Opportunity with Country_Object__c the Value from Country__c on Country_Object__C must be copied to Country__c on Opportunity.
Here is the updated Trigger and Unit Test
Trigger
Unit Test
If it works for you, do mark them as 'Solution' as it may help others.
Thanks
krpr
All Answers
Hi,
Below code should be good for your requirement :
Let me know if I missed something. Hope it helps!
Hey, first of all you can just use a formula field instead of trigger !!
Create a formula field (text) and its value would be something like this : Country_Object__r.Name. Thats it !!
In case you want this in trigger, change the code to the following:
o.Country__c = o.Country_Object__r.Name;
o.Country_Object__c is a lookup field hence it contains the ID of the record to which it is related to.
Hi Vishal,
Many thanks for your help!!!
I have put together a test class, but unfortunately it is not working.
Maybe you have a chance to have a look...
Many thanks in advance!
Patrick
Hi,
You created an opportunity but you forgot to insert/update it in your test class. That's the reason your code coverage failed.
Below code should be good for you :
Let me know if you face any issues.
Hi Vishal,
Many thanks for the hint!
When trying to delpoy I still get an error:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Update_Country_Opportunity: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Update_Country_Opportunity: line 21, column 70: []", Failure Stac...
It seems to be referencing this part of the trigger
o.Country__c = mapCountryObjects.get(o.Country_Object__c).Name;
Your help is highly appreciated!!!
Hi,
Many thanks for your support.
When I try to deploy I get the following error refering to the line of code you proposed.
Do you have any idea what to do?
Many thanks!!!!
Hi,
Just to clarify
1. There is a Country_Object__c with fields Name, Country__c
2. There is a Lookup relation between Opportunity and Country_Object__c
3. When you associate Opportunity with Country_Object__c the Value from Country__c on Country_Object__C must be copied to Country__c on Opportunity.
Here is the updated Trigger and Unit Test
Trigger
Unit Test
If it works for you, do mark them as 'Solution' as it may help others.
Thanks
krpr
Many thanks krpr!!!
I was able to deploy the code.