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
kev388kev388 

Help with Trigger

Hi Everyone,

I am stuck on a trigger. I want this to populate the Inside Sales Representative on the record being saved.  However I am getting an error:

Review all error messages below to correct your data.
Apex trigger OpportunityISR caused an unexpected exception, contact your administrator: OpportunityISR: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityISR: line 5, column 1


Here is the Trigger Code:

trigger OpportunityISR on Opportunity (before insert){
    Opportunity oppty;
Set<Id> OpptyId= new Set<Id>();
    
    if(oppty.Inside_Sales_Representative__c == null) {
      oppty.Inside_Sales_Representative__c = Userinfo.getUserId();
      System.debug('Setting Sales Representative to ' + Userinfo.getUserId());       
    } else {
      if(Trigger.isInsert) {
        oppty.Inside_Sales_Representative__c = Userinfo.getUserId();
        System.debug('Setting Opportunity Owner to ' + Userinfo.getUserId());       
      }
}
                     }
Best Answer chosen by kev388
Edwin VijayEdwin Vijay
trigger OpportunityISR on Opportunity (before insert){

   for(Opportunity oppty: Trigger.new)
{ 
    if(oppty.Inside_Sales_Representative__c == null) {
      oppty.Inside_Sales_Representative__c = Userinfo.getUserId();
      System.debug('Setting Sales Representative to ' + Userinfo.getUserId());       
    }
}

}

Hope the code above solves the issue

All Answers

Edwin VijayEdwin Vijay
trigger OpportunityISR on Opportunity (before insert){

   for(Opportunity oppty: Trigger.new)
{ 
    if(oppty.Inside_Sales_Representative__c == null) {
      oppty.Inside_Sales_Representative__c = Userinfo.getUserId();
      System.debug('Setting Sales Representative to ' + Userinfo.getUserId());       
    }
}

}

Hope the code above solves the issue
This was selected as the best answer
kev388kev388
Hi Edwin that fixed the error - but for some reason its is not inserting the ID in the field to pull up the proper person on saving the record
Edwin VijayEdwin Vijay
Is your Debug statement printed correctly?
kev388kev388
The debug statement is not printed correctly - Once I corrected that statement it worked fine! Thank you Edwin!