function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mac SwiftMac Swift 

Please help me with the issue.

For example - we have ABC opportunity and having AA, BB, CC products in that opportunity. Now Sales rep won the opportunity and customer wants to purchase only AA and CC product and don't want the BB product. Here Sales rep will delete the BB product from the opportunity and close the deal as "Closed Won"

PROBLEM: Mgt wants the report of the deals with closed lost status. How I can address such issues in my reporting if sales rep delete the products from the opportunity if customer doesn't want them. Logically those are opp lost but not able to track them via reporting.
Best Answer chosen by Mac Swift
YiQin HeYiQin He
I assume you are using Opportunity Products
Here's the steps:
1. Add a new checkbox field in Opportunity Products: Setup -> Customize(Left panel) -> Opportunity -> Opportunity Products -> Fields -> New 
In the example, I name this field 'IsDeleted'
User-added image
User-added image

2. Create a custom button for Opportunity Products: Opportunity Products -> Button And Links -> New Button And Links
Settings for the btton: Display Type: Detail Page Button, Behavior: Excute Javascript, Content Source: OnClick Javascript.
Copy and paste the following code to the white box in that setting page:

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}

var p = new sforce.SObject("OpportunityLineItem");
p.Id = "{!OpportunityLineItem.Id}";
p.IsDeleted__c = true;
result = sforce.connection.update([p]);
if(result[0].getBoolean("success"))
{
    alert("Opportunity product updated successfully");
    window.location.reload();
}
else
{
    alert("Error: " + result);
}

User-added image

3. Add the button to your page layout: Opportunity Products -> Page Layouts -> (Assmuing you are using the standard layout) Edit -> Drag and drop your button into the layout and save.

User-added image

Now if you go to your product page, you will find this button.

All Answers

YiQin HeYiQin He
Hi Mac,

I suggest that instead of completely delete the product, you could create a flag field for addressing if the product is deleted or not. So when your sales rep 'delete' it, it's actully set the flag to true while your data is still there. 
Mac SwiftMac Swift
Thank you for replying!

Could you please elaborate more. I mean steps to create this functionality.
I am Newbie!
 
YiQin HeYiQin He
I assume you are using Opportunity Products
Here's the steps:
1. Add a new checkbox field in Opportunity Products: Setup -> Customize(Left panel) -> Opportunity -> Opportunity Products -> Fields -> New 
In the example, I name this field 'IsDeleted'
User-added image
User-added image

2. Create a custom button for Opportunity Products: Opportunity Products -> Button And Links -> New Button And Links
Settings for the btton: Display Type: Detail Page Button, Behavior: Excute Javascript, Content Source: OnClick Javascript.
Copy and paste the following code to the white box in that setting page:

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}

var p = new sforce.SObject("OpportunityLineItem");
p.Id = "{!OpportunityLineItem.Id}";
p.IsDeleted__c = true;
result = sforce.connection.update([p]);
if(result[0].getBoolean("success"))
{
    alert("Opportunity product updated successfully");
    window.location.reload();
}
else
{
    alert("Error: " + result);
}

User-added image

3. Add the button to your page layout: Opportunity Products -> Page Layouts -> (Assmuing you are using the standard layout) Edit -> Drag and drop your button into the layout and save.

User-added image

Now if you go to your product page, you will find this button.
This was selected as the best answer
Mac SwiftMac Swift
Thank you so much for the quick reply and help! Really Appreciate!!

When I copy paste the code for creating the button its working and also able to drag the custom button i.e Delete_Product to the Opportunity Product Layout. But when I go to Opportunity product and try to change the layout for the Product related list the button is not available. 

I dont know where I am wrong. Sorry for asking such a basic question as I am trying to learna nd explore.

 
Mac SwiftMac Swift
Hi YiQin He,

Thankyou again for your help! Its working now I am able to edit the page layout as well.

Appreciate if you could help me to remove the "Del" button. As if Sales rep use that button in that case again the issue will remain the same. We want that Sales rep are not able to remove the data as it will affect the reports. If there is no "Del" button on the left side then Sales rep needs to click on the product and and then click on the custom button i.e  "Delete Product" and then the IsDelete Check box will checked.

Please help me!
Mac SwiftMac Swift
"Del" Button
YiQin HeYiQin He
Hi Mac,

Unfortunately, you cannot modify action button with standard page. A simple way is that you could set user's permission to not allow them delete the data, but the action button is still there. Another way is much complicated but it does meet your requirement: You will need to create a VisualForce page and embed your VF page into Opportunity page.
Mac SwiftMac Swift
Thanks YiQin!
Somebody suggested to have a trigger. So when Sales rep delete the products line item from the opportunity it will update the custome object as opp lost and then we can do the reporting easliy and track those deleted products as well.

Could you help me in creating that kind of trigger.
YiqinYiqin
You mentioned 'update the custom object', do you mean that you want to save the deleted product to a new custom object or just update the custom flag field that I suggested to create?
 
Mac SwiftMac Swift
Yes new Custom Object (may be we can call it Deleted Opportunity)
YiqinYiqin
I will just show you the basic structure of how to create a delete trigger:
trigger OpportunityLineitem_AfterDelete on OpportunityLineItem (after delete) {
	for(OpportunityLineItem oli : Trigger.old)
    {
        //Create a instance of your custom object and copy all of your product value
        //and finally insert it.
    }
}