+ Start a Discussion
jayshree kbjayshree kb 

assign values in lookup in trigger

I have an Obj called Observation. Im trying to write a trigger so when each time a record in inserted in Observation a new record has to be created in another obj called Share_Staging_Table__c. Im geeting an error when im trying to assign value to the lookupfield .Pls find the code and pls do help.


public class ObsTriggerHandler {
List<Share_Staging_Table__c> SSTList= new list<Share_Staging_Table__c>();
Set<Id> userSet = new Set<Id>();   
List <User> ObsUser = new List <User>();
public void Afterinsert(List<Observation__c> newRec)
  {
for(Observation__c recObservation : newRec){ 
    userSet.add(recObservation.LastModifiedBy.id);
    ObsUser = [select Name ,id from User where id IN :userSet ];
    Share_Staging_Table__c SST = new Share_Staging_Table__c() ; 
    SST.Observation__c = recObservation.Name;
    SST.Shared_with__c = ObsUser[0].id;
    SSTList.add(SST);  
    }
  }
    if(SSTList != null && SSTList.size() != 0) 
    {
        Database.insert(SSTList) ;
    }
   
   }
jayshree kbjayshree kb
Here Shared_with__c and LastModifiedBy both field are look up fields .