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
Glenn BalmonteGlenn Balmonte 

Need help for Trigger Assigning Records

Hello Experts,

Need your inputs on how we can assign particular records created by this trigger to specific user. Say the record "Website Audit Report and Recommendations" will be assign to User A and the record "Optimize keywords in footer links" will be assign to User B. 

=================================================

trigger AutoCreateProductionTask on SFDC_Project__c (after insert) {

   List<Production_Task__c> newRecord = new List<Production_Task__c>(); 

   List<String> namePT = new List<String>();
   namePT.add('Website Audit Report and Recommendations');
   namePT.add('Keyword Research');
   namePT.add('Competitor Research');
   namePT.add('Run page speed loading test and make improvements');
   namePT.add('Run site on validator and fix broken links and html errors');
   namePT.add('Run crawl test and redirect any obsolete urls');
   namePT.add('Keyword mapping, meta data creation and implementation');
   namePT.add('Text content creation');
   namePT.add('Upload optimized text content, insert and highlight ketwords');
   namePT.add('Optimize keywords in footer links');
   namePT.add('Optimize alt tags of images');
   namePT.add('Create or improve HTML sitemap page');
   namePT.add('Add breadcrumbs navigation on inner pages');
   namePT.add('Create Google Places listing');
   namePT.add('URL and Directory Submission');
   namePT.add('Create Google Webmaster Tools profile');
   namePT.add('Create Google Analytics profile');
   namePT.add('Create Google Analytics Contact Forms Goal Setup');
   namePT.add('Add in AI Directory');   
   namePT.add('Create and submit xml sitemap to Google');
   namePT.add('Create and publish 1 Press Release');
   
    for (SFDC_Project__c aim : Trigger.new) {
        
        if (aim.RecordTypeID == Schema.SObjectType.SFDC_Project__c.getRecordTypeInfosByName().get('AIM Project').getRecordTypeId())
        if (aim.Product_Service__c == 'SEO - Premium'|| aim.Product_Service__c == 'SEO - Pro'|| aim.Product_Service__c == 'SEO - Starter'|| aim.Product_Service__c == 'SEO - Plus')
        
                {

          for(Integer i = 0; i < namePT.size(); i++){
            
            Production_Task__c aimTask = new Production_Task__c();
            aimTask.recordTypeId = Schema.SObjectType.Production_Task__c.getRecordTypeInfosByName().get('AIM').getRecordTypeId();
            aimTask.Name = namePT [i];
            aimTask.Due_Date__c = Date.today().addDays(30);
            aimTask.Project__c = aim.Id;
            aimTask.Instructions__c = 'test';
            
                newRecord.add(aimTask);   
            
           }
           }
            insert newRecord;
   }
   }

=================================================

Any assistance is highly appreciated. Thank you. 
Dhruv Gour 14Dhruv Gour 14
HI 

You can set OwnerId (Label - assigneeID ) of task to User you want to assign task.
Glenn BalmonteGlenn Balmonte
Hi Can you give example of that statement. Still learning APEX. :) 
Dhruv Gour 14Dhruv Gour 14
You just put following code below line "aimTask.Due_Date__c = Date.today().addDays(30);" 

//Code started
//It will query any user . You should put user's name at place of testName .​
User usr = [select id from user where name like '%testName%'];
//It will assign task to user 
aimTask.ownerID= usr.Id;


Thanks
Dhruv

 
Glenn BalmonteGlenn Balmonte
Thank you Dhruv. Appreciate your help 
Dhruv Gour 14Dhruv Gour 14
Your welcome glenn ,

Please mark this best answer if it solved your problem
Nitin Wader 21Nitin Wader 21
You need to add code line which will create conditional table segregating User A type and User B type task list. Set some flag here.

and then before you add new task record in List, you can assign it to required owner using flag value set earlier.

Hope this logic will work -

============================================================
trigger AutoCreateProductionTask on SFDC_Project__c (after insert) {

   List<Production_Task__c> newRecord = new List<Production_Task__c>(); 

   List<String> namePT = new List<String>();
  /* Your code for list population */ }

========================================
Here Create flag based segrrgeation -
Flag = False

If ( Recordtyp1,typ2,type 3) then Flag = True

============================================

/*your code for task creation*/

            aimTask.Name = namePT [i];
            aimTask.Due_Date__c = Date.today().addDays(30);
            aimTask.Project__c = aim.Id;
            aimTask.Instructions__c = 'test'
            If Flag == True
           ( aimTask.OwnerID = A.ID)
            Else 
            (aimTask.OwnerID = B.ID)

=====================================
/*Your further code*/