You need to sign in to do that
Don't have an account?
Apex Trigger Creates New Case but won't populate Contact field
Hi, I am just learning how to read/write Apex, so I'm sure this is a really basic problem, but I would really appreciate any help!
I created a trigger that will create a new Case record when an Opportunity meets the following criteria (Type = 'Maintenance Renewal' & Probability = 100). I'm running into two issues with the below code:
1) the trigger is working, except it is creating 2 identical Case records for each Opp which meets the above criteria
2) I can't figure out how to populate the 2 necessary lookup fields on the Case: Owner (which needs to be the Maintenance Renewal Queue) & Contact (which needs to be the Opportunity Owner)
Thank you in advance to anyone who can help!!
Here is the trigger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | trigger createNewCase on Opportunity (after update) { List<Case> listCase = new List<Case>(); for(Opportunity o : trigger.new) { if(o.Type == 'Maintenance Renewal' && o.Probability == 100) { //here Opportunity__c is lookup to Opportunity record for which new record is created listCase.add(new Case(Opportunity__c = o.id, Subject = o.Name, Origin = 'Email-Renewals', Priority = 'Medium', Status = 'New')); } } if(listCase.size() > 0) { insert listCase; } } |
Hi
I changed small thing in below code. Try this
trigger createNewCase on Opportunity (after update) {
List<Case> listcase = new List<Case>();
for(Opportunity o:trigger.new) {
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.probability != oldopp.probability)
{
if((o.Type == 'Maintenance Renewal' && o.Probability == 100) {
//here Opportunity__c is lookup to Opportunity record for which new record is created
listCase.add(new Case(Opportunity__c = o.id,
OwnerID = ' Queue Id' // you can hard code value or write query and get the Id
Contactid = o.OwnerId;
Subject = o.Name,
Origin = 'Email-Renewals',
Priority = 'Medium',
Status = 'New'));
}
}
}
if(listcase.size()>0) {
insert listcase;
}
}
All Answers
Hi
it creating cases when ever met your criteria. it will create based on how many times you update the record (condition :met criteria).
you need to restrict this need to use OldMap of trigger.
trigger createNewCase on Opportunity (after update) {
List<Case> listcase = new List<Case>();
for(Opportunity o:trigger.new) {
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.type != oldopp.type && o.probability != oldopp.probability)
{
if((o.Type == 'Maintenance Renewal' && o.Probability == 100) {
//here Opportunity__c is lookup to Opportunity record for which new record is created
listCase.add(new Case(Opportunity__c = o.id,
OwnerID = ' Queue Id' // you can hard code value or write query and get the Id
Contactid = o.OwnerId;
Subject = o.Name,
Origin = 'Email-Renewals',
Priority = 'Medium',
Status = 'New'));
}
}
}
if(listcase.size()>0) {
insert listcase;
}
}
Hi sivaext, thanks for the help!! It's now creating only 1 Case as it should, but something else strange is now happening.
The Case is only created when I change BOTH fields at the same time (Type & Probability). However, this will never actually happen in our business plan. The Type field will always be Maintenance Renewal, it is the Probability which will change as the Opp moves through the stages of the sales process. So, we need the trigger to fire when both conditions are true, but only the Probability field will actually be changing.
Is it possible to add this to the trigger? I've been doing trial/error all morning and can't get it right.
Hi
I changed small thing in below code. Try this
trigger createNewCase on Opportunity (after update) {
List<Case> listcase = new List<Case>();
for(Opportunity o:trigger.new) {
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.probability != oldopp.probability)
{
if((o.Type == 'Maintenance Renewal' && o.Probability == 100) {
//here Opportunity__c is lookup to Opportunity record for which new record is created
listCase.add(new Case(Opportunity__c = o.id,
OwnerID = ' Queue Id' // you can hard code value or write query and get the Id
Contactid = o.OwnerId;
Subject = o.Name,
Origin = 'Email-Renewals',
Priority = 'Medium',
Status = 'New'));
}
}
}
if(listcase.size()>0) {
insert listcase;
}
}
It works!!! Thanks! I really appreciate the help.