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
Peter CowenPeter Cowen 

trigger to add entitlement to case

I am trying to build a trigger that adds the entitlement to the case when the account name is populated.

So far I have
trigger defaultEntitlement on Case (Before Insert, Before Update) {
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;

When saving I get a Compile Error: Variable does not exist: acctIds.

Will this trigger assign the entitlement to the case and 2 is there anything else i need to add to the trigger
srlawr uksrlawr uk
To answer your first problem, you are referencing a list called accIds without first setting it up... I'm assuming you copied this code from somewhere else and have probably missed that bit.. it needs to be something like:
 
trigger defaultEntitlement on Case (Before Insert, Before Update) {

List<Id> accIds = new List<Ids>();
for(Case thisCase : Trigger.new) {
    accIds.add(thisCase.AccountId);
}

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;
                            }
                     }
                }
             }
         }
​}
(you were also missing an opening bracket, and I tried to pair them all up for you).

Otherwise, this will do what you are asking ultimately.. it goes through all the cases and copies whatever it find in the entitlement field into the case... (it also copied the AssetId in too).

It's not quite how I might do it, you might even be able to do this with workflow instead to be fair?
 
Eric VitucciEric Vitucci
I have tried this code and I keep getting this error? Error: Compile Error: Expecting '}' but was: '<EOF>' at line 26 column 1