0

I'm new to Apex and need a help regarding Triggers. I need to update a field(field__c) in custom object (obj1__c) (field of type checkbox) whenever a new record is created in case using email-to-case functionality. Please help me with the trigger Here is the trigger I have written. I didn't get any errors but the code is not working.

trigger CaseAfterInsert on Case (after insert) {
    String contactID;
    set<ID> obj1Id = new set<ID>();
    list<obj1__c> test = new list<obj1__c>();
    for(Case newCases : Trigger.new)
        {
            if(newCases.Subject.equalsIgnorecase('example'))
            {
               test.add(obj1__c);

            }
        }
    test = [Select field__c from obj1__c where Contact__c =: contactID];
    for(obj1__c test1 : test)
    {
    test1.field__c = false;
    System.debug('list test --------------------'+test);
            try {
                   update test1;
                   } 
              catch (Exception e)
              {
              System.debug('Exception occurred--------------'+e);
             }
    }
}

1 Answer 1

0

You're using ContactID to query obj1 objects but you are not giving it any value in your trigger. Because of this ContactID is null and probably your query is not returning any records.

Also you are using DML operation inside a for loop which is not recommended. Below code should work.

  trigger CaseAfterInsert on Case (after insert) {
//get new cases into a list
List<Case> newCases = Trigger.new;
//for contact ids to query obj1 records
set<ID> caseIds = new set<ID>();
//to keep obj1 records
list<obj1__c> test = new list<obj1__c>();
List<EmailMessage> = new List<EmailMessage>();
for(Case nc : newCases)
{
    caseIds.add(nc.Id);
}
//parentId is the id of Case related to the email message
messages = [SELECT Id, Subject, ToAddress, ParentId FROM EmailMessage WHERE ParentId IN :caseIds];

if(messages.size() > 0){
    //add ToAddress int a Set and query obj1 according to this set using IN
    for(Integer i = 0; i < messages.size(); i++)        {
        obj1Id.add(messages[i].ToAddress);
    }
    test = [Select field__c from obj1__c where Contact__c IN :obj1Id];
    for(Integer i = 0; i < test.size(); i++){
        test[i].field__c = false;
    }

    System.debug('list test --------------------'+test);
    //update is outside of for loop
    try {
        update test;
    } 
    catch (Exception e)
    {
        System.debug('Exception occurred--------------'+e);
    }

}

}

UPDATE: To address and Subject of email is not on the Case object. It belongs to EmailMessage object. See updated code above.

More info on EmailMessage class; EmailMessage

7
  • Thanks Sirin, My requirement is when a new case record is created using Email-case functionality then depending on the To address (SFDC Routing email services address) and the subject of email I need to update the field in a custom object. Actually in the place of obj1Id I need to use the ID of those records matching with the case. can you please help me with this. Thanks
    – abcd 1807
    Commented Aug 24, 2015 at 10:32
  • Also, can you please help me in capturing the 'To' email address from the case record created as I do not find any such field in Case object Thanks
    – abcd 1807
    Commented Aug 24, 2015 at 10:39
  • Please refer to updated answer.
    – tugce
    Commented Aug 24, 2015 at 11:03
  • Thanks Sirin, Can you please explain me "//add ToAddress int a Set and query obj1 according to this set using IN" this line? can I hard code the "TO" Address? obj1 query = [select field__c from obj1__c where ID=:ToAddress]; Is this correct? If yes, how is the cases mapping to the records of the obj1? Thanks in advance
    – abcd 1807
    Commented Aug 24, 2015 at 11:42
  • I dont understanda what you mean by hard-code. (I know what the term hard code means)
    – tugce
    Commented Aug 24, 2015 at 12:06

You must log in to answer this question.

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