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
Marypearl ParnellMarypearl Parnell 

Apex trigger - Travel Request that isn't exempt

May I get some assitance with my Apex Trigger?
The trigger should fire if the Travel Request record isn't an exemption. This is indicated by a check box:
 
trigger CreateTask on Travel_Request__c (before insert) 
{
list<task> listtask = new list<task>();
    for (travel request = trigger.new)
    {if (Exemption == 'True')
    {task NewTask = new task();
    NewTask.subject = 'Create Call Report';
    NewTask.duedate = system.today().adddays(7);
    NewTask.whatID = Id;
    listtask.add(NewTask);
    }
    
    }
if (listtask.size()>0)
insert listtask;
}

 
Raj VakatiRaj Vakati
Hi Parnell ,

Code is here 
trigger CreateTask on Travel_Request__c (before insert) 
{
list<task> listtask = new list<task>();
for (Travel_Request__c req : trigger.new)
{
if (req.Exemption__c == true){
	task NewTask = new task();
    NewTask.subject = 'Create Call Report';
    NewTask.duedate = system.today().adddays(7);
    NewTask.whatID = Id;
    listtask.add(NewTask);
}

}
if (listtask.size()>0)
{
insert listtask;
}
}


 
VamsiVamsi
Hi,

Please modify as below 
 
trigger CreateTask on Travel_Request__c (before insert) 
{
list<task> listtask = new list<task>();
    for (travel request : trigger.new)
    {
     if (request.Exemption__c == 'True')  // Please make sure that Exemption__c is the API name of the field labelled "Exemption " in Travel request object.
    {
      task NewTask = new task();
      NewTask.subject = 'Create Call Report';
      NewTask.duedate = system.today().adddays(7);
      NewTask.whatID = Id;  /*please make sure to provide appropriate one WhoID refers to people things.  So that would be typically a Lead ID or a Contact ID
WhatID refers to object type things.  That would typically be an Account ID or an Opportunity ID */

      listtask.add(NewTask);
    }
    
    }
if (listtask.size()>0)
insert listtask;
}

To provide a particular contact or lead or opp or account id - please retrieve that particular record out side of for loop. For example I am retrieving coontact.

ID conid = [select id from contact where Lastname = 'xyz' limit 1].id; 

Now replace  "NewTask.whatID = Id;" with 
NewTask.whatID = conid ;

Please mark as best answer if the above helps..!!!

 
Shruti SShruti S
There is no need to write lengthy codes to achieve this. It can be achieved with just point and click. You can easily do this with the help of Processes. Here are few steps on how you should be setting up your Process - User-added image

User-added image

User-added image

Feel free to ask if you have any doubts.
Marypearl ParnellMarypearl Parnell
I guess it does make sense to use Process Builder in this instance. You'd think I'd remember that as much as I use it.
Thanks to you all for your input!
Shruti SShruti S
So happy to help! Yes, these days a lot of them can be solved using Processes.

Do let us know how it went.
Please feel free to close the thread by marking a Best Answer to help someone else in the same quandary.

Thanks,
Shruti
hiwat16130 bondehiwat16130 bonde
Generate the break some time to get appreciating the purchasers that are true to the business enterprise by way of submitting these folks your unique family vacation unit card.san juan (https://www.tripindicator.com/san-juan-activities/1/903/N.html) This tends to exhibit your prospects this its business enterprise makes a difference and you simply weren't able to currently have applied 12 months without the need of them. By way of bothering so that you can give a vacation unit card that is definitely handwritten, your prospects is going to realise that you actually decide to put quite a few assumed plus efforts on the plan.