0

I need some help, this is my trigger. i have created a custom field on the user object called lastlogindate__c . i am trying to get the value from lastlogindate populated in the lastlogindate__c

i have go to setup -> users-> trigger -> new (copied the following code in below) but i get this error message "Error: Compile Error: unexpected token: public at line 1 column 0"

    public void ResetlastLogin_Update(List<User> oldUsers, List<User> newUsers) {

System.debug('ResetlastLogin_Update: entering trigger');

List<Id> idsToUpdate = new List<Id>();

for (integer i=0; i<newUsers.size(); i++) {
  User newVals = newUsers[i];
  User oldVals = oldUsers[i];

  if (newVals.lastlogindate != oldVals.lastlogindate__c) {
          idsToUpdate.add(newVals.Id);
  }
}
System.debug('Ids to Update: ' + idsToUpdate.size());

if (idsToUpdate.size() > 0) {

  List<User> usersToUpdate = [SELECT Id, lastlogindate, lastlogindate__c FROM User WHERE Id IN :idsToUpdate];
  for (User u : usersToUpdate) {

  if (u.lastlogindate__c == NULL ) {

     u.lastlogindate__c = u.lastlogindate ;
    }
   }
  update usersToUpdate;

}

System.debug('ResetlastLogin_Update_Update: exiting trigger');}
1
  • 3
    That's a function that belongs in a class (and a poorly written one at that). You should probably learn more about how to write triggers than borrowing code that you're not sure how to use.
    – sfdcfox
    Jul 15, 2014 at 20:06

1 Answer 1

2

I am not sure if you know much about SQL, but triggers in SQL and Salesforce work similarly. As the name suggest, a trigger happens when it is "triggered" by something.

If you look in the trigger introductions for Salesforce (link here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm )

Quote from the page "Apex triggers can be invoked through the use of triggers. A trigger is Apex code that executes before or after the following types of operations: insert update delete merge upsert undelete"

So then a basic trigger should firstly give the instruction of on what object this trigger respond from, and before or after what kind of event.

EG:

below is a trigger I wrote to update a Monthly Target custom field when a Target Value custom object has a new record created/inserted or updated. The first line, as you can see, starts with trigger, which states that this is a trigger, and then the name of the trigger (I call it UpdateAccountMonthlyTarget - you name yours whatever you like).

Then I state on which object will trigger the trigger. In this case, the two relevant objects are Account and the custom object Target Value. As I stated before, I wanted the trigger to occur when the target value is inserted or updated, so I say on "Target_value__c", using the API name of the custom object. Lastly, as you can see, I state "After insert, After update" because I am stating WHEN I want the trigger to happen.

trigger UpdateAccountMonthlyTarget on Target_Value__c (after insert, after update) {

// create a collection of accounts for saving new Monthly Targets

List accountlist = new List();

//create month and year variables

Date thisDay = Date.today();

Integer Month = thisDay.month();

Integer Year = thisDay.year();

// iterate through new Target_Value record created

for (Target_Value__c tv : trigger.new) {

      if ((tv.Year__c==thisDay.year()) && (tv.Month__c==thisDay.month())) {

      Account acc = new Account (id = tv.Related_Account__c, Monthly_Target__c =
      tv.Current_Month_Target__c);

      accountlist.add(acc);

      } 

      }

    update accountlist;   

}

There is a lot more to triggers, but this, and the link above, hopefully will give you a start to think about how to do this. Another website I recommend, and this guy has done some excellent job on teaching people about triggers, is http://www.sfdc99.com

One other place to start is Salesforce's own tutorial: https://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_qs_trigger.htm|StartTopic=Content%2Fapex_qs_trigger.htm|SkinName=webhelp

I hope these will start you off on the right track. i am a beginner as well, still learning Apex myself

You must log in to answer this question.

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