0

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.

4
  • That's where I am confused, when will this trigger. My scenario is whenever a Partner Logs into Portal it has to create a task, I am not really sure how to achieve this, can you guide me? Sorry i'm new to Salesforce so my question might be wierd.
    – Lin
    Dec 12, 2017 at 21:42
  • Your question makes sense but I'm not entirely sure how feasible it is. Does it have to create the task immediately or can you do it nightly? the easiest way I can see to trigger your code is to use scheduled apex.
    – gNerb
    Dec 12, 2017 at 21:47
  • It has to create immediately. can we do something like whenever this loginTime = Today() AND profile = 'portal user' condition is true it has to generate a task. But how can we run this class?
    – Lin
    Dec 12, 2017 at 21:49
  • You'll need a custom login page. I've updated my answer to point you in the right direction.
    – gNerb
    Dec 12, 2017 at 21:58

1 Answer 1

1

You'll need a custom login page for your community. That should allow you to react as soon as a user has authenticated.

Once you accomplish the above, your method that returns a page reference should return a page inside your community (success) or an error page/null (failure). If the page reference is a good page, you can write code to create the task prior to re-directing the user.

public PageReference login() {
    String startUrl = System.currentPageReference().getParameters().get('startURL');
    PageReference loginResult = Site.login(username, password, startUrl);

    if (loginResult.getUrl() == startUrl) {
        taskController.create_task();
    }

    return loginResult;
}

At least, that's the theory, I haven't needed to do this before so i haven't put it to the test. You'll have to be cautious with permissions as well, they can get tricky in communities.

1
  • Thanks for the help Toby, It was a Lightning page, so I had to create a custom lightning component and controller in order to achieve this.
    – Lin
    Dec 13, 2017 at 14:52

You must log in to answer this question.

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