Skip to main content
Can anyone help me out .I have a trigger on Opportunitylineitem which gets fired on every profile when the product gets added more than once .Now the trigger should not get fired for few of the profiles like

 

'System Administrator' && 'Custom Marketing Users 10K 25K' && 'Customer Service User' && 'Fulfillment User'. Where should i add these user profiles in my code.Any Suggestion plz

 

Code :

 

trigger oli_multiple_products_before_insert on OpportunityLineItem (before insert) {

 

    for (OpportunityLineItem oli : Trigger.new) {

 

        if (Trigger.isInsert) {

 

            Integer line_Count = [SELECT COUNT()

 

                                    FROM OpportunityLineItem o

 

                                    WHERE o.OpportunityId = :oli.OpportunityId

 

                                    AND o.PriceBookEntryId = :oli.PriceBookEntryId  ];

 

            if (line_Count > 0) {

 

                oli.addError('A Product can not be added more than once to the Opportunity.');

 

         }                   

 

        }

 

    }

 

}

 

i tried adding the profiles as

 

trigger oli_multiple_products_before_insert on OpportunityLineItem (before insert) {

 

    for (OpportunityLineItem oli : Trigger.new) {

 

        if (Trigger.isInsert) {

 

      

 

        if(

 

              userinfo.getProfileId() != 'System Administrator' &&

 

              userinfo.getProfileId() !='Custom Marketing Users 10K 25K '&&

 

              userinfo.getProfileId() !='Customer Service User'&&

 

              userinfo.getProfileId() !='Fulfillment User'

 

           ) {

 

            Integer line_Count = [SELECT COUNT()

 

                                    FROM OpportunityLineItem o

 

                                    WHERE o.OpportunityId = :oli.OpportunityId

 

                                    AND o.PriceBookEntryId = :oli.PriceBookEntryId  ];

 

            if (line_Count > 0) {

 

                oli.addError('A Product can not be added more than once to the Opportunity.');

 

         }                   

 

        }

 

    }

 

  }

 

}

 

But still throws an Error .I shall appreciate your help.
12 respostas
  1. 9 de jan. de 2015, 06:57
    Hi Meghna,

     

    Creating a unique text field

    1. Go to Setup > Build > Customize > Opportunities > Opportunity Products > Fields
    2. New custom field
    3. Choose datatype: Text and click Next
    4. Give it a name, and Length anything between 40 to 255
    5. Click Unique check box and Treat "ABC" and "abc" as different values (case sensitive) and Click Next
    6. Next > Save (don't add it to the layout)

    Creating a workflow

    1. Go to Setup > Build > Create > Workflow and Approvals > Workflow Rules 
    2. Create New Rule
    3. Choose object : Opportunity Product and click Next
    4. Give it a Name and Description
    5. Evaluate the rule when a record is: Created (Choose Created and Everytime its edited, to update existing records)
    6. Choose Formula Evaluates to True

    IF(

    CASE($Profile.Name ,

    "System Administrator", 0,

    "Custom Marketing Users 10K 25K",0,

    "Customer Service User",0,

    "Fulfillment User",0,

    1)=1,

    TRUE,

    FALSE

    )

    • Click Save & Next​​
    • Add Workflow Actions : New Field Update
    • Hi Meghna, Creating a unique text fieldGo to Setup > Build > Customize > Opportunities > Opportunity Products > FieldsNew custom fieldChoose datatype: Text and click NextGive it a name, and Length any
    • Update Unique text field created, with Formula
    • OpportunityId & PricebookEntryId

      Click

      Save > Done > Activate

    PS : You need to update existing records( either with other profile or by removing your profile from the list for some time)

     

    Thanks

     

    I hope it helps
0/9000