0

My code fails to compile with this error:

Compile Error: Incompatible element type Event for collection of CreateEventClass1.eventitems at line 41

Here is my Request Body in Postman:

{
    "eventitems": [{
            "whatid": "0012800000i92pd",
            "whoid": "0032800000TirUN",
            "OwnerId": "005280000010olH",
            "IsAllDayEvent": true,
            "Subject": "call",
            "StartDate": "2016-07-06T14:00:00.000+0000",
            "EndDate": "2016-07-06T15:00:00.000+0000",
            "Recurrence": true,
            "Recurrencestdate": "2016-07-05T00:00:00.000+0000",
            "Recurrenceenddate": "2016-07-06",
            "RecType": "RecursDaily",
            "Recintrvl": 1,
            "Invitees": ["0032800000TiE8N"]
        },
        {
            "whatid": "0012800000KQTwd",
            "whoid": "0032800000MLsdL",
            "OwnerId": "005280000010olH",
            "IsAllDayEvent": true,
            "Subject": "call",
            "StartDate": "2016-07-06T14:00:00.000+0000",
            "EndDate": "2016-07-06T15:00:00.000+0000",
            "Recurrence": true,
            "Recurrencestdate": "2016-07-06T00:00:00.000+0000",
            "Recurrenceenddate": "2016-07-07",
            "RecType": "RecursDaily",
            "Recintrvl": 1,
            "Invitees": ["0032800000MLmNd"]
        }
    ]
}

Here is my REST Class:

@RestResource(urlMapping = '/create Event1/')
global with sharing class CreateEventClass1 {
    public List < Eventitems > eventitems = new list < Eventitems > ();
    @Httppost
    global static void Eventhelper() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        Blob body = req.requestBody;
        string body1 = body.tostring();
        system.debug(body1);
        CreateEventClass1 obj = CreateEventClass1.parse(body1);
        createEvent(obj);
    }
    public static string CreateEvent(CreateEventClass1 cc) {
        List < eventitems > et1 = new list < eventitems > ();
        for (eventitems ei: cc.eventitems) {
            Event ee = new Event();
            ee.OwnerId = ei.Ownerid;
            system.debug(ei.Ownerid);
            ee.StartDateTime = ei.StartDate;
            ee.EndDateTime = ei.EndDate;
            ee.whatId = ei.whatid;
            ee.WhoId = ei.whoid;
            ee.IsAllDayEvent = ei.IsAllDayEvent;
            ee.Subject = ei.Subject;
            ee.IsRecurrence = ei.Recurrence;
            ee.RecurrenceDayOfMonth = ei.RDayofMonth;
            ee.RecurrenceDayOfWeekMask = ei.RDayofWeekMask;
            ee.RecurrenceDayOfMonth = ei.RDayofMonth;
            ee.RecurrenceEndDateOnly = ei.Recurrenceenddate;
            ee.RecurrenceInstance = ei.ReccurenceInstance;
            ee.RecurrenceMonthOfYear = ei.RecurrenceMonthYear;
            ee.RecurrenceStartDateTime = ei.Recurrencestdate;
            ee.RecurrenceType = ei.RecType;
            ee.RecurrenceInterval = ei.Recintrvl;
            et1.add(ee);
            if (ei.Invitees.size() != 0) {
                for (id contid: ei.Invitees) {
                    EventRelation er = new EventRelation();
                    er.RelationId = contid;
                    er.EventId = ee.id;
                    insert er;
                    et1.add(er);
                }
            }
        }
        insert et1;
        return et1.id;
    }
    global Class eventitems {
        public id whatid;
        public id whoid;
        public id OwnerId;
        public boolean IsAllDayEvent;
        public string Subject;
        public DateTime StartDate;
        public DateTime EndDate;
        public boolean Recurrence;
        public datetime Recurrencestdate;
        public date Recurrenceenddate;
        public String RecType;
        public Integer Recintrvl;
        public Integer RDayofWeekMask;
        public Integer RDayofMonth;
        public string ReccurenceInstance;
        public string RecurrenceMonthYear;
        public list < string > Invitees;
    }
    global static CreateEventClass1 parse(String json) {
        CreateEventClass1 n = (CreateEventClass1) system.JSON.deserialize(json, CreateEventClass1.class);
        system.debug(n);
        return (CreateEventClass1) System.JSON.deserialize(json, CreateEventClass1.class);
    }
}

1 Answer 1

1

I think you're getting confused with your data types.

You have:

List < eventitems > et1 = new list < eventitems > ();

Shortly followed by:

Event ee = new Event();

And then

et1.add(ee);

(which I guess is your line 41, because you did not tell us what line 41 was). You're inserting ee (which is an Event) into a et1 (which is a list of eventitems). That's why the insert is failing.

Perhaps what you need is to declare et1 as a List <Event>?

I have a couple of observations that might help you:

  • Your code is a bit unclear and difficult to follow (long methods, a class name ending in '1'...), which makes it easier for you to get into those situations. I would try to refactor it and create a new method that will return a new Event from an eventitem, and then have your loop inside CreateEvent() call that method.
  • Also, if what you're doing is creating Events, do you really need a custom REST service? Have you checked whether you can use the out-of-the-box REST API?

You must log in to answer this question.