0

i'm fetching data from soql need to insert account lookup value(accountId) and contact lookup value (ContactID) in case create from email services

here is my code:

String con_Email = envelope.fromAddress;

    //write soql and checking sender email & existing email of contact same
    List<Contact> conList = [SELECT Id, AccountID, Block_Email__c, Email, (SELECT AccountId, ContactId FROM cases) FROM contact WHERE email =: con_Email LIMIT 1];

    //Checking list size is greater then 0 and not eqaul to null
    if(conList.size()>0 && conList != null){

        //if Block Email checkbox true then email will not get into salesforce
        if(conList[0].Block_Email__c == true){

        }
        // if sender email & existing email of contact same create activity (task) 
        else if(conList[0].email == con_Email.trim()) {
            //Creating case
            Case cs = new Case();
            cs.Subject = email.subject;
            cs.Contact = conList[0].cases.ContactId;
            cs.Account = conList[0].AccountID;
            cs.Origin = 'E-Mail - FMB#1';
            cs.Description = email.plainTextBody;
            insert cs;
1
  • 1
    You definitely have no guarantee there are any cases, and also no guarantee of the ordering. You should add an ORDER BY clause to your inner query, and check if the list is empty as Kevan suggests.
    – Adrian Larson
    Feb 13, 2017 at 9:30

1 Answer 1

2

Change the following lines.

cs.Contact = conList[0].cases.ContactId;
cs.Account = conList[0].AccountID;

to

cs.ContactId = conList[0].Id;
cs.AccountId = conList[0].AccountID;

You will get contact id directly. You don't need to get it from cases list. Since you are using inner query all the cases will be having same contact id.

You must log in to answer this question.

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