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
Eric Blaxton.ax1862Eric Blaxton.ax1862 

Help writing a trigger that creates a task from a custom object

Hi and Thank you in advance for your help.

 

I have written a few triggers, but cannot figure this one out.   Here is my code:

 

trigger Insert_ISR onRegistration__c (afterinsert) {

 

Task[] tasks = newTask[0];

 

for(Registration__c ca: Trigger.new){

   

if(ca.Inside_Sales_Rep__c != '') {

      tasks.add(

new task(

        subject='New Registration In Progress',

        ownerid=ca.Inside_Sales_Rep__c,  --- here is the problem.  This field is a picklist which holds strings and the ownerid is an ID field.  How would you accomplish this?

        activitydate=system.today().adddays(1),

        whatid=ca.Id));

    }

  }

 

insert tasks;

}

Best Answer chosen by Admin (Salesforce Developers) 
zachbarkleyzachbarkley

Hi Eric,

Ok so I'm assuming that Salesforce has stored the name of your user in it's database, not the Id.

 

When you are trying to map accross the "STRING" which is the name of your user, it's saying that It wants the Id.

 

So you would do something like:

 

trigger Insert_ISR onRegistration__c (afterinsert) {
		Task[] tasks = newTask[0];
		for(Registration__c ca: Trigger.new){
		if(ca.Inside_Sales_Rep__c != '') {
			
			String SalesRepId;
			List<User> SALESREP = [SELECT Id, Name FROM User WHERE Name =:ca.Inside_Sales_Rep__c LIMIT 1];
			if(SALESREP.IsEmpty){
			}else{
				SalesRepId = SALESREP[0].Id;
			}
		    tasks.add(
			new task(
		        subject='New Registration In Progress',
		        ownerid=SalesRepId ,
		        activitydate=system.today().adddays(1),
		        whatid=ca.Id));
		    }
		}
		insert tasks;
	
	} 

Please note that this is not "Batch Complient" so you will need to batchify this trigger. Also it's not best practice to put your logic directly into a trigger, but make a handler class to reference in your trigger.

 

 

All Answers

zachbarkleyzachbarkley

Hi Eric,

 

Try the code below.

 

trigger Insert_ISR onRegistration__c (afterinsert) {
	Task[] tasks = newTask[0];
	for(Registration__c ca: Trigger.new){
	if(ca.Inside_Sales_Rep__c != '') {
	
	    tasks.add(
		new task(
	        subject='New Registration In Progress',
	        ownerid=Id.ValueOf(ca.Inside_Sales_Rep__c) ,
	        activitydate=system.today().adddays(1),
	        whatid=ca.Id));
	    }
	}
	insert tasks;

} 

 

Eric Blaxton.ax1862Eric Blaxton.ax1862
Hi,

Your suggestion did not work " Id.ValueOf(ca.Inside_Sales_Rep__c)".

It is still being treated as a string.

Eric Blaxton.ax1862Eric Blaxton.ax1862
I also tried to use the Includes method, but the method was not recognized.


expecting a right parentheses, found 'INCLUDES'


- Save error: expecting a right parentheses, found

'INCLUDES'

Thanks
zachbarkleyzachbarkley

Hi Eric,

 

Would you please give me an example value of this string ca.Inside_Sales_Rep__c

 

EG... A sample value is a0790000008k1B7

Eric Blaxton.ax1862Eric Blaxton.ax1862
Hi, here is the source.  The picklist holds Eric Blaxton.  


</a>
</span></td><td class="labelCol empty">&nbsp;</td><td class="dataCol empty">&nbsp;</td></tr><tr><td class="labelCol last"><label for="00NJ00000014yuk">Inside Sales Rep</label></td><td class="dataCol col02 last"><span><select id="00NJ00000014yuk" name="00NJ00000014yuk" tabindex="12"><option value="">--None--</option><option value="Eric Blaxton">Eric Blaxton</option><option value="Janet Lusk">Janet Lusk</option></select></span>
zachbarkleyzachbarkley

Hi Eric,

Ok so I'm assuming that Salesforce has stored the name of your user in it's database, not the Id.

 

When you are trying to map accross the "STRING" which is the name of your user, it's saying that It wants the Id.

 

So you would do something like:

 

trigger Insert_ISR onRegistration__c (afterinsert) {
		Task[] tasks = newTask[0];
		for(Registration__c ca: Trigger.new){
		if(ca.Inside_Sales_Rep__c != '') {
			
			String SalesRepId;
			List<User> SALESREP = [SELECT Id, Name FROM User WHERE Name =:ca.Inside_Sales_Rep__c LIMIT 1];
			if(SALESREP.IsEmpty){
			}else{
				SalesRepId = SALESREP[0].Id;
			}
		    tasks.add(
			new task(
		        subject='New Registration In Progress',
		        ownerid=SalesRepId ,
		        activitydate=system.today().adddays(1),
		        whatid=ca.Id));
		    }
		}
		insert tasks;
	
	} 

Please note that this is not "Batch Complient" so you will need to batchify this trigger. Also it's not best practice to put your logic directly into a trigger, but make a handler class to reference in your trigger.

 

 

This was selected as the best answer