+ Start a Discussion
Michael MMichael M 

Delete lookup object record when populated in child record

I need to accomplish two steps:
1) automatically populate a lookup field on a Lead child record, with its parent record. Once that is done, we have a couple formula fields on the Lead with information from the parent record that we want to display on the Lead.
2) Once that is done and we have populated all the formula fields, we want to automatically delete the parent record. 

The first step is already done, and this trigger works:

trigger LeadLookupParent on Lead (before insert, before update) {
Set<String> setssn = new Set<String>();
Map<String,Id> mapssn = new Map<String,Id>();
    
for(Lead l :trigger.new){
        if(l.UniqueNumber__c !=null && l.UniqueNumber__c != '') {
            setssn.add(l.UniqueNumber__c);
        }
  }  
if(!setssn.isEmpty()){ 
        for(Parentobj__c re : [SELECT id, Uniquenum__c FROM Parentobj__c WHERE (Uniquenum__c =:setssn)]){
                           mapssn.put(re.Uniquenum__c, re.Id); 
     }
  }
if(mapssn.size() > 0){
        for(Lead ld :trigger.new){
            if(ld.UniqueNumber__c !=null && ld.UniqueNumber__c != '')  {
                ld.Parentobj__c = mapssn.get(ld.UniqueNumber__c); 
            }
        }
    }
}

Where I am stuck is on the second step. I tried this trigger on Lead but it is giving me an error that the id does not exist. I am open to any other solutions or fixing this one:

trigger DeleteParentrecord on Lead (after insert, after update) {

Set<String> leadSsn = new Set<String>();
    for (lead ld: trigger.new){
        if (ld.Parent__c !=null){
            leadSsn.add(ld.Parent__c);
        }
        Parent__c par= [select id from parent__c where id = :leadSsn];
        delete par;
    } 
}
Best Answer chosen by Michael M
Rishabh Bansal 23Rishabh Bansal 23
Hi Michael,

I suppose you can achieve both the functionalities in the same code.

Please refer to this:-
trigger LeadLookupParent on Lead (before insert, before update) {
Set<String> setssn = new Set<String>();
Map<String,Parentobj__c > mapssn = new Map<String,Parentobj__c >();
    
for(Lead l :trigger.new){
        if(l.UniqueNumber__c !=null && l.UniqueNumber__c != '') {
            setssn.add(l.UniqueNumber__c);
        }
  }  
if(!setssn.isEmpty()){ 
        for(Parentobj__c re : [SELECT id, Uniquenum__c FROM Parentobj__c WHERE (Uniquenum__c =:setssn)]){
                           mapssn.put(re.Uniquenum__c, re); 
     }
  }
if(mapssn.size() > 0){
        for(Lead ld :trigger.new){
            if(ld.UniqueNumber__c !=null && ld.UniqueNumber__c != '')  {
                ld.Parentobj__c = mapssn.get(ld.UniqueNumber__c).Id;
                mapssn.get(ld.UniqueNumber__c).customField__c = true; // change the value of custom field
            }
        }
update mapssn.values();
    }
}
Thanks,
Rishabh
 

All Answers

Michael MMichael M
In fact, I don't need to delete it (it seems that deleting it might defeat the purpose, as the formula fields would be removed along with the parent record.) Instead, I can have the custom status field which exists on the parent object changed when the lookup field is populated on the child. I've tried using process builder to accomplish this, but it didn't work. How can that be done? 
Rishabh Bansal 23Rishabh Bansal 23
Hi Michael,

I suppose you can achieve both the functionalities in the same code.

Please refer to this:-
trigger LeadLookupParent on Lead (before insert, before update) {
Set<String> setssn = new Set<String>();
Map<String,Parentobj__c > mapssn = new Map<String,Parentobj__c >();
    
for(Lead l :trigger.new){
        if(l.UniqueNumber__c !=null && l.UniqueNumber__c != '') {
            setssn.add(l.UniqueNumber__c);
        }
  }  
if(!setssn.isEmpty()){ 
        for(Parentobj__c re : [SELECT id, Uniquenum__c FROM Parentobj__c WHERE (Uniquenum__c =:setssn)]){
                           mapssn.put(re.Uniquenum__c, re); 
     }
  }
if(mapssn.size() > 0){
        for(Lead ld :trigger.new){
            if(ld.UniqueNumber__c !=null && ld.UniqueNumber__c != '')  {
                ld.Parentobj__c = mapssn.get(ld.UniqueNumber__c).Id;
                mapssn.get(ld.UniqueNumber__c).customField__c = true; // change the value of custom field
            }
        }
update mapssn.values();
    }
}
Thanks,
Rishabh
 
This was selected as the best answer
Michael MMichael M
This worked, thank you Rishabh. Just a question for future reference: I found that this works for a checkbox field, e.g. to check off the custom field on the parent object. But to update a picklist value it didn't seem to work. Why would that be?