You need to sign in to do that
Don't have an account?
Michael 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;
}
}
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;
}
}
I suppose you can achieve both the functionalities in the same code.
Please refer to this:- Thanks,
Rishabh
All Answers
I suppose you can achieve both the functionalities in the same code.
Please refer to this:- Thanks,
Rishabh