+ Start a Discussion
ShawnaFisherShawnaFisher 

Trigger Question

I need to insert a record into a custom object on save in opportunity.  Here is my trigger:

 

trigger AddSRSplit on Opportunity (after insert) {       

 

      for (Opportunity o : Trigger.new) {    

          

      Sales_Rep_Split_Revenue__c Split= new Sales_Rep_Split_Revenue__c         

           

                   ( Name = 'No Split',          

                     opportunity__C = o.ID,          

                     Split__c = 100,                        

                     Sales_Rep__c = o.owner);   

       insert Split;                                   }                                                

                                                                         }

 

 

The problem is that I can not get the opportunity owner to be the sales rep name in the custom object, it keeps failing saying that an ID is expected.  Any help would be appreciated!

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi,

I am providing an running example

trigger testOPPO on Opportunity (after insert) {
    List<COntact> con = new List<Contact>();
    
    for(Opportunity o:Trigger.new){
        
        Contact c = new Contact();
        c.firstName = 'TEST';
        c.LastName = 'BOARD';
        con.add(c);
        c.ownerID = o.OwnerID;
    }
    insert con;
}

 

Change Contact with your Object.

All Answers

Sean TanSean Tan

The "Owner" field on the object is actually a reference Object to lookup to fields on the owner. For example you could do something like:

 

Owner.Name

 

Assuming you queried the Owner records that is. So in the context of the trigger it wouldn't assign anything since chances are it will be null when you first enter that Context.

 

Now to how to resolve your issue, use the OwnerId field instead, this should give you the Id you need straight. Lastly, you should not be using DML in a loop, you should bulk the the DML operation by adding the records to a list outside then call the insert call to the database. Try this:

 

trigger AddSRSplit on Opportunity (after insert) {       
	
	Sales_Rep_Split_Revenue__c[] splitList = new Sales_Rep_Split_Revenue__c[]{};
	
	for (Opportunity o : Trigger.new) {    		
		splitList.add(new Sales_Rep_Split_Revenue__c
		(
			Name = 'No Split',          		
			opportunity__C = o.ID,          		
			Split__c = 100,                        		
			Sales_Rep__c = o.OwnerId
		));		
	}                                                
	insert splitList;
}

 

gbu.varungbu.varun

Hi,

I am providing an running example

trigger testOPPO on Opportunity (after insert) {
    List<COntact> con = new List<Contact>();
    
    for(Opportunity o:Trigger.new){
        
        Contact c = new Contact();
        c.firstName = 'TEST';
        c.LastName = 'BOARD';
        con.add(c);
        c.ownerID = o.OwnerID;
    }
    insert con;
}

 

Change Contact with your Object.

This was selected as the best answer
ShawnaFisherShawnaFisher

This worked like a CHARM.  I am very new to Salesforce and would like to learn more about how to code triggers ect.  Is there a good resource that you know of?

ShawnaFisherShawnaFisher
Also THANK YOU so much!!