Opportunity to Sales Order Sample Apex Integration
Opportunity to Sales Order process is one of the most sought-after integrations in the Salesforce platform.
The process is unique to each business and hence customised to fit the needs. The article illustrates a simple integration to generate a Sales Order in Aedon from Salesforce Opportunities. Custom fields can be added to Sales Order object in Aedon for additional data mapping from Opportunity.
Sample Implementation
- A simple 3-line Apex Trigger is used to generate a Sales Order when the Opportunity is Closed-Won. The data field mappings are held in an Apex Class.
- A Salesforce Flow is written to send an email notification to the user.
Implementation Steps
- Create an Apex Trigger and copy and paste the code from below
- Create an Apex Class and copy and paste the code from below
- Create the Flow if you want an email notification as well.
Salesforce Flow

Apex Trigger
Converts the Sales Order when the Opportunity Stage is changed to Closed Won.
trigger OppSOCreate on Opportunity ( after update)
Apex Class
The Apex Class contains the business logic and the data mappings
public with Sharing class OppSOCreate {
//@InvocableMethod(Label = 'OppSOConversion')
public static void checkOpportunityStageValue(List opplist){
//List oppList1 =[SELECT Id,CloseDate,Description , AccountId ,Amount,StageName FROM Opportunity];
Id accId;
List oppLineItemList = [Select Id, OpportunityId,Product2Id ,Quantity,UnitPrice from OpportunityLineItem where OpportunityId IN:opplist];
String userId;
if(!Test.isRunningTest()) {
userId = String.valueOf(UserInfo.getUserId());
} else {
User u = [select Id,Name,Email from User where Email='testuser@gmail.com' WITH SECURITY_ENFORCED];
userId = u.Id;
}
List lstUserCompany = [select Id, Name, User__c, User__r.Name, Current_Logged_In_Company__c, Current_Logged_In_Company__r.Name from User_Company__c where User__c=:userId WITH SECURITY_ENFORCED LIMIT 1];
String compnayId = lstUserCompany[0].Current_Logged_In_Company__c;
VAT__c salesVatCode = [SELECT Id, Name from Vat__c where Name='S0′ and aedonpd4__Company__c=:compnayId with security_enforced];
VAT__c purchaseVatCode = [SELECT Id, Name from Vat__c where Name='P0′ and aedonpd4__Company__c=:compnayId with security_enforced];
String taxId;
List taxIdList = new List();
List accIDList = new List();
Currency_Exchange_Rate__c exchangeRateObj = new Currency_Exchange_Rate__c();
Currency__c basecurrency = [Select Id,Name,ISO_Code__c, IsBaseCurrency__c From Currency__c
where Company__c = :compnayId
and IsBaseCurrency__c = true WITH SECURITY_ENFORCED limit 1];
String billingStreet;
String billingCity;
String billingState;
String billingCountry;
String billingPostalCode;
String address;
Sales_Order_Header__c soHeader = new Sales_Order_Header__c();
for(Opportunity opp : opplist){
accId = opp.AccountId;
accIDList.add(accId);
for(Account acc : [select Id ,Name, Company__c,Balance__c ,Preferred_Currency1__c,Tax_Treatment__c, Credit_Limit__c,
BillingAddress,Account_Type__c,Active__c,
BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode from Account where Id=:opp.AccountId]){
taxId = acc.Tax_Treatment__c;
for(Currency_Exchange_Rate__c exchangeRate : [Select Id,Name,Exchange_Rate__c,Company__c,From_Currency__c,From_Date__c,To_Currency__c
FROM Currency_Exchange_Rate__c
WHERE Company__c = :compnayId AND
//AND From_Date__c <= :selectedate
From_Currency__c =: basecurrency.Id AND To_Currency__c=:acc.Preferred_Currency1__c
WITH SECURITY_ENFORCED ORDER BY From_Date__c DESC,LastModifiedDate DESC limit 1]){
exchangeRateObj = exchangeRate;
system.debug(exchangeRateObj);
}
if((opp.StageName == 'Closed Won')){
soHeader.Company__c = acc.Company__c;
soHeader.Account__c = acc.Id;
soHeader.Currency__c = acc.Preferred_Currency1__c;
soHeader.Currency_Exchange_Rate__c = exchangeRateObj.Exchange_Rate__c;
soHeader.Invoice_Date__c = opp.CloseDate;
soHeader.Posting_Date__c = opp.CloseDate;
//soHeader.Due_On__c = siwrap.dueOn;
soHeader.Posting_Status__c = 'Draft';
soHeader.Customer_Reference__c = opp.OrderNumber__c;
soHeader.Source__c = opp.Name;
if(acc.BillingStreet != null){
billingStreet = acc.BillingStreet;
}
else{
billingStreet = ";
}
if(acc.BillingCity != null){
billingCity = acc.BillingCity;
}
else{
billingCity = ";
}
if(acc.BillingState != null){
billingState = acc.BillingState;
}
else{
billingState = ";
}
if(acc.BillingCountry != null){
billingCountry = acc.BillingCountry;
}
else{
billingCountry = ";
}
if(acc.BillingPostalCode != null){
billingPostalCode = acc.BillingPostalCode;
}
else{
billingPostalCode = ";
}
address = billingStreet '\n' billingCity '\n' billingState '\n' billingCountry '\n' billingPostalCode;
soHeader.Address__c = address;
insert soHeader;
}
}
}
Account acc = [Select Preferred_Currency1__c from Account where Id=:accId];
Sales_Order_Header__c salesOrderDetails = new Sales_Order_Header__c();
if(soHeader.Id!=null){
salesOrderDetails=[Select id,Name,Account__c,Account__r.Name,Account__r.Display_Label_UI1__c,Company__c,Customer_Reference__c,Due_On__c,Invoice_Date__c,Currency__c,VAT_Treatment__c,
Balance_Outstanding__c,Currency_Exchange_Rate__c,Currency__r.Name,Posting_Status__c,Payment_Status__c,Total_Tax_Payable__c,Gross_Amount__c,Paid_Amount__c,VAT_Only__c,Source__c,Payment_Method__c,On_Hold__c,Planned_Pay_Date__c,Address__c,CreatedDate
from Sales_Order_Header__c where Id=:soHeader.Id WITH SECURITY_ENFORCED];
}
List productSet = new List();
List lstAccProds = [select Id,Name,Account__c,Product__c,Product__r.IsActive,Product__r.Display_Label_UI1__c,Product__r.Display_Label_Product_UI__c,Product__r.Description,Product__r.Sales_Account1__c,Product__r.Sales_Account1__r.Nominal_Account__c from Account_Product_Default__c where Account__c In:accIDList WITH SECURITY_ENFORCED];
// objAccProdWrapper.accProdDefaults = lstAccProds;
if(lstAccProds != null && lstAccProds.size() > 0) {
for(Account_Product_Default__c objAccProd : lstAccProds) {
productSet.add(objAccProd.Product__c);
}
}
system.debug(compnayId);
system.debug(productSet);
system.debug(taxIdList);
Decimal vat20=0.00;
Decimal Vat5=0.00;
Decimal discount=0;
Decimal total=0.00;
Decimal totalTax=0.00;
List sotList = new List();
for(OpportunityLineItem oppLine : oppLineItemList){
for(Product2 prodDetails: [Select Id, Name, Description, Product_Type__c, Sales_Account1__c,Purchase_Account1__c from Product2 where Id=:oppLine.Product2Id]){
system.debug('Prod Name: '+prodDetails.Name);
for(Product_Tax_Treatment__c lstProdTaxTreatment : [select Id,Name,Company__c,Product_Type__c,Product__c,VAT_Code__c,VAT_Code__r.Display_Label_UI1__c,VAT_Code__r.Rate__c from Product_Tax_Treatment__c WHERE Product__c =: prodDetails.Id AND Company__c =:compnayId AND Tax_Treatment__c =:taxId WITH SECURITY_ENFORCED]){
if(prodDetails.Product_Type__c == 'Sales'){
Sales_Order_Transaction__c sot = new Sales_Order_Transaction__c();
sot.Sales_Order_Header__c = soHeader.id;
sot.Company__c = soHeader.Company__c;
if(prodDetails.Sales_Account1__c!=null){
sot.Nominal_Code2__c = prodDetails.Sales_Account1__c;
}
else{
sot.Nominal_Code2__c = salesVatCode.Id;
}
sot.Product__c = oppLine.Product2Id ;
//sit.Product__r.Name = SIDetails.Products[i].productName;
//sit.Product__c = productIds[i].Id;
sot.Currency_Exchange_Rate__c = soHeader.Currency_Exchange_Rate__c;
sot.Currency__c = soHeader.Currency__c;
sot.Customer_Reference__c = 'test';
sot.Net_Transaction_Amount__c = oppLine.Quantity * oppLine.UnitPrice ;
//sot.Tax_Payable__c = ((SIDetails.Products[i].vatCodeRate * sit.Net_Transaction_Amount__c)/100).setScale(2);
sot.Posting_Date__c = soHeader.Posting_Date__c;
sot.Posting_Status__c = soHeader.Posting_Status__c;
sot.Quantity__c = oppLine.Quantity;
//sot.Tax_Rate__c = sit.Tax_Payable__c ;
sot.Amount__c = sot.Net_Transaction_Amount__c;
sot.Sale_VAT__c = lstProdTaxTreatment.VAT_Code__c;
sot.Tax_Rate__c = lstProdTaxTreatment.VAT_Code__r.Rate__c;
sot.Unit_Price__c = oppLine.UnitPrice ;
sot.Tax_Payable__c = sot.Net_Transaction_Amount__c * (sot.Tax_Rate__c/100);
sot.Amount__c = sot.Net_Transaction_Amount__c sot.Tax_Payable__c;
sot.Description__c = prodDetails.Description;
sotList.add(sot);
}
else if (prodDetails.Product_Type__c == 'Purchases'){
Sales_Order_Transaction__c sot = new Sales_Order_Transaction__c();
sot.Sales_Order_Header__c = soHeader.id;
sot.Company__c = soHeader.Company__c;
if(prodDetails.Purchase_Account1__c!=null){
sot.Nominal_Code2__c = prodDetails.Purchase_Account1__c;
}
else{
sot.Nominal_Code2__c = purchaseVatCode.Id;
}
sot.Product__c = oppLine.Product2Id ;
//sit.Product__r.Name = SIDetails.Products[i].productName;
//sit.Product__c = productIds[i].Id;
sot.Currency_Exchange_Rate__c = soHeader.Currency_Exchange_Rate__c;
sot.Currency__c = soHeader.Currency__c;
sot.Customer_Reference__c = 'test';
sot.Net_Transaction_Amount__c = oppLine.Quantity * oppLine.UnitPrice ;
//sot.Tax_Payable__c = ((SIDetails.Products[i].vatCodeRate * sit.Net_Transaction_Amount__c)/100).setScale(2);
sot.Posting_Date__c = soHeader.Posting_Date__c;
sot.Posting_Status__c = soHeader.Posting_Status__c;
sot.Quantity__c = oppLine.Quantity;
//sot.Tax_Rate__c = sit.Tax_Payable__c ;
sot.Amount__c = sot.Net_Transaction_Amount__c;
sot.Sale_VAT__c = 'a058d0000066MbAAAU';
sot.Unit_Price__c = oppLine.UnitPrice ;
sotList.add(sot);
}
}
}
}
if(salesOrderDetails.Id!=null){
insert sotList;
}
if(soHeader.Id!=null){
Sales_Order_Header__c salesOrderDetails1=[Select id,Name,Account__c,Account__r.Name,Account__r.Display_Label_UI1__c,Company__c,Customer_Reference__c,Due_On__c,
Invoice_Date__c,Currency__c,VAT_Treatment__c,Currency__r.IsBaseCurrency__c,
Convert_to_Invoice__c,Balance_Outstanding__c,Currency_Exchange_Rate__c,Currency__r.Name,Posting_Status__c,Payment_Status__c,
Total_Tax_Payable__c,Gross_Amount__c,Paid_Amount__c,VAT_Only__c,Invoice_ID__c, Invoice_Name__c,Invoice__c,Email_Sent_Text__c,Source__c,Payment_Method__c,On_Hold__c,Planned_Pay_Date__c,Address__c
from Sales_Order_Header__c where Id=:soHeader.Id WITH SECURITY_ENFORCED limit 1];
List invoiceTransactionDetails=[Select id,Name,Product__c,Product__r.Name,Product__r.Display_Label_Product_UI__c,Product_Code__c,Product_Description__c,Quantity__c,Tax_Rate__c,
Convert_to_Invoice__c,Allocated_Credit__c,Paid_Amount__c,Currency_Exchange_Rate__c,Currency__c,Description__c,Net_Transaction_Amount__c,Amount__c,Company__c,Company__r.Name,Discount__c,Discount_Value__c,Unit_Price__c,Tax_Payable__c,Sales_Order_Header__c,
Sale_VAT__c,Sale_VAT__r.Display_Label_UI1__c,Nominal_Code2__c,Nominal_Code2__r.Nominal_Account__c,Analysis_1__c,Analysis_2__c,Analysis_1__r.Name,Analysis_2__r.Name,Analysis_3__c,Analysis_3__r.Name,Analysis_4__c,Analysis_5__c,Analysis_6__c,Analysis_7__c,Analysis_8__c,Analysis_9__c,Analysis_10__c,Analysis_10__r.Name,Analysis_4__r.Name,Analysis_5__r.Name,Analysis_6__r.Name,Analysis_7__r.Name,Analysis_8__r.Name,Analysis_9__r.Name from Sales_Order_Transaction__c where Sales_Order_Header__c =:salesOrderDetails1.Id WITH SECURITY_ENFORCED];
}
}
}




















