You need to sign in to do that
Don't have an account?
WesCooper
Trigger - Create a Case Issue
Hey guys - I am having an issue and I just cannot figure it out. My trigger is made to create a new case and set certain fields of the new Case object. I am able to set all these fields, except for one, the Account field.
The Account field is a lookup field and for some reason, I am unable to set it. I have provided my code below. Any help would be greatly appreciated.
trigger ProgDetailTriggerCase on BLND_DFDT_Project__c (after insert) { AssignmentRule aRule = new AssignmentRule(); aRule = [SELECT id FROM AssignmentRule WHERE SobjectType = 'Case' AND Active = true limit 1]; Database.DMLOptions dmlOpts = new Database.DMLOptions(); dmlOpts.assignmentRuleHeader.assignmentRuleId = aRule.id; Id rtID = [select Id, name from RecordType where name = 'Implementation' and SObjectType = 'Case' limit 1].Id; Set<Id> accId = new Set<Id>(); for (BLND_DFDT_Project__c pdt : Trigger.new){ accId.add(pdt.BLND_Account_Link__c); } Map<Id, Account> accMaps = new Map<Id, Account>([SELECT Id, parentid FROM Account WHERE Id IN :accID]); for (BLND_DFDT_Project__c pdt : Trigger.new){ Case newpdCase = new Case( Account = accMaps.get(pdt.BLND_Account_Link__c).parentid, Subject = 'New Program Detail Created', Priority = 'Medium', Status = 'New', Reason = 'Implementation', Description = 'Testing the trigger', RecordTypeId = rtID, Program_Detail__c = pdt.Id, Opportunity__c = pdt.BLND_Opportunity_Link__c ); newpdCase.setOptions(dmlOpts); insert newpdcase; system.debug(newpdCase.Account); } }
try this
i think it will work
Case newpdCase = new Case( AccountId= acc.id, Subject = 'New Program Detail Created', Priority = 'Medium', Status = 'New', Reason = 'Implementation', Description = 'Testing the trigger', RecordTypeId = rtID, Program_Detail__c = pdt.Id, Opportunity__c = pdt.BLND_Opportunity_Link__c );
All Answers
hi
i think you are getting parentid is null that's y the account id is not inserting in the Case.
try like this
JD - Thank you for the feed back. I should have mentioned that this line of code, actually throws an error.
Throws:
Description Resource Path Location Type
Save error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) ProgDetailTriggerCase.trigger /SF/src/triggers line 21 Force.com save problem
Initially, before I tried setting it by parent Id, i was just using:
and
I'm not sure why I'm getting an error. Thanks again for the help.
Hi
have you seen the accid is coming with values or not you can check in debuglog or developer console
or you can see in visualforce page with get methods.
Please let me know if have any issues
Whenever you are using a map, you have to know that there is a value in the map that you can use index by that value you are using. Since there are very few things in this world that we really know, you should try something like this:
I can't tell without running this through the debugger what's missing - but something is, so try setting up debug() lines and step through the code.
Good luck!
Guys - Thanks for all the assistance.
I've updated the code to the following and put some Debug lines in there to see if values are coming across for the Account obj.
This doesn't toss an error, but it also doesn't set the Account either.
The two debug lines return the following:
08:48:19:368 USER_DEBUG [24]|DEBUG|Acc. ID: 001c0000004g6sxAAA
08:48:19:368 USER_DEBUG [25]|DEBUG|Parent ID: null
It seems that there is an ID for the Account Obj, but not for the Account ParentID. Could this be the issue?
try this code
yeah you are right.
JD - Thanks for all the assitance.
The update you made I have already tried, but I get the following error:
Description Resource Path Location Type
Save error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) ProgDetailTriggerCase.trigger /SF/src/triggers line 27 Force.com save problem
I cannot set the object by ID, I have to set it using another account object or through a query. This actually confuses me a bit since I can set other objects by Id, but not the Account object.
Thanks again,
Wes Cooper
try with hardcoding like
Account = '001c0000004g6sx' or any other Account id
Check out is it working or not
Jd, the compiler throws an error when trying your suggestion:
I also tried doing it this way:
After this, I updated the code a bit. I removed parentid from the query and added Name. I also added two debug lines to test the values of the Case object.
The Case debug lines return exactly what I need. So they are correctly being set in the case object.
10:44:49:209 USER_DEBUG [41]|DEBUG|Test Studios
10:44:49:209 USER_DEBUG [42]|DEBUG|001c0000004d7UkAAI
Thanks again for all the assistance, guys. It is greatl appreciated.
Wes Cooper
try this
i think it will work
Case newpdCase = new Case( AccountId= acc.id, Subject = 'New Program Detail Created', Priority = 'Medium', Status = 'New', Reason = 'Implementation', Description = 'Testing the trigger', RecordTypeId = rtID, Program_Detail__c = pdt.Id, Opportunity__c = pdt.BLND_Opportunity_Link__c );
Wow!!! Jd, yes it worked, and after all that, I just want to give myself a FACEPALM, but I think it would be better suited for me to just laugh a bit and slam my head on the keyboard a bit.
Thank you for all the assitance.
Wes Cooper