Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I need to write a trigger:- A task should be assigned to a account on account creation if the reporting country of account owner is united states. I have written the code partially, but geting stuck on te task part. The reporting country part is a tricky one for me to handle..

trigger CreateTask on Account (before insert)
 {
    Map <ID, User> userMap = new Map<ID, User>();
    List<Account>accountInfoList = New List<Account>();
        List<Account>accountList = New List<Account>();
    Set<id>OwnerIDSet = new set<id>();
    for(Account acc : Trigger.new)
    {

           OwnerIDSet.add(acc.OwnerID); 
            accountInfoList.add(acc);
        }


      userMap = new Map<ID,User>([Select ID, Reporting_Country__c, Email, UserName from User where ID in :OwnerIDSet]);

      if (accountInfoList.size()>0)
    {
        For(Account acc:accountInfoList) 
        {
            if (acc.SDO_Billing_Country_Name__c == userMap.get(acc.OwnerID).Reporting_Country__c)
            {
                accountList.add(acc);
             }
  }
  }
   System.debug('accountList - '+accountList.size());
   List<Task> InsertTask = new List <Task>();
   if (accountList.size()>0)
  {
     For(Account acc:accountList) 
        {
     Task t = new Task();
     t.OwnerID = acc.OwnerID;
     t.subject= 'Task creation';
     t.status = 'in progress';
     t.priority  = 'high';
    // InsertTask.add
share|improve this question
add comment

2 Answers 2

I would move it to an AfterInsert trigger, and then assign the task WhatId to the relevant Account Id. Try this:

trigger CreateTask on Account (after insert)
{
    Map <ID, User> userMap = new Map<ID, User>();
    List<Account>accountInfoList = New List<Account>();
    List<Account>accountList = New List<Account>();
    Set<id>OwnerIDSet = new set<id>();

    for (Account acc : Trigger.new) {

        OwnerIDSet.add(acc.OwnerID); 
        accountInfoList.add(acc);
    }

    userMap = new Map<ID,User>([
        SELECT 
            ID, 
            Reporting_Country__c, 
            Email, 
            UserName 
        FROM 
            User 
        WHERE 
            Id IN : OwnerIDSet
    ]);

    if (accountInfoList.size()>0) {

        for (Account acc : accountInfoList) {

            if (acc.SDO_Billing_Country_Name__c == userMap.get(acc.OwnerID).Reporting_Country__c) {

                accountList.add(acc);
            }
        }
    }

    List<Task> InsertTask = new List <Task>();

    if (accountList.size()>0) {

        for (Account acc : accountList) {

        Task t = new Task();
        t.OwnerID = acc.OwnerID;
        t.subject= 'Task creation';
        t.status = 'in progress';
        t.priority  = 'high';
        t.WhatId = acc.Id

        InsertTask.add(t);
    }

    if (!InsertTask.isEmpty()) {

        insert InsertTask;
    }
}
share|improve this answer
add comment

DavinC's answer identifies the key point that you need the Account Id to be able to do this and that is not available in a "before insert" trigger but is in an "after insert" trigger.

Your question says:

if the reporting country of account owner is united states

(which isn't consistent with the code you posted) but here is how I would write that trigger:

trigger CreateTask on Account (after insert) {
    Set<Id> ownerIds = new Set<Id>();
    for (Account a : Trigger.new) {
        ownerIds.add(a.OwnerId); 
    }
    Map <Id, User> users = new Map<Id,User>([
            select Id
            from User 
            where Id in :ownerIds
            and Reporting_Country__c in ('United States', 'USA')
            ]);
    List<Task> inserts = new List<Task>();
    for (Account a : Trigger.new) {
        if (users.containsKey(a.OwnerId)) {
            inserts.add(new Task(
                    WhatId = a.Id,
                    OwnerId = a.OwnerId,
                    Subject = 'Task creation',
                    Status = 'In Progress',
                    Priority = 'High'
                    ));
        }
    }
    insert inserts;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.