+ Start a Discussion
mskudlarekmskudlarek 

Error Message on Apex Trigger

Still pretty new to Apex Triggers, but here is what I am trying to do.

I need the custom field Related_User__C on the custom object Activity__C to autopopulate with the current user's username. Here is what I've written:


trigger SetRelatedUser on Activity__c (before insert) {
    List <user> userid = UserInfo.getUserId();
        if (Activity__c. Related_User__c == null) 
          {
             Activity__c. Related_User__c = userid;                
          }  
}


I keep getting this error though...

Error: Compile Error: Illegal assignment from String to LIST<User> at line 2 column 5

Any help would be greatly appreciated. 

Thanks.

 

SLockardSLockard

That's because userid is a string, so you can't set that equal to a list. Try this:

 

trigger SetRelatedUser on Activity__c (before insert) { 
String userid = UserInfo.getUserId();
for (Activity__c a : Trigger.new) {
if (a.Related_User__c == null && userid != null) {
a.Related_User__c = userid;
}
}
}

 

mskudlarekmskudlarek

Thanks for your reply!

 

Now I'm getting this error:

 

Compile Error: Expression cannot be assigned at line -1 column -1

SLockardSLockard

Not sure about that, but I just realized you had spaces before the field names ... I edited my previous post above, so try it without those extra spaces.

mskudlarekmskudlarek

Tried it without spaces, still no dice. Thank you anyway!

SLockardSLockard

Well, sorry about that, my mind was temporarliy turned off. You need to assign the Activity__c to an actual variable instead of just using the object name.

Here ya go:

 

trigger SetRelatedUser on Activity__c (before insert) {
    String userid = UserInfo.getUserId();
    for (Activity__c a : Trigger.new)
    {
	if (a.Related_User__c == null && userid != null) 
	{
		a.Related_User__c = userid;                
	}
    }
}