+ Start a Discussion
Jag SinghJag Singh 

Case Trigger to populate Lookup for Entitlements

Hi,
 
I'm building a trigger for the Case object... whereby every time a Case is created I need the Entitlement Lookup field to automatically populate to 'Default Entitlement'. I've written this code below but comes up with an error message:

1. trigger CheckFieldValues on Case (before insert) {
2.    for (Case c : Trigger.new) {
3.        if (c.Status == 'New')
4.            c.Entitlement = 'Default Entitlement';          
5.        }
6.    }

ERROR MESSAGE: Error: Compile Error: Illegal assignment from String to SOBJECT:Entitlement at line 4 column 13

Not an expert on Triggers so any help would be good!

Thanks,
J
Best Answer chosen by Jag Singh
Vatsal KothariVatsal Kothari
Hi,

You can use below code:
trigger CheckFieldValues on Case (before insert) {

List<Entitlement> entList = [Select Id,Name from Entitlement where Name = 'Default Entitlement' limit 1];

for (Case c : Trigger.new) {
    if (c.Status == 'New')
        c.EntitlementId = entList[0].Id;
    }
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi,

You can use below code:
trigger CheckFieldValues on Case (before insert) {

List<Entitlement> entList = [Select Id,Name from Entitlement where Name = 'Default Entitlement' limit 1];

for (Case c : Trigger.new) {
    if (c.Status == 'New')
        c.EntitlementId = entList[0].Id;
    }
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
Jag SinghJag Singh
Thanks a lot Vatsal! :)