You need to sign in to do that
Don't have an account?
mohamed Sufiyan 8
Deactivate user and customer portal user from contact checkbox
I need to deactivate a user and portal user by clicking checkbox in contact page.
trigger activeuser on Contact (after update) {
ID contactId = [Select contactid from User where id =:UserInfo.getUserId()].contactId;
for(Contact c : Trigger.new){
if(c.isactive__c == true)
{
User u = [SELECT Id,ContactId,IsActive,IsPortalEnabled FROM User WHERE contactId =:contactId ];
u.CompanyName ='test';
u.IsActive=false;
u.IsPortalEnabled=false;
Update u;
}
}
}
am getting error
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa)
any help..
trigger activeuser on Contact (after update) {
ID contactId = [Select contactid from User where id =:UserInfo.getUserId()].contactId;
for(Contact c : Trigger.new){
if(c.isactive__c == true)
{
User u = [SELECT Id,ContactId,IsActive,IsPortalEnabled FROM User WHERE contactId =:contactId ];
u.CompanyName ='test';
u.IsActive=false;
u.IsPortalEnabled=false;
Update u;
}
}
}
am getting error
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa)
any help..
<pre>
trigger ActiveUser on Contact ( after update )
{
ContractTriggerHelper.deactivateUsers_future( Trigger.newMap.keySet() );
}
public class ContractTriggerHelper
{
@future
public static void deactivateUsers_future( List<Id> contactIds )
{
List<User> usersToUpdate = new List<User>();
for ( User user :
[ SELECT Id, IsActive
FROM User
WHERE ( Contact.IsActive__c = false
AND ContactId IN :contactIds
AND IsActive = true
)
]
)
{
user.IsActive = false;
usersToUpdate.add( user );
}
update usersToUpdate;
}
}
</pre>
All Answers
<pre>
trigger ActiveUser on Contact ( after update )
{
ContractTriggerHelper.deactivateUsers_future( Trigger.newMap.keySet() );
}
public class ContractTriggerHelper
{
@future
public static void deactivateUsers_future( List<Id> contactIds )
{
List<User> usersToUpdate = new List<User>();
for ( User user :
[ SELECT Id, IsActive
FROM User
WHERE ( Contact.IsActive__c = false
AND ContactId IN :contactIds
AND IsActive = true
)
]
)
{
user.IsActive = false;
usersToUpdate.add( user );
}
update usersToUpdate;
}
}
</pre>
DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction. However, deleting a DML operation has no restrictions.
To Avoid the MIXED DML ERROR
- Create a method that performs a DML operation on one type of sObject.
- Create a second method that uses the future annotation to manipulate a second sObject type.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htmtrigger CreateNewChangeRequest on Request__c (After Update) {
JIRA_Triggers_Activation__c activateTriggers = JIRA_Triggers_Activation__c.getInstance();
if ( activateTriggers.Activate_Triggers__c == TRUE ) {
if(system.isFuture()) return;
if(recursivecheck36.runOnce()){
for(Request__c v :trigger.new)
{
if (trigger.isUpdate)
{
ReqCallOut.updateCase(v.Id);
break;
}
}
}
}
}
@isTest
Public class Requesttest {
@isTest(seeAllData=true)
Public static void createnew(){
try{
Id chId ;
Test.StartTest();
ReqCallOut.updateCase(chid);
Test.StopTest();
}Catch(Exception e){
System.debug(' Requesttest Exception ' + e.getMessage());
}
}
trigger ActiveUser on Contact ( after update )
{ ContactTriggerHelper.deactivateUsers_future( Trigger.newMap.keySet() );
}
public class ContractTriggerHelper {
@future
public static void deactivateUsers_future( List<Id> contactIds )
{ List<User> usersToUpdate = new List<User>();
for ( User user : [ SELECT Id, IsActive FROM User WHERE ( Contact.IsActive__c = false AND ContactId IN :contactIds AND IsActive = true ) ] )
{
user.IsActive = false;
usersToUpdate.add( user );
}
update usersToUpdate;
}
}