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
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana 

Update a field to change the lookup to another object

Hello,

I have a an field on a custom object that is of type loopup. I want to change the lookup to another object. From what I understand, the only option is to create a new field pointing to the new object and rename it. But I do have many triggers, classes, relationship that is associated with the old field and want to change that too. I do like to keep the old field and rename to field__old__c and rename the new one as field__c  which is the name of current existing field.
But due to those triggers and other relationship having on the existing field, I am having lot of cyclic reference issues. Is there a way to overcome this? What is the general rule to manage these type of changes and move to production?
Tarun J.Tarun J.
Hello Gopikrishnan,

If you change existing lookup field to new one, all you code wherever you are populating this field using current object reference get failed. This will break all your code. So, if you want to change the lookup reference to another object, you need to update all classes/triggers where this field is being refered.

Also, note that,this is affect your existing records as well. 

It would be good if you create a new field and label it as existing field and use it.

-Thanks,
TK
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana
Thank you Tharun!! When you say 'It would be good if you create a new field and label it as existing field and use it'..... how can I rename a field with the name of an existing field ? Content by Gopi. Typos by iPhone.
Tarun J.Tarun J.
I mean to say, you can use same label name as an existing field for UI purpose but API name will be different as you cannot have two fields with same API name in same object.
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana
Right. So basically I have to create new field that points to new object, then change the code to use the new field. Then rename both the field. Right? Content by Gopi. Typos by iPhone.
Tarun J.Tarun J.
Correct.

Just one question, as you are going to update code with new field, why are you going to interchange the API names of both fields? You can give label name of new field as existing field and use different API name. Anyways, API names are for back-end prupose, it won't have any impact on UI. Also, it is not advisable to change API names of the exiting field.

-Thanks,
TK
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana
Thanks Tharun!! That make sense. Content by Gopi. Typos by iPhone.