salesforceapex-codeapexsalesforce-service-cloudsalesforce-chatter

Need to Access Managed Package Objects and fields in Apex trigger


i have to write an apex trigger on a Managed Packaged insttalled object and access in field for new and old values inside Apex trigger.

Please see below the piece of code that i am using in here:

trigger EmailScoreCalculator on sendgrid4sf__SendGrid_Email_Status__c (after        update ) {

 sendgrid4sf__SendGrid_Email_Status__c  oldOpp = Trigger.oldMap.get(sendgrid4sf__SendGrid_Email_Status__c.Id);

Object Name : sendgrid4sf__SendGrid_Email_Status__c(Object installed from Manage Package)

But i am getting invalid Key In Map Error:

Please Suggest is it because of Manage-package object or anything Wrong i am doing here,thanks.

Note: I am not able to see object "sendgrid4sf__SendGrid_Email_Status__c" in workbench.


Solution

  • Can you use Trigger.old.keySet() to get all keys and then iterate over the list of keys and access the values for particular key. As per my knowledge we can not use ObjectName.Id to access id of any record. So I think you are doing mistake at line

    sendgrid4sf__SendGrid_Email_Status__c oldOpp = Trigger.oldMap.get(sendgrid4sf__SendGrid_Email_Status__c.Id);

    So If you want to access all the values from the old map then use following

    for(String recordId: Trigger.oldMap.keySet()){

    sendgrid4sf__SendGrid_Email_Status__c oldOpp = Trigger.oldMap.get(recordId);

    }

    Hope this helps.