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
Asif Khan 44Asif Khan 44 

Delete records from 2 unrelated objects

I have 2 objects A and B each having a phone number  field. the Mobile number field of Object A automatically gets updated in the phone number field in Object B.Now I want that all the records in B should be deleted if its matching record(phone Number) isnt found in Object A.

Object A ---> mobile number
Object B --> phone number
please note : I want the deletion of the orphan records of B working in a trigger
A Query to deal with this conundrum would be helpful and much appreciated
Thanks



 
Best Answer chosen by Asif Khan 44
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Asif, 

You can run one time script, if you have a lot of orphan records in B - 
map<String,String> phoneAndNameMap = new map<String,String>(); 
for(ObjectA a : [SELECT mobileNo, name from ObjectA]) {
		phoneAndNameMap.put(a.mobileNo, a.name);
}

List<ObjectB> objBList = new List<ObjectB>();
for(ObjectB b : [SELECT phoneNO, name from ObjectB]){
	if(phoneAndNameMap.get(b.phoneNO) == null){
		objBList.add(b);
	}
}

if(objBList!=null && objBList.size()>0){
	delete objBList;
}
then, you can write trigger on B, on UPDATE, you can check whether record in present in Object A or not, If not you can delete the record.


 

All Answers

Tammer SalemTammer Salem
If I understand this correctly Object A is the parent and Object B is the child? And you say that you are setting the phone number field in Object B records when A phone number field is updated? So why would you ever have unmatched phone numbers in Object B? (is this becuase users would manually change it)?
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Asif, 

You can run one time script, if you have a lot of orphan records in B - 
map<String,String> phoneAndNameMap = new map<String,String>(); 
for(ObjectA a : [SELECT mobileNo, name from ObjectA]) {
		phoneAndNameMap.put(a.mobileNo, a.name);
}

List<ObjectB> objBList = new List<ObjectB>();
for(ObjectB b : [SELECT phoneNO, name from ObjectB]){
	if(phoneAndNameMap.get(b.phoneNO) == null){
		objBList.add(b);
	}
}

if(objBList!=null && objBList.size()>0){
	delete objBList;
}
then, you can write trigger on B, on UPDATE, you can check whether record in present in Object A or not, If not you can delete the record.


 
This was selected as the best answer
Asif Khan 44Asif Khan 44

well , what you said is correct , but the situation m faced with right now is that there was some dummy data along with real data in Object B, now I wish to delete those dummy data using this logic that if the phone field in OBject A does not match with the mobile field in Object B , then those records should be deleted in Object B 

 

Asif Khan 44Asif Khan 44
And there is no relationship between oBject a and object b. But I have used trigger to insert  data in Object B and that trigger is fired when an insertion of record happens in Object A 
Asif Khan 44Asif Khan 44
Thanks Sumit Kumar Singh, Your code works perfectly fine .