You need to sign in to do that
Don't have an account?
Yaris 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
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.
All Answers
Adding that brings up the following Error: Compile Error: Variable does not exist: contactIds at line 5 column 39 (as described above).
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.
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.
If so, this trigger is not firing upon case creation. Maybe I'm missing something...
Your help is valued, Jeffrey!
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.
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.