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
ArikArik 

Task Created from Workflow to trigger new Workfow

I want a field update to take place via workflow once a task is created with another workflow.  The field is custom so it cant be added when task created...any ideas

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

acct.TestDate__c = Date.newInstance(2011,5,1);

All Answers

Andy BoettcherAndy Boettcher

You could key off of the Subject Name - have a Trigger in place that updates a checkbox field to TRUE based on that subject.  Then, you could have a workflow trigger off of that field change.

 

-Andy

ArikArik

i tried that - it didnt work

Andy BoettcherAndy Boettcher

Ahhh - found out why.  Salesforce doesn't allow this to happen.

 

You may have to go 100% APEX on this route.

 

-Andy

ArikArik

Any thoughts on how to write the APEX ?

Andy BoettcherAndy Boettcher

How are you kicking off the initial workflow?  That's the object that we'd have to start from.

ArikArik

Thanks for answering. 

The object has a field called "Contract Signed"  which is a check box.  Then there are a bunch of dates on the object representing differents steps in the process, such as last day for this, last day for that, etc.  What we want is when the checkbox checks, events are created from that checkbox.  Right now, the way it works, is tasks are created using a workflow.  I thought i could use a field update and put a custom box on the task to trigger a apex trigger to turn task into event, which does not work if task is created from workflow.  Understand ?

Andy BoettcherAndy Boettcher

To get all of the fields you want on that Task (or Event), you'd have to forgo Workflow altogether on the object and go full on APEX Triggers.  Using the trigger, you can detect if a field has changed from the previous value (false to true, or "a" to "b") and then create a Task or Event that way.

 

If you give me your object and field names that you want to do this from - I can structure up some code for you to start from.

 

-Andy

 

 

ArikArik

Thank you

Just use generic fields - they are real specific

Andy BoettcherAndy Boettcher

Something like this...

 

trigger AccountSimpleTrigger on Account (before update) {

	////////////////////////////////////
	// This trigger detects the change of the TestCheckbox field
	// and creates an Event
	////////////////////////////////////
	
	if(trigger.isUpdate && trigger.isBefore) {
		
		// Create empty List to hold new Events (bulk DML) 
		List<Event> lstNewEvents = new List<Event>();
		
		for(Account a : trigger.new) {
			
			// Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
			if(a.TestCheckbox__c = true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
				
				// Set Start Date
				Datetime dtmStart = Datetime.newInstance(System.Now().Year(),System.Now().Month(),System.Now().Day(),System.Now().Hour(),System.Now().Minute(),0);
				Datetime dtmEnd = dtmStart.addMinutes(60);
				Long lngClassDuration = (dtmEnd.getTime() - dtmStart.getTime()) / 60000;
				
				// Create Event
				Event newEvent = new Event();
				newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
				newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
				newEvent.Subject = 'New Event - Some Reason';
				newEvent.StartDateTime = dtmStart;
				newEvent.EndDateTIme = dtmEnd;
				newEvent.DurationInMinutes = Integer.valueOf(lngClassDuration);
				
				lstNewEvents.add(newEvent);
				
			}
		}
		
		if(lstNewEvents.size() > 0) { insert lstNewEvents; }
		
	}

}

 

ArikArik

This works great !!!

I am going to do the following with it.  I am going to basically make many copies of this because I have many events that have to be generated.  In other words, closing is 1 event, inspection is another event.  So my plan is to put a bunch of check marks on the object (hidden from view - meaning not on the layout), and then have 1 checkbox on the layout, whcih will cause (via workflow) all of the checkboxes to be marked true.  That would trigger the event creation for each one.

The next tiny small favor is (i know big deal) is to ask how to grab a field called eventdate and not the system date to create the event.  Also, how can I make it an "all day event".

Finally - I need the test class.

Thank you so much !!!!

Andy BoettcherAndy Boettcher

One "best practice" note - if you're going to do this on multiple checkboxes, use IF statements within the single trigger to do it.

 

One small error I just saw - in the code I provided...this is wrong..

 

if(a.TestCheckbox__c = true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {

 

should be:

 

if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {

Sorry about that.

 

If "eventdate" is a field on the object that you're triggering on - you'll just substitute out the "System.Today()". for the field name.

 

Test Class - just spin an @isTest class up and:

 

1) insert your parent record

2) flip your checkboxes to true and update the record

 

That should get them all to fire.

 

-Andy

ArikArik

Again sorry to be a pain -

How do you make the event an all day event ?

and honestly - the test class thing i never know how to do that

Thank You again

Andy BoettcherAndy Boettcher

In the code, add this line:

 

newEvent.IsAllDayEvent = true;

 (big smile) - it's not a completely full ride on the Test Class.  Start one up and take an initial stab at it.  I'll help you as you encounter issues, but I have to challenge you a *little*.  *smile*

 

-Andy

ArikArik

You rock andy - Thank you !!!

ArikArik

Can you please look at this:

trigger AccountSimpleTrigger on Account (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
    
    if(trigger.isUpdate && trigger.isBefore) {
        
        // Create empty List to hold new Events (bulk DML) 
        List<Event> lstNewEvents = new List<Event>();
        
        for(Account a : trigger.new) {
            
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
                
                // Set Start Date
                Datetime dtmStart = a.TestDate__c;
                Datetime dtmEnd = a.TestDate__c;
                Long lngClassDuration = (dtmEnd.getTime() - dtmStart.getTime()) / 60000;
                
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.IsAllDayEvent = true;
                
                
                lstNewEvents.add(newEvent);
                
            }
        }
        
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }

I have a mistake:

Error:Apex trigger AccountSimpleTrigger caused an unexpected exception, contact your administrator: AccountSimpleTrigger: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Date]: [Date]: Trigger.AccountSimpleTrigger: line 36, column 39

I am so close -I know you regret answering my first post, but if you knew how much I appreciate this :-)
       
Andy BoettcherAndy Boettcher

I don't regret it!  I'm absolutely happy I can help!

 

You need to have the StartDate filled in - that's required.

 

-Andy

ArikArik

It's the all day event thing that is throwing the error - I have it in the wrong place - any thoughts ?

Andy BoettcherAndy Boettcher

The APEX error you referenced is talking about the StartDateTime field - put that back in the code and you should be fine.

 

-Andy

ArikArik

Attached is where i am & it Works :-) !!!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

trigger AccountSimpleTrigger on Account (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
    
    if(trigger.isUpdate && trigger.isBefore) {
        
        // Create empty List to hold new Events (bulk DML) 
        List<Event> lstNewEvents = new List<Event>();
        
        for(Account a : trigger.new) {
            
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
                
                // Set Start Date
                Datetime dtmStart = a.TestDate__c;
                Datetime dtmEnd = dtmStart.addMinutes(60);
                Long lngClassDuration = (dtmEnd.getTime() - dtmStart.getTime()) / 60000;
                
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.StartDateTime = dtmStart;
                newEvent.EndDateTIme = dtmEnd;
                newEvent.DurationInMinutes = Integer.valueOf(lngClassDuration);
                
                
                lstNewEvents.add(newEvent);
                
            }
        }
        
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
        
    }

}

 

However, I have tried 100 diffeent ways to make it a all day event and every way gives error -

 

Please tell me hwere to put statement and what to delete

 

Thank you again !!!

Andy BoettcherAndy Boettcher

Here is the code modfied to make it an all-day event:

 

trigger AccountSimpleTrigger on Account (before update) {

	////////////////////////////////////
	// This trigger detects the change of the TestCheckbox field
	// and creates an Event
	////////////////////////////////////
	
	if(trigger.isUpdate && trigger.isBefore) {
		
		// Create empty List to hold new Events (bulk DML) 
		List<Event> lstNewEvents = new List<Event>();
		
		for(Account a : trigger.new) {
			
			// Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
			if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
				
				// Set Start Date
				Date dteStart = Date.newInstance(System.Now().Year(),System.Now().Month(),System.Now().Day());
				
				// Create Event
				Event newEvent = new Event();
				newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
				newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
				newEvent.Subject = 'New Event - Some Reason';
				newEvent.ActivityDate = dtmStart.Date();
				newEvent.IsAllDayEvent = true;
				
				lstNewEvents.add(newEvent);
				
			}
		}
		
		if(lstNewEvents.size() > 0) { insert lstNewEvents; }
		
	}

}

 

ArikArik
What follows is my attempt at a test class - (pathetic)
Any chance to look ?

@isTest
private class testAccountSimpleTrigger
{
public static testMethod void unitTestAccountSimpleTrigger()
{
tk1.CreateEvent__c=true;
//Task tk2 = new Task(Subject='Callme',WhatId=con.id,CreateEvent__c=true,Priority='Normal',Status='Completed',ActivityDate = System.Today(),description='This is a test');
update tk1;
}
}
Andy BoettcherAndy Boettcher

Self-initiated effort is never pathetic.  Showing that you're taking the initiative to ask and learn is what this community is all about.

 

Best practices dictate that you should create a new Account from within your Test Class:

 

Account acct = new Account();
acct.Name = 'Test Account';
insert acct;

 

Then, (using our sample trigger as the basis), set your checkboxe to TRUE and update the account again:

 

test.startTest();

acct.TestCheckbox__c = true;
update acct;

test.stopTest();

 

-Andy

ArikArik

This is beyond out of my league - is there any chance for the answer key - please

Andy BoettcherAndy Boettcher

Ahhh but you were so close!

 

@isTest
private class testAccountSimpleTrigger {

	public static testMethod void unitTestAccountSimpleTrigger() {

		Account acct = new Account();
		acct.Name = 'Test Account';
		insert acct;

		test.startTest();

		acct.TestCheckbox__c = true;
		update acct;

		test.stopTest();

	}
}

 

ArikArik

I swear - last post -

 

testAccountSimpleTrigger.unitTestAccountSimpleTrigger919.0System.DmlException: Update failed. First exception on row 0 with id 001M0000009HQivIAG; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountSimpleTrigger: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ActivityDate]: [ActivityDate] Trigger.AccountSimpleTrigger: line 34, column 39: []Class.testAccountSimpleTrigger.unitTestAccountSimpleTrigger: line 13, column 9 External entry point
Andy BoettcherAndy Boettcher

What did your trigger end up looking like?

ArikArik

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
trigger AccountSimpleTrigger on Account (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
    
    if(trigger.isUpdate && trigger.isBefore) {
        
        // Create empty List to hold new Events (bulk DML) 
        List<Event> lstNewEvents = new List<Event>();
        
        for(Account a : trigger.new) {
            
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
                
                // Set Start Date
                Date dteStart = a.TestDate__c;
                
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.ActivityDate = a.TestDate__c;
                newEvent.IsAllDayEvent = true;
                
                lstNewEvents.add(newEvent);
                
            }
        }
        
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
        
    }
Andy BoettcherAndy Boettcher

Your a.TestDate__c field is null when the Trigger fires.

 

-Andy

ArikArik

Pardon my iggnorance, but see below:

 

Error: Compile Error: Variable does not exist: a.TestDate__c at line 8 column 9

 

@isTest
private class testAccountSimpleTrigger {

    public static testMethod void unitTestAccountSimpleTrigger() {

        Account acct = new Account();
        acct.Name = 'Test Account';
        a.TestDate__c = '1/1/2012';
        insert acct;

        test.startTest();

        acct.TestCheckbox__c = true;
        update acct;

        test.stopTest();

    }
}

Andy BoettcherAndy Boettcher

acct.TestDate__c = '1/1/2012';

ArikArik

So close....

Error: Compile Error: Illegal assignment from String to Date at line 8 column 9

@isTest
private class testAccountSimpleTrigger {

    public static testMethod void unitTestAccountSimpleTrigger() {

        Account acct = new Account();
        acct.Name = 'Test Account';
        acct.TestDate__c = '1/1/2012';


        insert acct;

        test.startTest();

        acct.TestCheckbox__c = true;
        update acct;

        test.stopTest();

    }
}

Andy BoettcherAndy Boettcher

acct.TestDate__c = Date.newInstance(2011,5,1);

This was selected as the best answer
ArikArik

you are god !!!

Starz26Starz26

Nice thread....

 

Glad to see someone willing to help so much and someone willing to learn.....

 

This is what this community is about!!!!

ArikArik

Andrew is the greatest !!!

ArikArik
Converting the variables in this trigger to the following:

Account to rethink2__Closing__c
a.TestCheckbox__c to Deliver_Earnest_Money__c

a.TestDate__c = Earnest_Money_Deadline__c

I keep getting wierd errors when I substitute the variables on the Custom Object (API Namerethink2__Closing__c)

Any Possible Help would be appreciated - I have been on it for 8 hours and have given up



trigger AccountSimpleTrigger on Account (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
    
    if(trigger.isUpdate && trigger.isBefore) {
        
        // Create empty List to hold new Events (bulk DML) 
        List<Event> lstNewEvents = new List<Event>();
        
        for(Account a : trigger.new) {
            
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
                
                // Set Start Date
                Date dteStart = a.TestDate__c;
                
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.ActivityDate = a.TestDate__c;
                newEvent.IsAllDayEvent = true;
                
                lstNewEvents.add(newEvent);
                
            }
        }
        
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
        
    }
Andy BoettcherAndy Boettcher

What is the trigger you're really working with - and what errors are you seeing?

 

-Andy

ArikArik

its the exact same one.

 

I cant get past the basic things like eof curly bracket, etc

 

I assumed I could just copy the trigger, make it work on the custom object and it just doesnt.  I could do a screen shot if you want

Andy BoettcherAndy Boettcher

Nothing to be embarassed of - this is how we all learn this stuff.  =)  Post up what you're working with and we'll parse through it.

 

-Andy

ArikArik

trigger <ErnestMoney> on rethink2__Closing__c (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
   
    if(trigger.isUpdate && trigger.isBefore) {
       
        // Create empty List to hold new Events (bulk DML)
        List<Event> lstNewEvents = new List<Event>();
       
        for(rethink2__Closing__c a : trigger.new) {
           
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
               
                // Set Start Date
                Date dteStart = a.TestDate__c;
               
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.ActivityDate = a.TestDate__c;
                newEvent.IsAllDayEvent = true;
               
                lstNewEvents.add(newEvent);
               
            }
        }
       
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
       
    }


Andy BoettcherAndy Boettcher

You're missing one "close" curly brace } at the end of the trigger - that should do it for ya.  Everything else looks good.

 

-Andy

ArikArik

hi this is the error:

 

Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1

 

trigger ErnestMoney on rethink2__Closing__c (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
   
    if(trigger.isUpdate && trigger.isBefore) {
       
        // Create empty List to hold new Events (bulk DML)
        List<Event> lstNewEvents = new List<Event>();
       
        for(rethink2__Closing__c a : trigger.new) {
           
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
               
                // Set Start Date
                Date dteStart = a.TestDate__c;
               
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.ActivityDate = a.TestDate__c;
                newEvent.IsAllDayEvent = true;
               
                lstNewEvents.add(newEvent);
               
            }
        }
       
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
       
    }

Andy BoettcherAndy Boettcher

Yup - that's exactly it.  Put a } "right curly bracket" on the last line of the trigger file.

 

trigger <ErnestMoney> on rethink2__Closing__c (before update) {

    ////////////////////////////////////
    // This trigger detects the change of the TestCheckbox field
    // and creates an Event
    ////////////////////////////////////
   
    if(trigger.isUpdate && trigger.isBefore) {
       
        // Create empty List to hold new Events (bulk DML)
        List<Event> lstNewEvents = new List<Event>();
       
        for(rethink2__Closing__c a : trigger.new) {
           
            // Determine if the checkbox is TRUE and the user has just flipped it from FALSE.
            if(a.TestCheckbox__c == true && a.TestCheckbox__c != trigger.oldMap.get(a.Id).TestCheckbox__c) {
               
                // Set Start Date
                Date dteStart = a.TestDate__c;
               
                // Create Event
                Event newEvent = new Event();
                newEvent.OwnerId = UserInfo.getUserId(); // Sets the current userId as the Event Owner
                newEvent.WhatId = a.Id; // Sets the Account as the Event's "Related To"
                newEvent.Subject = 'New Event - Some Reason';
                newEvent.ActivityDate = a.TestDate__c;
                newEvent.IsAllDayEvent = true;
               
                lstNewEvents.add(newEvent);
               
            }
        }
       
        if(lstNewEvents.size() > 0) { insert lstNewEvents; }
       
    }

// PUT IT RIGHT BELOW THIS LINE!
}

 

 

 

ArikArik

It Works !!!!!

 

Halalujua -

 

I didnt undertand because the error said it needed a bracket at the beggining not the end

 

Thank You !!!!

ArikArik

I'm Back - I  know you want to hit delete, but just look at this for 1 second - pleaseeeee:

 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

 

@isTest
private class testCICDocumentsObjectionDeadline{

    public static testMethod void unitTestCICDocumentsObjectionDeadline() {

        rethink2__Closing__c.Name = new Name();
        rethink2__Closing__c.Name = 'Test Closing';
rethink2__Closing__c.CIC_Documents_Objection_Deadline__c = Date.newInstance(2011,5,1);


        insert rethink2__Closing__c.Name;

        test.startTest();

        rethink2__Closing__c.CIC_Documents_Objection_Deadline_Checkbo__c = true;
        update rethink2__Closing__c.Name;

        test.stopTest();

    }
}

Andy BoettcherAndy Boettcher

Give this a whirl:

 

@isTest
private class testCICDocumentsObjectionDeadline{

    public static testMethod void unitTestCICDocumentsObjectionDeadline() {

        rethink2__Closing__c thisObj = new rethink2__Closing__c();
        thisObj.Name = 'Test Closing';
	thisObj.CIC_Documents_Objection_Deadline__c = Date.newInstance(2011,5,1);


        insert thisObj;

        test.startTest();

        thisObj.CIC_Documents_Objection_Deadline_Checkbo__c = true;
        update thisObj;

        test.stopTest();

    }
}

 

In APEX when you're working with records - you need to declare a variable as an instance of an object.  The "thisObj" is an example of this.  All of the work you do post-variable declaration is with the variable only - no need to reference the object again.

 

-Andy

 

ArikArik

Thank You Very very Much !!!!!