A task has to be created automatically whenever a certain profile user logs into Portal and it should assign to one user X. The Portal is designed on lightning. I have created a class where I can get data based on Login time, but I am not able to get any task for that user(I used System Admin User for testing)
public without sharing class taskController {
@AuraEnabled
public static string create_task(){
LoginHistory tsk = [SELECT Id,UserId
FROM LoginHistory
WHERE UserId=:userinfo.getUserId()
ORDER BY LoginTime desc limit 1];
User sys_adm = [SELECT id, Email FROM User WHERE Name = 'my name']; // this is id of user who recieves task
Integer TaskCount = [SELECT count()
FROM Task
WHERE userLoginId__c =:tsk.id]; // used this coz whenever a user logs in it generates multiple times
Task task = new Task();
if(TaskCount == 0){
User portalUser = [SELECT Id, Username, ContactId, Contact.Name, Contact.AccountId, Contact.Account.Name, Contact.Account.Email__c
FROM User
WHERE Id =:UserInfo.getUserId()]; //this is the portal user login query
task.Subject = portalUser.Contact.Name + 'from ' + portalUser.Contact.Account.Name + 'has logged into the portal';
task.Description = portalUser.Contact.Name + 'from ' + portalUser.Contact.Account.Name + 'has logged into the portal at' + DateTime.now()+ '. Account e-mail: '+ portalUser.Contact.Account.Email__c;
task.OwnerId = sys_adm.Id;//task owner
task.WhatId = portalUser.Contact.AccountId; //portal login user
task.ActivityDate = Date.Today();
insert task;
}
return task.id;
}
}
I have tried to login to Portal but it did not generate any task on my User.