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
cdavis@elt.comcdavis@elt.com 

Future? Im a newbie

trigger Touch_Imp on User (after insert) {
if(trigger.isInsert){
    Set<String> usercontactid = new Set<String>();
    for (User u : Trigger.new) {
        usercontactid.add(u.contactid);
    }
    if([SELECT COUNT() FROM implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid]>0){
        Implementation__c[] updateimp = [SELECT id from Implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid];
        Database.update(updateimp);
    }
}
}

 Above is my current code. But I get an error message about updaing a setup object and non-setup object at the same time when I try to create a user record now. I believe I need to use the future command to fix this, but I have no idea how. I'm pretty new to apex coding and I'm really hoping you guys can give me a step by step on how to accomplish this task.

 

More Info (background on what I'm trying to do):

  I'm trying to get my triggers on my Imp object to fire after my new user is created. The only way I know how to do that is to update the Imp record after the User record is updated. If there is a better way to accomplish this, please let me know! If I can supply any more needed information, just ask and I'll see what I can stir-up.

 

Thanks for reading, and hopefully helping :smileyhappy:

Best Answer chosen by Admin (Salesforce Developers) 
Sandeep001Sandeep001

Try this-

 

trigger Touch_Imp on User (after insert) {

    Set<String> usercontactid = new Set<String>();
    for (User u : Trigger.new) {
        usercontactid.add(u.contactid);
   }
   
    if([SELECT COUNT() FROM implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid]>0)                 
UpdateNonSetupObjects.updateContact(usercontactid); } public class UpdateNonSetupObjects { @future public static void updateContact(Set<String> usercontactid ) { Implementation__c[] updateimp = [SELECT id from Implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid];
Database.update(updateimp);; } }

 Cheers,

Sandeep

All Answers

Starz26Starz26

Couple of questions:

 

1. What is the exact error

2. How is there Implementation_contact__c with the ID of the new user if they did not exist before?

 

Comment:

 

1. The correct way to reference the field in the select statement is:

 

if([SELECT COUNT() FROM implementation__c WHERE implementation_contact__c IN :usercontactid]>0)

and

 

 

[SELECT id from Implementation__c WHERE implementation_contact__c IN :usercontactid];



cdavis@elt.comcdavis@elt.com

1. Here is the exact error

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Touch_Imp caused an unexpected exception, contact your administrator: Touch_Imp: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a02K0000000wOoaIAE; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Implementation__c, original object: User: []: Trigger.Touch_Imp: line 9, column 1

 2. These are customer portal users, they were contacts who then became users once enabled. Which is why I am comparing the contactid on the user record to the imp contact.

 

I hope this answers your questions.

 

Also thank you for your comments, my SELECT statements were a little redundant there.

Starz26Starz26

Does THIS Help?

cdavis@elt.comcdavis@elt.com

does what help? i made your changes from the comments before i got the error message above.

Starz26Starz26

Click on THIS in the above post.

cdavis@elt.comcdavis@elt.com

oh, sorry

 

But when I do I get this: An Unexpected Error has occurred. 

Starz26Starz26

I get that to a lot on these forums.....

 

to fix simply, click in the url address and press enter or glick go. F5 to reload may work too.

Sandeep001Sandeep001

Try this-

 

trigger Touch_Imp on User (after insert) {

    Set<String> usercontactid = new Set<String>();
    for (User u : Trigger.new) {
        usercontactid.add(u.contactid);
   }
   
    if([SELECT COUNT() FROM implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid]>0)                 
UpdateNonSetupObjects.updateContact(usercontactid); } public class UpdateNonSetupObjects { @future public static void updateContact(Set<String> usercontactid ) { Implementation__c[] updateimp = [SELECT id from Implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid];
Database.update(updateimp);; } }

 Cheers,

Sandeep

This was selected as the best answer
cdavis@elt.comcdavis@elt.com

Thank you for the code, but I dont understand the Public Class part. How do I use that? it is seperate from the triger correct? and if so where does it go?

cdavis@elt.comcdavis@elt.com

I found the Apex Classes section and added it there. Thank you for the help, at first glance it appears to work. Let me do some more testing in my sandbox here and I will see if all is well.

 

Thanks so much for giving me the full code!!

Sandeep001Sandeep001

Yes, you need to add the public class as new apex class. Njoy and keep learninig :)

cdavis@elt.comcdavis@elt.com

I'm having trouble with code coverage and getting this into my production org. Can someone maybe help me get this through? So far any triggers I have gotten up there I have just guessed on. and that really isn't good for my org I'm sure.

cdavis@elt.comcdavis@elt.com

I was able to make up enough to get it through. =)

 

Thanks again for all the help!!!!