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
Alexandros Toulis 6Alexandros Toulis 6 

Auto delete record using Apex class/ Process Builder

Hi,

My goal is to be able to delete records in Salesforce, as part of a Process Builder.
As I don't know how to work with Flows I went online and followed this Apex method: https://automationchampion.com/tag/call-an-apex-from-process/

Custom Object: Exec_Ed_Opportunity__c
Related: Events

I want to be able to delete Exec Ed Opportunity related Events based on a field criteria.


Can you help me to build the Apex Class? I get errors when I try.

Thanks,
Alexandros
Best Answer chosen by Alexandros Toulis 6
Alexandros Toulis 6Alexandros Toulis 6
Hi Rounak/ Amarpreet,

The Apex Class now is like:

public class DeleteUnconfirmedEvents{
    @InvocableMethod
    public static void eventDelete(List<id> eventId){
    
        Event evnt = [Select Id, WhatId from Event where Id in: eventId LIMIT 1];
        
        List<Event> eventList = new List<Event>();

        eventList = [select id From Event where WhatId =: evnt.WhatId and Event_Status__c = 'Not Confirmed'];

        if(!eventList.isEmpty()){
            delete eventList;
        }
    }
}



I still get this error: Error Occurred: An Apex error occurred: System.QueryException: List has no rows for assignment to SObject

Is it related to this known issue?  
https://help.salesforce.com/articleView?id=000328824&type=1&mode=1



How can I change the class to not have the error?

Thanks,
Alexandros

All Answers

Rounak SharmaRounak Sharma
hello Alexandros,
public class ExecEdOpportunity {   
  @InvocableMethod
    public static void OppDelete(List EventIds)     {
        List<Exec_Ed_Opportunity__c> eedList = [select id, name (select id From events) from Exec_Ed_Opportunity__c where id IN:EventIds];
        delete eedList ;
   }
}
Please let me know if it solves your problem
AmarpreetAmarpreet
Hi,

Try below code:
public class EventsDelete
{
    @InvocableMethod
    public static void deleteEvents(List<Id> opportunityIds)
    {
        List<Event> events = [SELECT id from Event where whatId IN:opportunityIds ];
        delete events;
    }
}

 
Alexandros Toulis 6Alexandros Toulis 6
Hi @Rounak thanks a lot! Can we add the criteria Event Status equals 'Confirmed' ?
I also get the below error: 
Error: Compile Error: Missing '<' at 'EventIds' at line 3 column 39
User-added image
 
Alexandros Toulis 6Alexandros Toulis 6
**I want to be able to delete Events related to Ex Ed Opportunity with field criteria on Event Status not equals 'Confirmed'
Alexandros Toulis 6Alexandros Toulis 6
In a nutshell 5 Events are related to one Exec Ed Opportunity. Two of them get 'Confirmed'. I need automatically to delete the remaining three Evets.
Rounak SharmaRounak Sharma
hello.
so to remove the first error you need List<id> eventIds
for your condition part you can put the if condtion before the delete event. i hope it will work.
 
Alexandros Toulis 6Alexandros Toulis 6
Hi @Rounak,

Can you re-write the apex class for me? I am still getting errors. Will this apex class delete the events, right?
 
Alexandros Toulis 6Alexandros Toulis 6
Hi Rounak/ Amarpreet,

The Apex Class now is like:

public class DeleteUnconfirmedEvents{
    @InvocableMethod
    public static void eventDelete(List<id> eventId){
    
        Event evnt = [Select Id, WhatId from Event where Id in: eventId LIMIT 1];
        
        List<Event> eventList = new List<Event>();

        eventList = [select id From Event where WhatId =: evnt.WhatId and Event_Status__c = 'Not Confirmed'];

        if(!eventList.isEmpty()){
            delete eventList;
        }
    }
}



I still get this error: Error Occurred: An Apex error occurred: System.QueryException: List has no rows for assignment to SObject

Is it related to this known issue?  
https://help.salesforce.com/articleView?id=000328824&type=1&mode=1



How can I change the class to not have the error?

Thanks,
Alexandros
This was selected as the best answer
Rounak SharmaRounak Sharma
hi Alexandros,
Can you run your query in query editor.
put a debug statement after 
 Event evnt = [Select Id, WhatId from Event where Id in: eventId LIMIT 1];
system.debug('evnt******'+evnt);
and also
 eventList = [select id From Event where WhatId =: evnt.WhatId and Event_Status__c = 'Not Confirmed'];
system.debug('eventList ******' +eventList +eventList.size() );

Please give the screeshot so that i cna help you in a better way.
Thanks
Alexandros Toulis 6Alexandros Toulis 6
Is this what you mean Rounak?
Sorry I have never worked with Apex Class before.

User-added image
Rounak SharmaRounak Sharma

hello alexandros,
Yes this way only.

You need to check in logs when you perform the process. what you are getting in these
 

Alexandros Toulis 6Alexandros Toulis 6
I manage to resolve it Rounak, thanks a lot for your help! The Apex Class is right, I just had to Set Apex Variables in my Process Builder.
It's not working fine!
 
Rounak SharmaRounak Sharma
hello alexandros,
Please mark it as solved as it will still be in unsolved thread and you will be getting unwanted mails .
Thanks