Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Apex Trigger Creates New Case but won't populate C...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Apex Trigger Creates New Case but won't populate Contact field
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 02:15 PM
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.Pr { //here Opportunity__c is lookup to Oppor listCase.add(new Case(Opportunity__c = o Subject = o.Name, Origin = 'Email-Renewals', Priority = 'Medium', Status = 'New')); } } if(listCase.size() > 0) { insert listCase; } } |
Solved! Go to Solution.
Re: Apex Trigger Creates New Case but won't populate Contact field
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 10:39 PM
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;
}
}
Re: Apex Trigger Creates New Case but won't populate Contact field
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-03-2013 08:51 AM
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.
Re: Apex Trigger Creates New Case but won't populate Contact field
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-03-2013 08:03 PM
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;
}
}
Re: Apex Trigger Creates New Case but won't populate Contact field
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 05:28 AM
It works!!! Thanks! I really appreciate the help.

