Created
April 5, 2019 11:02
-
-
Save mhamzas/4124fbe65fa50dfe0aa62421d3971b79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Name : CJA Create Order from Opportunity | |
| * Description : This Trigger will create order from opportunity and also add opporutnity products data into the order ITEMS field. | |
| * Date Created : 3/4/2019 | |
| * Last Date Modified : 3/4/2019 | |
| * | |
| */ | |
| trigger CJACreateOrderFromOpportunity on Opportunity (after update) { | |
| Map<Id, Opportunity> oppIdToObjMap = new Map<Id, Opportunity>(); | |
| //Get opportunities | |
| for(Opportunity opp : Trigger.new) | |
| { | |
| if(opp.StageName == 'Order Approved' && Trigger.Oldmap.get(opp.id).StageName <> 'Order Approved') | |
| oppIdToObjMap.put(opp.id, opp); | |
| } | |
| system.debug('SYA ::'+oppIdToObjMap); | |
| Map<Id, List<OpportunityLineItem>> oppIdToOppLineMap = new Map<Id, List<OpportunityLineItem>>(); | |
| //Get Opp with Opp products in a Map | |
| for(OpportunityLineItem oppLine: [select id, OpportunityId, Product2.Variant_SKU__c, Quantity, UnitPrice, Discount, CurrencyIsoCode from OpportunityLineItem where OpportunityId = : oppIdToObjMap.keyset()]) | |
| { | |
| if(oppIdToOppLineMap.get(oppLine.OpportunityId) <> null) | |
| { | |
| List<OpportunityLineItem> oppLineLst = oppIdToOppLineMap.get(oppLine.OpportunityId); | |
| oppLineLst.add(oppLine); | |
| oppIdToOppLineMap.put(oppLine.OpportunityId, oppLineLst); | |
| }else | |
| { | |
| List<OpportunityLineItem> oppLineLst = new List<OpportunityLineItem>(); | |
| oppLineLst.add(oppLine); | |
| oppIdToOppLineMap.put(oppLine.OpportunityId, oppLineLst); | |
| } | |
| system.debug('SYA ::'+oppIdToOppLineMap); | |
| } | |
| List<Order> ordToInsert = new List<Order>(); | |
| //Parse the opp products and add it to the Orders item_name field | |
| for(Opportunity opp : oppIdToObjMap.values()) | |
| { | |
| String itemName = ''; | |
| if(oppIdToOppLineMap.get(opp.Id).size() > 0) | |
| { | |
| for(OpportunityLineItem oppLine : oppIdToOppLineMap.get(opp.Id)) | |
| { | |
| if(itemName <> '') | |
| itemName += ';'; | |
| itemName += oppLine.Product2.Variant_SKU__c+'|'+oppLine.Quantity+'|'+oppLine.UnitPrice+'|'+oppLine.CurrencyIsoCode;//+'\\|'+oppLine.Discount; | |
| } | |
| } | |
| Order Ord = new Order(AccountId = opp.AccountId, Status = 'Draft', CurrencyIsoCode = opp.CurrencyIsoCode, Created_From_TradeGecko__c = FALSE, EffectiveDate = date.today(), Issue_Date__c = date.today(), Item_Name__c = itemName); | |
| system.debug('SYA ::'+Ord); | |
| ordToInsert.add(Ord); | |
| } | |
| try | |
| { | |
| insert ordToInsert; | |
| }catch(Exception ex) | |
| { | |
| system.debug('SYA ::'+ex); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment