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
Tammie SilberTammie Silber 

I am using the Enterprise Edition of SF. Trigger to Update Contact Object based upon change in date on Asset Object

Using the Enterprise edition of Salesforce, I need to write a trigger and am unsure of the code that I need to write. I am just starting to learn about triggers and APEX coding. I want to accomplish the following task which cannot be done via a workflow because the asset object cannot connect to a contact object field (according to salesforce support). 

When the asset field of "License Expiration Date" changes to X date, I want the Contact "Role" field to automatically update
Example is as follows: An asset is a license file in my organization. Asset License Expiration Date field is May 15, 2014. Current calendar date is May 14, 2014. Therefore the license has expired and the customer is no longer a current customer. Therefore, I need to write a trigger to automatically update the Contact Role field from "Current Customer" to "Previous User". Im not sure how to write the trigger coding to make this possible. 

Thanks!

ShashForceShashForce
Hi,

To write the trigger, we need to understand the details of how the Asset record and the Contact record are associated. If you can provide the same, I can provide sample trigger code.

Thanks,
Shashank
Tammie SilberTammie Silber
Hi Shashank,

Thanks for your quick reply! In my organization, any time we send out a license to our software, we track the product that we sold, as well as the issue and expiration date of that product under the asset object. The asset is created under the contact record.

Under the contact object, we have a field called role. When the asset field "license expiration date" is older than the current calendar date, I want the trigger to automaticlaly update the contact record field "role" from "current user" to "previous user". 

Does this make sense? Please let me know if I can provide you with any futher detail. 

Thanks for your help!


    
ShashForceShashForce
Hi Tammie,

Instead of a trigger, you should go for Scheduled APEX. Triggers are fired only when a record is edited. When the expiration date reaches today, there will not be an edit on the record which will fire the trigger. Hence, you should use scheduled APEX (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm) so that the code is invoked everyday without an edit. Here's how:

1.) Write an apex class using below code. I wrote this sample code assuming asset__c is a custom object under contacts. You may have to change it a little based on your setup:

global class changeContactRole implements Schedulable {
   global void execute(SchedulableContext SC) {
      list<contact> contactsToUpdate = new list<contact>();
      list<asset__c> assetList = [select contact__c,contact__r.Role__c,License_Expiration_Date__c from asset__c where License_Expiration_Date__c = :date.today()];
      for(asset__c asset:assetList){
              contact con = new contact();
              con.Id = asset.contact__c;
              con.Role__c = 'Previous User';
              contactsToUpdate.add(con);
      }
      update contactsToUpdate;
   }
}

2.) Undet Setup | Develop | Apex Classes, click on "Schedule APEX". On the new apex job page, give the job a name, select changeContactRole as the apex class, select frequency as weekly and select all days of the week to run it. Also, select end date as some date far in the future, and a preferred time to run the apex job daily.

3.) To write a test class for this, please use this as reference: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

4.) If you want to test the code to check if it works as expected for you, create the same as a trigger and test it out by manually changing the expiration date of a test asset record to today:

trigger updateContactRole on Asset__c (after update,after insert) {
    list<account> updates = new list<account>();
    list<asset__c> assetList = [select account__c,account__r.Role__c,License_Expiration_Date__c from asset__c where License_Expiration_Date__c = :date.today()];
      for(asset__c asset:assetList){
              account con = new account();
              con.Id = asset.account__c;
              con.Role__c = 'Previous User';
              updates.add(con);
      }
      update updates;
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank