You need to sign in to do that
Don't have an account?
rakesh muppiri
how to get parentID or parent of child object
Parent: Account
Child: Payment__c
trigger myPaymentTrigger on Payment__c (before delete, before insert, after update) {
List<Payment__c> obj =new List<Payment__c>();
//Account a = new Account();
if(trigger.isInsert || trigger.isUpdate){
obj = trigger.new;
Account a = ; //Here i want to write Query that should return Parent Account of Payment__c
}
if (trigger.isDelete) { }
}
I am not very good at triggers. You need to create a map of account then get the account id you can try this one.
trigger myPaymentTrigger on Payment__c (before delete, before insert, after update) {
set<id> accids=new set<id>
for(Payment__c p: trigger.new){
accids.add(p.Account);
}
map<id.Account> pmap= new map<id,Account>([SELECT id,name FROM Account WHERE id IN: accids]);
if(trigger.isInsert || trigger.isUpdate){
Account a = pmap.get(p.Account); // variable a will hold the complete record of Account
a.Custom_field__= a.id; // this will will you the id of the record in a custom_field__c
}
if (trigger.isDelete) { }
}
p.Account= where Account is name of the lookup field created on child object.
Hi Rakesh,
You can use following code :
Account__c is the field on Payment__c object whuch is lookup to Account. I am using list instead of Sobject Account because if you insert/update/delete on/from Payment__c using dataloader (Onetime multiple data can be inserted/updated/deleted) then it also work.
trigger myPaymentTrigger on Payment__c (before delete, before insert, after update) {
List<Payment__c> obj =new List<Payment__c>();
Set<id> accids = new set<id>();
if(trigger.isInsert || trigger.isUpdate){
for(Payment__c pay: Trigger.New){
obj.add(pay);
accids.add(pay.Account__c) // Here Field Account__c is a lookup to Account.
}
// Here you get Account for a payment
List<Account> acclist = [select id,Name from Account where ID IN :accids];}
if (trigger.isDelete) { }
}
}
acclist is list of account. If you insert one payment then acclist contains on account for those Payment__c.
If this is the solution for your issues then please mark as solution and it helps other who faces similar type of issues.
Agreed with BK,
you should write bulkified code and follow all the best practices of salesforce.
Hi Rakesh
Use following modified code:
If Accountlink__c is your lookup field on payment to Account then use Accountlink__c instead of Account__c.
If this is your solution then please mark it as a solution and it helps other for similar issues.