-1

I am new to Salesforce. I have Account (Master) and Keys (Detail). I created a trigger on Keys. Now I need to get reference to the Account object to do some manipulations.

I tried ApexPages.currentPage().getParameters().get('Id'); but it throws System.NullPointerException: Attempt to de-reference a null object:

Any help is greatly appreciated -Thank you

  • you will always have a reference to account from key @Sasi. Just use the field name that you gave to the master-detail field to Account. e.g. for the first record Trigger.new[0].acc__c if your field name is acc__c in Keys__c – Saumya Ranjan Satapathy May 23 '16 at 13:30
1

Here is some sample code that we usually use in triggers:

Set<Id> accountIds=new Set<Id> ();
//Prepare a set of all related account that are coming to key trigger
for(Key__c key:trigger.new){
    accountIds.add(key.Account__c);
    }
// Prepare a map that maps account id to Account Record. In SOQL put all fields that you may require for your logic
Map<Id,Account> accountMap=new Map<Id,Account> ([Select id,name from Account where id in :accountIds]);
for(Key__c key:trigger.new){
    Account relatedAccount=accountMap.get(key.Account__c);
    // here onwards you have key and it's related record. So put your trigger logic here

    }

I hope that helps.Please note that this is just one way of doing things, there could be alternative approaches as well.

| improve this answer | |
0

You can query the fields you need from the account.

An example:
Account temp = [SELECT Id, CreatedDate FROM Account WHERE Keys__c = IdOfCurrentKey];

You will have to rename the fields in my query to the correct fields in your org. Also change the IdOfCurrentKey to the correct id of the key, for example key.Id

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.