1

I have been battling this for a while and I assume this might be a permission issue.
I do have a trigger that updates sample requests for a product with manager that needs it to be assigned. The manager updated using trigger and is assinged based on product selected for sample. When I make a request for a sample using administrator, it populates the bu manager properly. But when requesting it a partner user, it doesn't. Below is my trigger and class.

I suppose the issue might be in either of the two methods I have provided below.

Trigger

Trigger SamplingTrigger on Sampling__c (before insert, before update, before delete, after insert,after update) {
    if (Trigger.isBefore) {          
        if(Trigger.isInsert) {
            //Before Insert Triggers
            SampleUtility.AutoUpdateSampleSemtechTeam(Trigger.new);  
            SampleUtility.PopulateRegionTerritoryOnSampling(Trigger.new);
        }
        if(Trigger.isInsert || Trigger.isUpdate){
            SampleUtility.updateSamplingTransZone(Trigger.new);
            SampleUtility.updateSamplingEVB(Trigger.new, Trigger.oldMap);
        }  
    }
    if(Trigger.isAfter) {
        //call handler.after method
        if(Trigger.isInsert){
            SampleUtility.populateBUManagerOnSample(Trigger.new);
            SampleUtility.RequestSharing(Trigger.new,Trigger.newMap);          
        }

        if(Trigger.isUpdate){
            SampleUtility.RequestSharing(Trigger.new,Trigger.newMap);
            SampleUtility.autoApproval(Trigger.new);
        }
    }
}



Class

public static void populateBUManagerOnSample(List<Sampling__c>sampling){
            //After insert Trigger
            List<Business_Unit_Managers__c> lstBUMgrs=[Select b.Product_Line__c ,b.Product_Family__c,b.Object_Type__c, b.Item_Accounting_Class__c, b.Id, b.Business_Unit__c, b.BU_Manager__c, b.BU_Manager1__c, b.BU_Manager2__c, b.BU_Manager3__c, b.BU_Manager4__c From Business_Unit_Managers__c b where b.Object_Type__c='Sample'];
            Map<String,Business_Unit_Managers__c> MapBUMgrs = new Map<String,Business_Unit_Managers__c>();
            List<Sampling__c > lstSampleUpdate=new List<Sampling__c >();

            for(Business_Unit_Managers__c bu:lstBUMgrs)
            MapBUMgrs.put(bu.Item_Accounting_Class__c + bu.Product_Line__c + bu.Product_Family__c ,bu);
            System.debug('Map BU' +MapBUMgrs);


            if(Trigger.isAfter){
              for(Sampling__c sp: sampling){  
                id tempUser;
                Sampling__c s = new Sampling__c (id=sp.id); 


                if(MapBUMgrs.containsKey(sp.Business_Unit__c + sp.Product_Line__c + sp.Product_Family__c )){
                  Business_Unit_Managers__c temps = MapBUMgrs.get(sp.Business_Unit__c + sp.Product_Line__c + sp.Product_Family__c);
                  System.debug('inside' +temps);
                  if(temps.BU_Manager__c!=null)
                  tempUser = temps.BU_Manager__c;
                  else if(temps.BU_Manager1__c!=null)
                  tempUser=temps.BU_Manager__c;
                  else if(temps.BU_Manager2__c!=null)
                  tempUser=temps.BU_Manager__c;
                  else if(temps.BU_Manager3__c!=null)
                  tempUser=temps.BU_Manager__c;
                  else if(temps.BU_Manager4__c!=null)
                  tempUser=temps.BU_Manager__c;
                  s.BU_Manager__c  =(temps.BU_Manager__c!=null  ? temps.BU_Manager__c :tempUser);
                  s.BU_Manager1__c =(temps.BU_Manager1__c!=null ? temps.BU_Manager1__c :tempUser);
                  s.BU_Manager2__c =(temps.BU_Manager2__c!=null ? temps.BU_Manager2__c :tempUser);
                  s.BU_Manager3__c =(temps.BU_Manager3__c!=null ? temps.BU_Manager3__c :tempUser);
                  s.BU_Manager4__c =(temps.BU_Manager4__c!=null ? temps.BU_Manager4__c :tempUser);
                  lstSampleUpdate.add(s);
                }
              }
            }
            try{
              update lstSampleUpdate;
              system.debug('Product Line! ' + lstSampleUpdate);
            }
            catch(Exception e){
              system.debug('Exception '+e);
            }
          }

public static void RequestSharing(List<Sampling__c> sampling, Map<Id,Sampling__c>samplingMap) {
    //After Insert, After Update
    //Set only for FA
    Id PartnerUsermap = UserUtils.USER_BY_USERTYPE.get('PowerPartner');

    List<GroupMember>GroupMemberList = new List<GroupMember>([select GroupId,UserOrGroupId from GroupMember]); 
    List<Sampling__Share> sShareList = new List<Sampling__Share>();


    if(PartnerUsermap != null){
      for(Sampling__c s : sampling){ 
        if(PartnerUsermap == s.CreatedById){
          for(GroupMember grpmem : GroupMemberList){
            if(grpmem.UserOrGroupId == s.CreatedById){   
              Sampling__Share  sshare = new Sampling__Share ( ParentId = s.id,UserOrGroupId = grpmem.GroupId,AccessLevel = 'Read');                         
              sShareList.add(sshare);                         
            }
          }
        }
      } 
    }
    if(!sShareList.isEmpty()){
      Database.SaveResult[] lsr = Database.insert(sShareList,false);           
      Integer i=0;
      for(Database.SaveResult sr : lsr){
        if(!sr.isSuccess()){ 
          Database.Error err = sr.getErrors()[0]; 
          if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
            &&  err.getMessage().contains('AccessLevel'))){

            samplingMap.get(sShareList[i].ParentId).
            addError(
              'Unable to grant sharing access due to following exception: '
              + err.getMessage());
          }
        }
        i++;
      }  
    }     
  }


Any help would be greatly appreciated.

8
  • You may need to implement user sharing
    – crmprogdev
    Jun 9, 2015 at 22:26
  • I tried to implement user sharing for "Request" object by making it public read/write. But it doesn't allow me as its already implemented on above class defined. Method "RequestSharing"
    – Gops
    Jun 9, 2015 at 22:48
  • Check that your partner profile has read and write access to the Sampling__c object (CRUD).
    – brovasi
    Jun 9, 2015 at 22:56
  • In addition to @brovaski comments, check your debug logs to see what happens in detail when trigger is fired as a partner. Could also try running your test class as a Partner Portal User too.
    – crmprogdev
    Jun 9, 2015 at 23:31
  • Hi,<br/>After making some changes, I am getting new error now as below<br/>insufficient access rights on cross-reference id<br/>.
    – Gops
    Jun 10, 2015 at 0:27

0

You must log in to answer this question.

Browse other questions tagged .