You need to sign in to do that
Don't have an account?
Sisodia Saurabh
Future method cannot be called from a future or batch method: Error
Hi,
I am getting this error while I am trying to update contact which are associated to user objects.
trigger CommunityLoginOnContactTrigger on User (after update) {
for(User u: trigger.new)
{
system.debug('USer trigger got triggered');
if(system.isFuture()) return;
if(u.ContactId != null)
{
if(u.LastLoginDate__c != null)
{
UpdateContactFromTestUser.updateContacts(u.ContactId, u.LastLoginDate__c);
}
}
}
}
global class UpdateContactFromTestUser {
@future
public static void updateContacts(String userId, Datetime Lastlogin) {
if (userId!=null && userId != '') {
Contact c = [ Select Last_Community_Login__c,Community_Category__c from Contact where id =:userId];
if(c.Last_Community_Login__c != Lastlogin)
{
c.Last_Community_Login__c = Lastlogin;
update c;
}
}
}
}
I am getting this error while I am trying to update contact which are associated to user objects.
trigger CommunityLoginOnContactTrigger on User (after update) {
for(User u: trigger.new)
{
system.debug('USer trigger got triggered');
if(system.isFuture()) return;
if(u.ContactId != null)
{
if(u.LastLoginDate__c != null)
{
UpdateContactFromTestUser.updateContacts(u.ContactId, u.LastLoginDate__c);
}
}
}
}
global class UpdateContactFromTestUser {
@future
public static void updateContacts(String userId, Datetime Lastlogin) {
if (userId!=null && userId != '') {
Contact c = [ Select Last_Community_Login__c,Community_Category__c from Contact where id =:userId];
if(c.Last_Community_Login__c != Lastlogin)
{
c.Last_Community_Login__c = Lastlogin;
update c;
}
}
}
}
As per Salesforce standards you cannot call Future method from a future context/method.
Batch Apex runs in a Future context internally so you cannot call updateContacts method which is annotated with @future.
Hope this helps you..
Thanks for replying.
Scenario: I am copying field data from User to contact using trigger(after update) on User. it works fine but when I am deactivating the user I am getting below error:
"MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User"
Somewhere I read to overcome above error use @future but you are correct future cannot be called from Batch, as batch itself is future/Async operation.
cutting it in short I need solution of below scenario:
In my org 1 scheduled batch job runs which copy the LastLogin to custom field "LastLoginDate__c" on User object. Now I have to copy "LastLoginDate__c" values of User object to related contact's custom field "Community_LastLogin__c".
Since you are using Batch apex you cannot call @future method, the only option that will solve yur issue is, call a Queuable Apex class from your Batch which does the DML operation.
Before that let me know what all DML operations you are performing in your Batch Apex class, if you can share the code then l can help you out quickly. Thanks
Thanks for replying. I needed to provide solution asap so I wrote different Batch apex with scheduler to copy User objecct's "LastLoginDate__c" to Contact's "Community_LastLogin__c".
Thanks.