+ Start a Discussion
mandycmandyc 

Trigger that creates new records and utilizes the new Ids

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;            
}

 

kriskkrisk

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;
}

mandycmandyc

Thanks, Krisk but I need to insert the opportunity Id into the Order__c.Opportunity_Name__c field (not the name of the opportunity).

kriskkrisk

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.