+ Start a Discussion
Ryan GardnerRyan Gardner 

Removing a field from an instance of an sObject

I have a class backing a visualforce page for a custom forecasting module. Depending on which user is updating the forecast data, I want to update a different set of fields. In my visualforce I display a list of opportunities, and I want to display all the fields I query for regardless of which user is viewing the forecast, i.e. I can't just not query some fields depending on which user I'm dealing with. It looks something like this:
 
//define user roles
Boolean userIsSalesRep = false;
Boolean userIsManager = false;

if(boo=foo){
    userIsSalesRep = true;
}else{
    userIsManager = true;
}
So I have 2 Booleans that show me who I'm dealing with, then I have a method to update the opportunities. When I do the update, I don't want to update certain fields depending on which user is calling the update.
public List<Opportunity> opportunities {get; set;}
......
......

public void save(){

    // I want to do something like this-->
    if(userIsSalesRep){
        for(Opportunity o: opportunities){
            o.remove('fieldXX');
        }
    }

    update(opportunities);
}
Does anybody have a good idea on how to do this. I have some clunky ideas on what I could do, but I really don't want to make it clunky.


 
Anoop AsokAnoop Asok
Here is my clunky idea! ;)
  • Put the fields that a Sales Rep is not supposed to update in a FieldSet.
  • Capture the opportunity from the trigger (preferably before update event)
  • If the user is Sales Rep, and if any of the fields in the FieldSet are updated, revert the change based on the value you've in Trigger.Old record.

Same applies for manager users too, and I'm assuming you've an option to identify whether the logged in user is Sales Rep or Manager from the trigger as well.