+ Start a Discussion
SFWesSFWes 

Trigger Case Update Error

Hi All. I'm having an issue when trying to run this trigger. I believe it's because the record is locked.

 

This is all based off of an email-to-case process. When the Case is created, the system should link the appropriate contact to the Case. However, if the contact is not in ur system, only the Supplied fields are populated. What I want to do is, create a new Contact, Add it to a generic Account and then insert it into the newly created Case. But I'm having issues. My code is below.

 

trigger NewContactAdd on Case (after insert) {

        Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
        Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
        List<Case> cases = new List<Case>();
        Contact webCont = null;
        
        for(Case cs: Trigger.new){
			cases.add(cs);
            if(cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)'){

                if(cs.Contact == Null){
                    webCont = new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                   ); 
                   insert webCont;
                }
            }
        }
    
        for(Integer i=0; i < cases.size(); i++){
            Case c = cases.get(i);
        	c.ContactId = webCont.Id;
        }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

well then you have to modify it a lil

trigger NewContactAdd on Case(after insert) {


    Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
    Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
    List<Contact> webCont = new List<Contact>();
    List<Case> cases = new List<Case>();
    for (Case cs: Trigger.new) {
       
        if (cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)') {

            if (cs.Contact == Null) {
                webCont.add(new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                ));
                cases.add(new case(id = cs.Id));
            }
        }
    }

    for (Integer i = 0; i < cases.size(); i++) {
        cases[i].ContactId = webCont[i].Id;
    }
update cases;
}

 

 

Problem was : You cant use the refrerence of trigger.new in after call to do the update. 

 

 

All Answers

Avidev9Avidev9
What is the exact error ?
Avidev9Avidev9

Ahh got the problem!

in after trigger the Trigger.New is readonly and you are trying to assign a value to it

 

so some minor changes, try this

 

trigger NewContactAdd on Case(before insert) {


    Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
    Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
    List<Contact> webCont = new List<Contact>();
    List<Case> cases = new List<Case>();
    for (Case cs: Trigger.new) {
       
        if (cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)') {

            if (cs.Contact == Null) {
                webCont.add(new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                ));
                cases.add(cs);
            }
        }
    }

    for (Integer i = 0; i < cases.size(); i++) {
        cases[i].ContactId = webCont[i].Id;
    }

}

 Some pointers

  • You dont have any check if the contact already exists it can create dupe contacts
SFWesSFWes

Avid - The reason that I wasnt checking for a dup contact is because I figured if the Contact field on the Case wasn't populated, that means that Salesforce did not match the SuppliedEmail to a Contact record in Salesforce.

 

Right now, if I have Wes Cooper (wescooper@mydomain.com) as a Contact under the ACME Account, then when a new email comes with the same email address, it is attached to the Case as the actual Contact. It's because of this, I thought it would be best to use the "After Insert" since I want to see if a Contact was matched, and added to the Case. 

 

Thanks for your fixes. I'll test them out and let you know how it goes.

Avidev9Avidev9

well then you have to modify it a lil

trigger NewContactAdd on Case(after insert) {


    Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
    Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
    List<Contact> webCont = new List<Contact>();
    List<Case> cases = new List<Case>();
    for (Case cs: Trigger.new) {
       
        if (cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)') {

            if (cs.Contact == Null) {
                webCont.add(new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                ));
                cases.add(new case(id = cs.Id));
            }
        }
    }

    for (Integer i = 0; i < cases.size(); i++) {
        cases[i].ContactId = webCont[i].Id;
    }
update cases;
}

 

 

Problem was : You cant use the refrerence of trigger.new in after call to do the update. 

 

 

This was selected as the best answer
SFWesSFWes

Avid - This is a huge help. I had to make one minor Adjustment, though. Since the New Contact was only being created, but not inserted, it was never actually attaching to an Account or Case. I just changed some of the code to Insert it and everything works properly.

 

I do, however, have an additional question. We are using the service console. When a User opens the newly created Case, it will see that the Contact was added to the Generic Account. From there, the Agent can change the Contacts Account from the generic one, to the Account they actually belong to. 

 

The problem with this is that it doesn't change Account field on the actual Case. After changing the Contact Account on the Contact level, is there anyway to automatically update the Case value as well?

 

The only thing I see to do do is put an update trigger on the Contact and update all the Cases tied to it. However, I don't like that since someone might work at Acme Co USA, but then is moved to Acme Co Canada. This is problematic since we lose Data.

 

Do you have any suggestions?

Avidev9Avidev9
I guess trigger is the way to do it!
And you have to put in the logic in the update trigger