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
- :
- Re: Trigger that creates new records and utilizes ...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Trigger that creates new records and utilizes the new Ids
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:34 AM
The below trigger creates opportunity records once order__c records meet certain criteria. The order__c table has a field called Opportunity_Name__c that I would like to populate with the opportunity Id that is generated in the trigger. Can someone show me how to do this?
trigger createNewOpportunity on Order__c (after update) {
Id orderRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Order__c' and Name = 'Order'].Id;
Id oppRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = 'CDA'].Id;
Id TTO = [SELECT Id FROM Account WHERE Agreement_Company_Code__c = 'W'].Id;
List<Opportunity> o = new List<Opportunity>();
for (Order__c order: Trigger.New){
if (order.Contact__c <> Null && order.Account__c <> Null && order.RecordTypeId == orderRT && order.Status__c == 'Open'){
getOppNumber controller = new getOppNumber();
string oppNumber = controller.generateOpportunityNumber(TTO);
o.add(new Opportunity(
RecordTypeId = oppRT,
Record_Sub_Type__c = 'Confidential Agreement',
AccountId = order.Account__c,
Opportunity_Number__c = oppNumber,
StageName = 'Initial Contact',
Name = order.Account__r.Name + ' - ' + order.PI_Name__c + ' - ' + oppNumber.removeStart('D')));
}
}
insert o;
}
Re: Trigger that creates new records and utilizes the new Ids
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:45 AM
trigger CreateNewOpportunity on Invoice__c (after update) {
Id orderRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Order__c' and Name = 'Order'].Id;
Id oppRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = 'CDA'].Id;
Id TTO = [SELECT Id FROM Account WHERE Agreement_Company_Code__c = 'W'].Id;
List<Opportunity> o = new List<Opportunity>();
for (Order__c order: Trigger.New){
if (order.Contact__c <> Null && order.Account__c <> Null && order.RecordTypeId == orderRT && order.Status__c == 'Open'){
getOppNumber controller = new getOppNumber();
string oppNumber = controller.generateOpportunityNumber(TTO);
oppName = order.Account__r.Name + ' - ' + order.PI_Name__c + ' - ' + oppNumber.removeStart('D');
o.add(new Opportunity(
RecordTypeId = oppRT,
Record_Sub_Type__c = 'Confidential Agreement',
AccountId = order.Account__c,
Opportunity_Number__c = oppNumber,
StageName = 'Initial Contact',
Name=oppName));
}
order.Opportunity_Name__c = oppName;
}
update order;
insert o;
}
Re: Trigger that creates new records and utilizes the new Ids
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:53 AM
Thanks, Krisk but I need to insert the opportunity Id into the Order__c.Opportunity_Name__c field (not the name of the opportunity).
Re: Trigger that creates new records and utilizes the new Ids
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 09:35 AM
Sounds like you are trying to do a compound transaction. I generally prefer to do that within an Apex class as it provides a better control on Transactions.
What I mean is that I have options such as savepoints and rollbacks.
And specifically for your situation you can do a Database.insert for new opportunity which gives back a SaveResult object that includes inserted Id and any errors.
Based on this you can take further action as to whether to rollback or extract the Id and update your Order.

