Differences

This shows you the differences between two versions of the page.

adminhelp:quotesadministration:quote_2.0:scripting_examples [2018/08/09 10:02] (current)
mnikolic created
Line 1: Line 1:
 +====== Scripting Examples ======
 +In Quote 2.0, you have two options for creating custom calculations via scripting:\\
  
 +**Quote Fields and Calculations** - here you can create basic scripts that are triggered either after a value in a quote field is changed, after an item is added to a quote or when an item is updated.\\
 +        * The following custom calculation sets the default discount to 10% when items are added/updated:
 +<code> for item in context.AffectedItems:
 +           item.Discount = 10</code>
 +        * This script updates the quantity of the maintenance item so it matches the total number of items in the quote. This calculation should be triggered before extended list price is calculated.
 +<code>maintenance = context.Quote.GetItemByItemNumber(1)
 +      for item in context.AffectedItems:
 +          maintenance.Quantity += 1
 +
 +      #add maintenance item to affected items so that the rest of the calculations run for this item as well
 +
 +      context.AffectedItems.AddItem(maintenance)</code>
 +
 +**Global Scripts** - for complex calculations that span over multiple fields, you should create a global script and attach it to an event (e.g. **OnItemAdded** and **OnItemUpdated**).
 +       * This script calculates main item totals and totals for each product type.
 +<code>
 +def Calculate(quote, fieldName): 
 +
 +   grandTotal = 0 
 +   for p in quote.ProductTypes: 
 +       p[fieldName] = 0 
 +
 +   for item in quote.GetAllItems(): 
 +       if item.IsLineItem == False: 
 +           grandTotal += item[fieldName] 
 +      
 +           pt = iter(filter(lambda x: x.ProductTypeName == item.ProductTypeName, quote.ProductTypes)).next() 
 +       
 +           pt[fieldName] += item[fieldName] 
 +       
 +    quote.Totals[fieldName] = grandTotal 
 +
 +
 +
 +def Execute(quote): 
 +   '''Calculate totals for OTC, MRC and Grand Total 
 +  
 +   Args: 
 +       quote (IQuote2): Quote to calculate totals 
 +   ''' 
 +   Calculate(quote, "GrandTotal") 
 +   Calculate(quote, "TotalAmountTotal_OTC") 
 +   Calculate(quote, "TotalAmountTotal_MRC")</code>
You are here: CallidusCloud CPQ Online HelpAdmin Page HelpQuotes AdministrationQuote 2.0Scripting Examples