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
Matt Blatnik 8Matt Blatnik 8 

Relating multiple records and assigning tasks

Hi, 

I have two custom objects: Library and employees. Each employee record represents information about a user in the company. One of the fields is department.

In library i publish useful articles. 

How can I make salesforce search and assign tasks to employees to read a publisehd article when i select department "marketing" in the library object. Same field and value exists in employee object.

to sum up - when I publish an article for marketing I want each employee in markteing to get a task to read the article.
CongnizentCongnizent
Hi Matt ,

You have to write an apex trigger on library object like 

trigger test on library__c (before insert) {
    
        if(trigger.new.get(0).department__c !=null)
            list<employ__c> listOfEmploy=[Select ID from employ__c where Department__c= :trigger.new.get(0).department__c ];
            
        List<Task>  taskToInsert = new List<Task>();
        for(employ__c aa : listOfEmploy)    
        {
            if(aa.department__c !=trigger.oldMap().get(aa.ID) )
            {
                 Task T = new Task();
                T.Type = 'Email';
                T.Description = ''; //string
                T.OwnerId = aa.ID; //user id
                T.WhatId = ''; //record id
                taskToInsert.add(T);
                
                
            }
        }
    
}
if(taskToInsert.size()>0)
insert taskToInsert;



There may be some syntax errors . But it will definately  solve your problem.
Feel free to ask any query

Thanks
Dhruv
 
Suman Kumari 76Suman Kumari 76
Hi I need help