3

I'm beginner in salesforce.

So here is my question:

I have a custom field in Accounts where you can choose a day of the week. When you create a register inside an account type i would like to update an Activity with that info.

How can i do that? Is it a trigger? How would it be?

Thanks all!

3 Answers 3

-1

you can achieve by trigger and Peduo code is below.

trigger Activity on Account (after insert, after update) {

    Account myAccount=trigger.new[0]; 
    List <Account> lstAcc = new List <Account> ([select Id, name from Account where  Id =: myAccount.id and week__c = 'Monday']);
    List <Task> lstTasks = new List<Task>();
    for (Account oAcc : lstAcc)
    {
      task oTask = new Task();
      oTask.Subject = 'Other';
      oTask.Priority = 'Normal';
      oTask.WhatId = oAcc.Id;

     lstTasks.add(oTask);
    }
    upsert lstTasks;

}
0
5

There is a few things wrong with Ashas answer:

  • It is not handling multiple accounts being inserted or updated. It only works on the first one in the list
  • It is doing a SOQL query that is not needed

See the code below for a modified version of the code that will handle bulk creation of Accounts and will create a task for them all

trigger AccountAfter on Account (after insert) { // Only firing on accounts being inserted

    List <Task> lstTasks = new List<Task>();
    for (Account acc : Trigger.new) {
        if (acc.Day__c == 'Monday') {

            lstTasks.add(new Task(
                        Subject = 'Task for ' + acc.Day__c, 
                        Priority = 'Normal', 
                        WhatId = acc.Id));
        }
    }
    if (lstTasks.size() > 0) {
        insert lstTasks;
    }

}
0

It is doing nothing xD :

trigger CreateActivity on Account (after insert){

    List<Event> EventList = new List<Event>();

    for(Account acc : Trigger.new){
    EventList.add(new Event(
            OwnerId = acc.CreatedById,
            WhatId = acc.Id,
            Tipo_de_viaje__c = 'Recogida de Alimentos'));
    }
    if (EventList.size() > 0) {
        insert EventList;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .