RE: Calculating Quote.Discount/Percent in CRMScript trigger
Hi,
I have a script that also maybe can benfit from the content in this thread by making it more efficient.
This script is started by the trigger after a sale is saved.
The only possible event, from my understanding, is for this script to be executed in the context of a user saving a sale.
The reason is that the users create a project just before order creation and the ERP system requires a valid project number on all quote lines. In many cases the project doesn't exist until it is time to create the order, and in this case the already created quote lines needs to get populated with project number.
The reason for the after event and not the before, is because it needs to respond to the case when the sale gets connected to a project, and I haven't been able to read the updated project data from the current context (i.e. when the user connects the sale to a project with the project dropdown on the sale's main window or changes the project connected to the sale from one project to another project).
The issue I'm having with making the code below more efficient with the approach in this thread is that I can't find the values needed in the EventData object. Since the context I'm in is related to the sale.
Is there a way to change the current context within the script or some other smart way of taking advantage of the setOutputValue method for the script below?
#setLanguageLevel 3;
EventData eventData;
NSProjectAgent projectAgent;
NSProjectEntity projectEntity;
NSSaleAgent saleAgent;
NSSale sale;
NSQuoteAgent quoteAgent;
NSQuote quote;
NSQuoteAlternative[] quoteAlternatives;
NSQuoteLine[] quoteLines;
String[] output;
eventData = getEventData();
sale = saleAgent.GetSale( eventData.getInputValue( "SaleEntity.SaleId" ).toInteger() );
projectEntity = projectAgent.GetProjectEntity( sale.GetProjectId() );
if ( sale.GetProjectId() == 0 )
{
output.pushBack( "Salg " + sale.GetSaleId().toString() + " er ikke lagt inn i prosjekt. Avslutter." );
return;
}
else
{
output.pushBack( "Salg " + sale.GetSaleId().toString() + " ligger under prosjekt " + sale.GetProjectId().toString() + "..." );
if ( projectEntity.GetActiveErpLinks() < 1 )
{
eventData.setBlockExecution( true );
eventData.setMessage( "Prosjektet er ikke koblet til ERP." );
output.pushBack( "Prosjekt " + sale.GetProjectId().toString() + " ikke koblet til ERP. Avslutter." );
return;
}
else
{
output.pushBack( "Prosjekt " + sale.GetProjectId().toString() + " koblet til ERP..." );
if( quoteAgent.GetQuoteFromSaleId( sale.GetSaleId() ).GetQuoteId().toString().isEmpty() )
{
output.pushBack( "Tilbud ikke opprettet for salg " + sale.GetSaleId().toString() + ", dette er mest sannsynlig et \"direktesalg\". Avslutter." );
return;
}
quote = quoteAgent.GetQuoteFromSaleId( sale.GetSaleId() );
output.pushBack( "Tilbud " + quote.GetQuoteId().toString() + " ligger under salg " + sale.GetSaleId().toString() + "..." );
quoteAlternatives = quoteAgent.GetQuoteAlternatives( quote.GetActiveQuoteVersionId() );
for( Integer i = 0; i < quoteAlternatives.length(); i++ )
{
quoteLines = quoteAgent.GetQuoteLines( quoteAlternatives[ i ].GetQuoteAlternativeId() );
if( quoteLines.length() == 0 )
{
output.pushBack( "Tilbudsalternativ " + i.toString() + " inneholder ingen tilbudslinjer." );
}
else
{
for( Integer j = 0; j < quoteLines.length(); j++ )
{
quoteLines[ j ].SetExtraField1( projectEntity.GetProjectNumber() );
quoteAgent.SaveQuoteLine( quoteLines[ j ] );
}
}
}
}
}
Best regards.