You need to sign in to do that
Don't have an account?
Mario Schwarz
APEX: Create a case, for every item in a picklist for the account object.
Hello,
I have a custom field in accounts, a pick list with multiple choice option. For every item that has been chosen, I want to create a related case. Could someone give me a hint? Please not a whole code, I want to do it on my own but I don't know how :D
Thanks a lot for your time
I have a custom field in accounts, a pick list with multiple choice option. For every item that has been chosen, I want to create a related case. Could someone give me a hint? Please not a whole code, I want to do it on my own but I don't know how :D
Thanks a lot for your time
* Use After create/update trigger on Account
* Multi-select picklist values are separated by semi-colon (;), use the same logic
i managed to make it work, but only based on one item in the picklist field. If i choose two or more, i still only get that one case.
My idea is that i´m doing something wrong with the if statements. There has to be one case, for every item selected. How do i do that?
Please consider that i´m relatively new to this and i wrote it by myself so there might be some huge mistakes.
trigger CaseForSelectedItem on Account (after insert, before update) {
Map < Id, Id > existingCases = new Map < Id, Id >();
for (Account acc : Trigger.new){
//Set variable for the null pointer exeption
if (acc.ApexTrigger__c != null){
//Create a String list to pick items from the picklist
List <String> selectedItems = new List<String>();
//.split takes the items and splits them
selectedItems = acc.ApexTrigger__c.split(';');
//Create a List with the Cases to create
List <Case> casesToCreate = new List<Case>();
//Define the items of the String
for (String item : selectedItems) {
Case myCase = new Case();
myCase.subject = item;
myCase.Description = 'The following Items have been chosen: ' + selectedItems;
myCase.AccountId = acc.Id;
if (Trigger.isUpdate){
//Check if there is already a Case with the same Id
if( existingCases.get( acc.Id ) != null ) {
myCase.AccountId = existingCases.get(acc.Id);
}
else {
myCase.AccountId = acc.Id;
//Check if there is already a Case with the same Id
}
}
//Add my Case to the List
casesToCreate.add(myCase);
}
insert casesToCreate;
}
}
}