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
SFDC GuestSFDC Guest 

Trigger to get LastModifiedByName from sObjects

Hi All. I have a custom object - name "Error_Log__c" and a custom text field - "Record_Id__c". Here Record_Id__c is the id of sobject Id (like opportunity, or account or contact or any object record id). When a Error_Log__c record is created then, a chatter post should be created and it should be posted to user "LastModifiedBy.Name" of sobject (account or contact or opportunity) record.

There is no relation between sobjects and Error_Log__c, but the custom text field - "Record_Id__c" is the id of sobject record id ( like account or contact or Opportunity record id, etc.).

Please let me know how to send the chatter post using dynamic soql for this. Thanks
srlawr uksrlawr uk
This is an unpleasant thing to do in Salesforce, as we don't have polymorphic ID fields.. which is why I am guessing you opted for a Text field to hold the ID?

That is one way to do it.. and then you have a couple of choices for how to determine the record type.

1) Use a formula field that checks the first three characters of the ID.... if it's 001 it's an Account, if its 003 then it's a contact, and 006 for an Opportunity - you can then use this field to set the "FROM" in your Dynamic SOQL

2) Do the same thing as above, but on the fly in Apex as you load each record...

I would personally strongly consider replacing the text field with three lookups, one to each record the Error can be linked to.. then you can use validation rules (or a trigger) to ensure that only one of them is ever populated - this giving you hard links to the record.

In your Apex, you then just need to say
 
if(error.Account__c != null) {

    // access to all the Account__r.LastModifiedDate etc.

} else if(error.Contact__c != null) {

    //access to all those related fields

}

etc..

If you must use the text field though, and assuming you have used a formula field to determine the type... it would end up looking something like
 
String queryString = 'SELECT LastModifiedBy.Name FROM ' + errorObj.Object_Type__c + ' WHERE Id = \'' + errorObj.Record_Id__c + '\'';

sObject resultRecord = Database.query(queryString);


Hopefully all that gets you someway along the road to where you need to be?! Cheers.
SFDC GuestSFDC Guest
hi srlawr,
I have wrote below trigger.
trigger ApplicationLogTrigger on Application_Log__c (after insert, after update) {
    List<SObject> ObjList = new List<SObject>();
    String ObjType ='';
    String ObjID='';
    List<Application_Log__c> LogList = new List<Application_Log__c>([Select ID, ObjectType__c, Record_Id__c from 
                                                                     Application_Log__c where ID IN : trigger.new ]);
    System.debug('LogList--->' +LogList);
    for(Application_Log__c app : LogList){
        if(app.Id!=null){
            ObjType = app.ObjectType__c;
            ObjID = app.Record_Id__c;
        }
    }
    System.debug('LogList--->' +LogList);
   ObjList =database.query('Select LastModifiedBy.Name from'+ ObjType + 'WHERE Id =:\''+ ObjID + '\''); 
   System.debug('ObjList'+ ObjList);
}

Below is the error I am getting.
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ApplicationLogTrigger caused an unexpected exception, contact your administrator: ApplicationLogTrigger: execution of AfterUpdate caused by: System.QueryException: unexpected token: Id: Trigger.ApplicationLogTrigger: line 15, column 1