function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yaris GutierrezYaris Gutierrez 

APEX Trigger Compile Error Using SalesForce-Provided Code

Hi Everyone,

I'm new to APEX triggers and am trying to use the following guide, https://help.salesforce.com/articleView?id=entitlements_auto_add.htm&type=0, to automatically add entitlements to new cases when created from web mail, email-to-case, and communities. However, when attempting to "Save" the code copied and pasted from the page, I am presented with the error Error: Compile Error: unexpected token: } at line 43 column 0.

I was under the impression that this was due to there being an extre "}" in the code block, so I removed this and received Error: Compile Error: Variable does not exist: contactIds at line 5 column 39
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
    List <EntitlementContact> entlContacts = 
                [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId 
                From EntitlementContact e
                Where e.ContactId in :contactIds
                And e.Entitlement.EndDate >= Today 
                And e.Entitlement.StartDate <= Today];
        if(entlContacts.isEmpty()==false){
            for(Case c : Trigger.new){
                if(c.EntitlementId == null && c.ContactId != null){
                    for(EntitlementContact ec:entlContacts){
                        if(ec.ContactId==c.ContactId){
                            c.EntitlementId = ec.EntitlementId;
                            if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                                c.AssetId=ec.Entitlement.AssetId;
                            break;
                        }
                    } 
                }
            } 
        } else{
            List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, 
                    e.AccountId, e.AssetId
                    From Entitlement e
                    Where e.AccountId in :acctIds And e.EndDate >= Today 
                    And e.StartDate <= Today];
            if(entls.isEmpty()==false){
                for(Case c : Trigger.new){
                    if(c.EntitlementId == null && c.AccountId != null){
                        for(Entitlement e:entls){
                            if(e.AccountId==c.AccountId){
                                c.EntitlementId = e.Id;
                                if(c.AssetId==null && e.AssetId!=null)
                                    c.AssetId=e.AssetId;
                                break;
                            }
                        } 
                    }
                } 
            }
        }
    } 
}
Thanks in advance, everyone. 
 
Best Answer chosen by Yaris Gutierrez
JeffreyStevensJeffreyStevens
Ya - you'll have the same problem on line # 25 also with accids.  So, when you do the :contactIds - that tells us that you should have a list or set called contactIds.  So, I'd guess that the code in the help acticle is not complete.  I'm not 100% sure - but - - becuase the trigger is on the case - most likely - you need to loop through all of the Cases that fired this trigger and get the list of Contact and account IDs.  So, Maybe add code like this just before line#2...
set<id> contactIds = new set<id>();
set<id> acctIds = new set<id>();
For(Case c :trigger.new) {
    if(c.ContactId != null) {
        contactIds.add(c.ContactId);
    }
    if(c.AccountId != null) {
        acctIds.add(c.AccountId);
    }
}

 

All Answers

JeffreyStevensJeffreyStevens
I think you're missinbg an open bracket "{" on ilne #33 on the if (c.AssetId==...) statement.
Yaris GutierrezYaris Gutierrez
Thanks for your quick response, Jeffrey! Much appreciated.

Adding that brings up the following Error: Compile Error: Variable does not exist: contactIds at line 5 column 39 (as described above).
JeffreyStevensJeffreyStevens
Ya - you'll have the same problem on line # 25 also with accids.  So, when you do the :contactIds - that tells us that you should have a list or set called contactIds.  So, I'd guess that the code in the help acticle is not complete.  I'm not 100% sure - but - - becuase the trigger is on the case - most likely - you need to loop through all of the Cases that fired this trigger and get the list of Contact and account IDs.  So, Maybe add code like this just before line#2...
set<id> contactIds = new set<id>();
set<id> acctIds = new set<id>();
For(Case c :trigger.new) {
    if(c.ContactId != null) {
        contactIds.add(c.ContactId);
    }
    if(c.AccountId != null) {
        acctIds.add(c.AccountId);
    }
}

 
This was selected as the best answer
Yaris GutierrezYaris Gutierrez
Thanks so much for this, Jeffrey. That fixed the issue overall. It may be a good idea for Salesforce to actually update their documents to reflect these changes as their code does not work as is. 

Lastly, and I earnestly appreciate your assistance, are triggers supposed to fire off automatically once criteria is met? I only ask because I am attempting to test this and not getting the results expected.

I'm accepting your answer and truly want to thank you for your quick responses.
JeffreyStevensJeffreyStevens
You welcome.

Well - this is a BEFORE INSERT and BEFORE UPDATE trigger.  So, the trigger will get called every time a Case is inserted or updated.

Be sure to mark the question as answered also.

 
Yaris GutierrezYaris Gutierrez
Right. So when I'm creating a case using email-to-case, the expected result, if I understand it correctly, is that when a case is created (inserted, I assume this means), the trigger should fire and assign the entitlement to the case in question given the account or contact has an entitlement to begin with, correct?

If so, this trigger is not firing upon case creation. Maybe I'm missing something... 

Your help is valued, Jeffrey! 
JeffreyStevensJeffreyStevens
I would that that the trigger is being fired.  You're still in sandbox - right?  Is the email to Case happening in production?  Also - if you know how to check for user logs - you could add a debug statement system.debug('Case triggered fired...');  - or something like that to verify that it's running.
Yaris GutierrezYaris Gutierrez
I am still in sandbox, yes. To answer your question, email-to-case is happening in sandbox at the moment (e.g. I send an email to the email-to-case address in sandbox, case gets created) -- this has not been pushed to production just yet :)

I don't know how to check for user logs, but I am sure I can figure it out. I'm assuming it's adding system.debug('Case triggered fired...'); in the code above (maybe before line 43?). If so, I'll do that now and see what's happening. But yes, I believe that based on your comment, the trigger should be getting fired, unless there's something else in the code that's not working as intended. 
JeffreyStevensJeffreyStevens
Ok good.  Ya - add the debug statement after line #1 - that way you know for sure that the trigger is getting called.  After you verfiy that - you can add more debug's to find out why entitlements are not being created.
Yaris GutierrezYaris Gutierrez
Hey Jeffrey -- haven't been able to pinpoint the issue specifically as I'm not seeing any errors. Updating the case, the entitlement gets inserted just fine without any issues. However, creating one (e.g. insert) and nothing happens. I'll continue looking into this, but if you have any insight, I'd greatly appreciate it.
JeffreyStevensJeffreyStevens
Check to see if you're getting anything back from the SOQL's (after old line# 7, and #26) with a debug statement like - - 

system.debug('entlContacts='+entlContacts);
and
system.debug('entls='+entls);

I would bet tht one of those isn't returning anything - that's probably why it's not happening on insert. 

It also might be that the contactIds and AccountIds are empty on insert (I don't remember the exact timing of those getting populated on email to case) - so you might do debug's to verify they are getting populated on inserts.  If they are not - then that help article has more issues.  In that case - you'd have quite a bit more work.