+ Start a Discussion
Akis AthanasiadisAkis Athanasiadis 

Auto delete records

Hello.

I have a question.I need to create an auto delete process throuch process builder.

I have a custom object "revenues" and a custom object "purchases".Every time i add,create or delete a revenue a record from purchases must be created/updated/deleted.As for now I have accomplished the creation and update process but I cannot find a way to auto delete a purchase record when the relative revenue record is deleted.Can you give me some help with this?

Best Answer chosen by Akis Athanasiadis
Akis AthanasiadisAkis Athanasiadis
I get an error "content is not allowed in prolog" in line 1

All Answers

UC InnovationUC Innovation
Hi Tehodosios,

You will need to write a delete trigger to accomplish this. How are revenues and purchases related? Which one is the parent and which is the child?
Akis AthanasiadisAkis Athanasiadis
Hmmmm.I know that using a trigger this could be implemented.that's why I posted this question.What would be the apex code in order to import it into the process builder?They are related with a look up relationship from the purchases to revenues.
UC InnovationUC Innovation
Is it OK to use a trigger in your case? The trigger code is very simple. 
 
trigger DeletePurchases on Revenue__c (before delete)
{
	Set<id> idSet = new Set<id>();
	for (Revenue__c r : trigger.old)
	{
		idset.add(r.id);
	}
	
    // Assuming Revenue__c is the lookup field on Purchase__c looking up to revenue.
	list<Purchase__c> listOfPurchases = [SELECT id FROM Purchase__c WHERE Revenue__c in: idset];
	
	delete listOfPurchases;
}

 
Akis AthanasiadisAkis Athanasiadis
Aaaah.great I will check it later.Although I discovered another problem regarding update.When I update the parent record from revenues all the child records from purchases are updated.not only the child record that belongs to this specific parent record.Do you hav any idea of how to avoid this problem?
Akis AthanasiadisAkis Athanasiadis
I get an error "content is not allowed in prolog" in line 1
This was selected as the best answer
UC InnovationUC Innovation
Hi Theodosios,

Where did you save your trigger code? It looks like it's not expecting apex code content for some reason. Make sure you go to Setup->Develop->Apex Triggers and you can see your trigger in that list.

As for your update issue, I'd probably need to see your process builder to tell what's going on. Do you mean every child record in the org gets updated? It definitely shouldn't be doing that, it should be updating all the purchase records that belong to the revenue that fired the process builder only (which can be more than one!)
Akis AthanasiadisAkis Athanasiadis

User-added image

As you can see I am already there but I receive this message.As for the problem with the update of the child record from a specific parent record,I found the solution

UC InnovationUC Innovation
Hi Theodosios,

It looks like your filename is a .vfp, meaning you're saving it as a visual force page. Go to the dev console, File-> New -> Apex Trigger. Select your Revenue as the object and write that code in there. The file extension should be .apxt.

Hope this helps!