0

I want to add all the open activities from an opportunity to another opportunity. From my understanding, open activity is split into event and task. This is what I have so far :

List<Opportunity> rec = [Select Id, (Select ActivityDate,Description,
                        Subject FROM OpenActivities) FROM Opportunity
                        WHERE id='123456789'];

for(Opportunity a : rec) {
    System.debug(a.OpenActivities);
}

This is printing out the fields that I've specified (ActivityDate,Description,Subject) but since Open Activities can be either an event or task, so I'm unsure how I would check for that. I think I've found a field of the name ActivitySubtype but I guess what my issue is, even in this example, I'm not able to access only the Description of OpenActivities. Not really sure how to do that...

And I really have no idea how to insert an Open Activity event/task into an opportunity. I'm reading around for it right now but it would help to get some possible advice/guidance on that too

1 Answer 1

1

You can use the OpenActivity documentation to help you out; notably there's a field called "IsTask" that tells you if you're working with a task or event. You could pull the descriptions like this:

for(Opportunity a : rec) {
    for(OpenActivity b: a.OpenActivities) {
        System.debug(b.Description);
    }
}

That said, I'd personally rather get the individual objects separately since you intend to be working with them directly:

Opportunity[] rec = [SELECT (SELECT ActivityDate, Description FROM Tasks 
                             WHERE IsClosed = FALSE),
                            (SELECT ActivityDate, Description FROM Events 
                             WHERE ActivityDate >= TODAY)
                     FROM Opportunity WHERE Id = '006000012356155'];

This would probably be easier to work with, because you can't actually insert OpenActivities (it's a "view" of two objects); you have to manipulate the underlying data, so simply querying those records directly is going to be easier.

2
  • Thanks! So I can't use Apex to insert all the open activities to another opportunity. I feel like there has to be way, still looking around tho Aug 18, 2016 at 23:33
  • @user2582622 nothing can insert OpenActivities. It's not a "real" object: you need to insert Task and Event records instead.
    – sfdcfox
    Aug 19, 2016 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .