I have created email service and its activate and anyone can send email. When i'm sending email from gmail into salesforce it showing error in debug log that

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Assigned To ID: id value of incorrect type: 0039E000002PRSZQA4: [OwnerId]

how to create task when email match to existing email of contact

global class ContactFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

        String con_Email = envelope.fromAddress;


        List<Contact> con = [SELECT Id, Email FROM contact WHERE email =: con_EMail LIMIT 1];

        if(con[0].email == con_Email) {
            Task T = new Task();
            T.Type = 'Email';
            T.OwnerId = con[0].id; //user id
            //T.WhatId = ; //record id
            insert T;

        result.success = true;
        return result;

1 Answer 1


The T.OwnerId field must be a User Id. In your code, you are assigning a Contact Id.

See here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm

OwnerId: ID of the User who owns the record. Label is Assigned To ID.

WhoId: The WhoId represents a human such as a lead or a contact. WhoIds are polymorphic. Polymorphic means a WhoId is equivalent to a contact’s ID or a lead’s ID. The label is Name ID.

WhatId: The WhatId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. WhatIds are polymorphic. Polymorphic means a WhatId is equivalent to the ID of a related object. The label is Related To ID.

So your code could be something like this:

Task T = new Task();
T.Type = 'Email';
T.OwnerId = assignedUser.Id; // user id
T.WhoId = con[0].id; // contact id
//T.WhatId = relatedTo.Id; // related record id
insert T;

You must log in to answer this question.

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