+ Start a Discussion
shan876shan876 

How do I work around???PLZZZ

I downloaded the Quote Sale Line app from app excahnge written by salesforce.com. I have two scenarios:

  • where if the user is not the admin and sales_discount is more then 30% then they can not edit the sales discount.
  • Now if the reseller is on the reseller table and the discount follows through but if it is more then 30% that above rule applies and does not insert the record from the code below and it comes up with an error.

Here is the code I modified but do not know how to place in the correct parameters for a validation or a new trigger rule:

 

// copy lines from the oppportunity to this quote public void copyLineItemsFromOpptyToQuote() { // delete any existing quote lines OpportunityLineItem[] olist; olist = [Select Id, OpportunityId, SortOrder, PricebookEntryId, Quantity, TotalPrice, UnitPrice, ListPrice, ServiceDate, Description, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, IsDeleted , PricebookEntry.Name, PricebookEntry.Product2id From OpportunityLineItem where OpportunityId = :quote.Opportunity__c ]; // create new quote lines SFDC_520_QuoteLine__c[] qlines = new list<SFDC_520_QuoteLine__c>(); for ( OpportunityLineItem oitem : olist) { Reseller__c[] r =[Select Name, License__c from Reseller__c where Name=:quote.Partner_Direct__c]; if(r.isEmpty()){ SFDC_520_QuoteLine__c ql = new SFDC_520_QuoteLine__c( Quote__c = quote.id, Sales_Discount__c = 0.0, Qty_Ordered__c = oitem.Quantity, Unit_Price__c = oitem.UnitPrice, Description__c = oitem.Description, ServiceDate__c = oitem.ServiceDate, Product2__c = oitem.PricebookEntry.Product2id ); qlines.add(ql); } if(!r.isEmpty()){ SFDC_520_QuoteLine__c ql = new SFDC_520_QuoteLine__c( Quote__c = quote.id, Sales_Discount__c =r[0].License__c, Qty_Ordered__c = oitem.Quantity, Unit_Price__c = oitem.UnitPrice, Description__c = oitem.Description, ServiceDate__c = oitem.ServiceDate, Product2__c = oitem.PricebookEntry.Product2id ); qlines.add(ql); } } delete quote.Quote_Lines__r; if ( qlines.size() > 0 ) { insert qlines; } }

 

 Any help would be truly appreciated....

Thanks

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
WhyserWhyser

I didn't download this app, but I'll try my best.

I'm not exactly sure how you would determine what the sales discount is but I would think that you would be comparing it to the list price of the product to the sales price indicated by the oitem, or you would be using the field Sales_Discount__c

 

To determine the user, you can use the UserInfo object and call either UserInfo.getName() to return the full name of the user, or UserInfo.getUserName() or UserInfo.getUserId() to get the login name or ID if you want to determine whether or not it's the admin user. 

 

Assuming you are comparing PricebookEntry value to Opportunity Line Item value

To determine how much of a discount you're getting, you'll need to figure if ( oitem.ListPrice - oitem.Unitprice )/oitem.ListPrice > 0.3 to determine if it's more than 30% discount.

 

If you're using the Sales_Discount__c field indicated by the custom field in SFDC_520_QuoteLine__c or whatever value that will be populating it, then use that value to determine whether you are getting 30% off or not.

 

Then you can do this check before you do the if ( r.isEmpty() ) statements

 

if( ( oitem.ListPrice - oitem.Unitprice )/oitem.ListPrice > 0.3 && UserInfo.getName() != 'admin' )

  throw new myException('Cannot give more than 30% discount');

 

To throw exceptions like the example above, you will need to create a new class like so

public class MyException extends Exception {}

somewhere in your main class

 

Hope that helps?