You need to sign in to do that
Don't have an account?
Newbie needs help with custom settings reference in Apex trigger
I have a trigger that needs to use a specific account id and a specific user id. Instead of hardwiring the IDs in the trigger I want to store the values in a custom setting and reference them in the trigger.
I created a list custom setting CS_Constants and a field called SF_ID. Then I added two data sets to the list.
Name: CSAccount
SF_ID: 001J000000FpzyZIAR
Name: CSSupervisor
SF_ID: 005A0000001BBFuIAO
I am having trouble using the values in the trigger.
Here is what I tried in a method call to create contact:
...
OwnerID=CS_Constants__c.getInstance('CSAccount'),
...
and I get this error:
Error: Compile Error: Invalid initial expression type for field Contact.OwnerId, expecting: Id at line 38 column 53
How do I make this work the way I need?
Thanks,
Beth
This threw me initially too the first time I worked with Custom Settings
ID ownerId = CS_Constants__c.getInstance('CSAccount').SF_ID__c;
Best practice would be to do it this way though:
This way you protect yourself from someone deleting the custom setting that you need
In addition, in your test method, you either need to insert this custom setting or, use @isTest(SeeAllData=true) as Custom Settings are not inherently visible to testmethods at V24.0 or higher
All Answers
This threw me initially too the first time I worked with Custom Settings
ID ownerId = CS_Constants__c.getInstance('CSAccount').SF_ID__c;
Best practice would be to do it this way though:
This way you protect yourself from someone deleting the custom setting that you need
In addition, in your test method, you either need to insert this custom setting or, use @isTest(SeeAllData=true) as Custom Settings are not inherently visible to testmethods at V24.0 or higher
Custom Setting would always return you a string object and you are trying to typecase a string into an Id.
You need to explicitly typecast to an Id or store it in a string and later set it as Id on update.
Invalid initial expression type for field Contact.OwnerId, expecting: Id at line 38 column 53
thanks Sammy - I corrected the code above
It compiles now, but doesn't actually work. I am not running it through a test method, rather I am sending an email to the sandbox and the trigger is executing as part of email to case. When I send the email, no case gets created.
I'm not clear on where I would put
@isTest(SeeAllData=true)
in a trigger,
or insert the custom settings into the trigger, if that is the issue.
Thanks,
Beth
Beth --
I think I may be confusing you
1. the reference to @isTest applies to yout testmethod which you will eventually have to write in orfer to migrate your trigger (+ testclass) to production
2. I infer from your remark on flow that the following is your situation
* EmailToCase
* Case trigger fires
* case trigger looks up custom setting and assigns ownerid to Case
* Trigger ends
* Case is created
You say Case is not created -- there could be lots of reasons for this and you have to look at the emailToCase diagnostics to see what is going on. Alternatively (and better), you can simulate a new Case being inserted by using either Execute Anonymous Apex or write a test method. In either case, you'll do something simple like:
insert new Case(fldA = valx, fldB = valy, ....); and then you can get more diagnostics from the debug log
Hi Eric,
Thank you for the additional information. I got it to work last night - I had transcribed the dataset variable names in the assignment statement so it saved but didn't work.
Thanks again for all your help and the code snippet!
Beth