You need to sign in to do that
Don't have an account?
Thomas Caillet
Create multiple case for each opportunity product when opportunity is won (trigger)
Hi everybody,
I'm trying to make a trigger that create one case for each opportunity product when opportunity is won.
I know how to create case but I don't know how to browse all opportunity products.
I'm new on apex so I need your help please.
Thanks to you
I'm trying to make a trigger that create one case for each opportunity product when opportunity is won.
I know how to create case but I don't know how to browse all opportunity products.
I'm new on apex so I need your help please.
Thanks to you
Yes, please try with below code, I tried it on my org and it works:
trigger OpportuntiyTrigger_AT on Opportunity(after insert, after update) {
if ((Trigger.isAfter && Trigger.isInsert) || (Trigger.isAfter && Trigger.isUpdate)) {
List < id > ListOpportunityLineItem = new List < id > ();
Map < Id, Opportunity > opptyMap = new Map < id, Opportunity > ();
for (Opportunity o: Trigger.new) {
if (o.StageName == 'Closed won')
ListOpportunityLineItem.add(o.id);
opptyMap.put(o.id, o);
}
List < OpportunityLineItem > opptyLineItemList = new List < OpportunityLineItem > ([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN: ListOpportunityLineItem]);
List <
case >caseListToInsert = new List < Case > ();
for (OpportunityLineItem o: opptyLineItemList) {
Case c = new Case(Subject = O.name, Description = o.description, Origin = 'web', AccountId = opptyMap.get(o.OpportunityId).Accountid);
caseListToInsert.add(c);
}
if (caseListToInsert != null && caseListToInsert.size() > 0)
insert caseListToInsert;
}
}
Thanks,
Gaurav
All Answers
In SFDC Opportunity Product is named as Opportunity Line Item, please find below code to create new case:
trigger OpportuntiyTriggerHandler on Opportunity (after insert, after update){
set<id> ListOpportunityLineItem = new set<id>();
for(Opportunity o: trigger.new){
if(o.StageName == 'won')
ListOpportunityLineItem.add(o.id);
}
List<OpportunityLineItem> opptyLineItemList = new List<OpportunityLineItem>([select id, name, Description from opportunityLineItem where OpportunityId IN :ListOpportunityLineItem]);
List<case> caseListToInsert = new List<Case>();
for(OpportunityLineItem opl: opptyLineItemList){
Case c = new Case(description = opl.description, status = opl.name);
caseListToInsert.add(c);
}
if(caseListToInsert != null && caseListToInsert.size > 0)
insert caseListToInsert;
}
Please let me know if you still face any issues, or contact me on email: gauravgarg.nmims@gmail.com, skype: gaurav62990
Thanks,
Gaurav
I didn't test your code because you create cases according to OLI and me I'd like to do according to opportunity with OLI information.
I find something that works. See the code below :
trigger createCase on Opportunity (after update)
{
if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
{
List<Case> ll=new List<Case>();
for(OpportunityLineItem la : [SELECT PricebookEntry.Product2.Name from OpportunityLineItem where opportunityId =: Trigger.new])
{
String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,OwnerId='XXX', AccountId = trigger.new[0].AccountId);
ll.add(c1);
}
insert ll;
}
}
Hi Thomas,
Above correct code is correct, if you want to use Standard Product and Pricebook object, but Opportunity Product are referred as OpportunityLineItem.
Also, above code will need to update as per your requirement. This code will only work for single (first) record.
Thanks,
Gaurav
You're sure because I tested it and it works for all OLI and it create as many cases as OLI
In case of MassUpdate it will fail. If you manually update the Opportunity then the trigger will work as expected.
Thanks,
Gaurav
Thanks
Please replace below line:
Case c = new Case(description = opl.description, status = opl.name); // Replace this line
Case c = new Case(description = opl.description, subject = opl.name);
Thanks,
Gaurav
Case c = new Case(AccountId = o.AccountId);
Doesn't work :/
Thomas,
For creating new case, Origin is a requried standard field. Please look below Code.
Case c = new Case(Subject = O.name, Description = o.description, Origin = web, AccountId = O.Accountid);
insert c;
I have tried this in my org. It works.
let me know if you still face some issue, or contact me on: email: gauravgarg.nmim@gmail.com, skype: gaurav62990
Thanks,
Gaurav
Thomas
Thomas,
can you please post the updated code, you are trying to insert.
Thanks,
Gaurav
trigger OpportuntiyTriggerHandler on Opportunity (after insert, after update){
set<id> ListOpportunityLineItem = new set<id>();
for(Opportunity o: trigger.new){
if(o.StageName == 'Closed won')
ListOpportunityLineItem.add(o.id);
}
List<OpportunityLineItem> opptyLineItemList = new List<OpportunityLineItem>([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN :ListOpportunityLineItem]);
List<case> caseListToInsert = new List<Case>();
for(OpportunityLineItem opl: opptyLineItemList){
Case c = new Case(Subject = O.name, Description = o.description, Origin = web, AccountId = O.Accountid);
insert c;
}
if(caseListToInsert != null && caseListToInsert.size() > 0)
insert caseListToInsert;
}
Yes, please try with below code, I tried it on my org and it works:
trigger OpportuntiyTrigger_AT on Opportunity(after insert, after update) {
if ((Trigger.isAfter && Trigger.isInsert) || (Trigger.isAfter && Trigger.isUpdate)) {
List < id > ListOpportunityLineItem = new List < id > ();
Map < Id, Opportunity > opptyMap = new Map < id, Opportunity > ();
for (Opportunity o: Trigger.new) {
if (o.StageName == 'Closed won')
ListOpportunityLineItem.add(o.id);
opptyMap.put(o.id, o);
}
List < OpportunityLineItem > opptyLineItemList = new List < OpportunityLineItem > ([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN: ListOpportunityLineItem]);
List <
case >caseListToInsert = new List < Case > ();
for (OpportunityLineItem o: opptyLineItemList) {
Case c = new Case(Subject = O.name, Description = o.description, Origin = 'web', AccountId = opptyMap.get(o.OpportunityId).Accountid);
caseListToInsert.add(c);
}
if (caseListToInsert != null && caseListToInsert.size() > 0)
insert caseListToInsert;
}
}
Thanks,
Gaurav
Have a nice day :)
Thomas
Have a nice day you too.