Im a beginner and still learning to write apex.. Im creating a new object from the old object. I want to reparent the Lookup to the new object and delete it from the old object.

//map of ids of new object created from the old object

 Map<Id,Id> objectMap = new Map<Id,Id>();

//Lookup is a lookup field on the custom object called "Object"

List<Lookup__c> lookUpToUpdate = new List<Lookup__c>();
    for(Lookup__c lookUp:[Select id,Object__c from Lookup__c where Object__c in :objectMap.keySet()]){
        lookUp.Object__c = objectMap.get(lookUp.Object__c);
 update lookUpToUpdate;

the above successfully reparents the lookup to the new object, but it doesn't get deleted from the old object, how can I del it from the parent object?

  • You have to update the old record by removing that ID.
    – Arabinda
    Sep 27, 2019 at 6:42

1 Answer 1


I'm assuming your "objectMap" contains the new object in the key and the old object in the value.

You just need to loop through the values and update the old object to not have a value for the lookup. You can add all the updated records (old and new) to the same list to update them all at the same time.

//after your first soql query and for loop, go through and pull the old records and remove the value in the lookup
    for(Lookup__c oldLookup:[Select id,Object__c from Lookup__c where Object__c in :objectMap.values()]){
        oldLookup.Object__c = null;
 update lookUpToUpdate;

You must log in to answer this question.