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
rakesh muppirirakesh 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) { }

}

GulatiGulati

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.

Bindhyachal Kumar SinghBindhyachal Kumar Singh

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.

 

 

GulatiGulati
Thanks BK, for adding valuable inputs to my comment. I appreciate that.
Vinit_KumarVinit_Kumar

Agreed with BK,

 

you should write bulkified code and follow all the best practices of salesforce.

 

 

rakesh muppirirakesh muppiri
I tried but it is giving error like this Error: Compile Error: Variable does not exist:
GulatiGulati
Could you please post your code here and the specify the line in which you are getting this error.
rakesh muppirirakesh muppiri
trigger myPaymentTrigger on Payment__c (before delete, after insert, after update) { set accids=new set(); for(Payment__c p: trigger.new){ accids.add(p.Account); } map pmap= new map([SELECT id,name FROM Account WHERE id IN: accids]); //List obj =new List(); // Account a = new Account(); //When adding new payments or updating existing payments if(trigger.isInsert || trigger.isUpdate){ Account a = pmap.get(p.Account); a.= a.id; //sObject[] obj1 = trigger.new; //obj = trigger.new; //sObject s = [SELECT Accountlink__c FROM Payment__c WHERE Payment__c.Id=:obj[0].Id]; //a = obj.Account; //a.T_Total__c = a.T_Total__c + obj[0].First_Amount__c; update a; } if (trigger.isDelete) { } }
Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Rakesh

 

Use following modified code:

 

trigger myPaymentTrigger on Payment__c (before delete, after insert, after update) {
set<id> accids=new set<id>();

if(trigger.isInsert || trigger.isUpdate){
for(Payment__c p: trigger.new){
accids.add(p.Account__c);
}
map<id,Account> pmap= new map<Id,Account>([SELECT id,name FROM Account WHERE id IN: accids]);
  // Here you have to give API Name for lookupfield to Account and Account can not be a field API Name so, //you need to give correct API Name.
 Account a = pmap.get(p.Account__c);  
 a = a.id;
}

if (trigger.isDelete) {
}
}

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.

Umarani Akula 2Umarani Akula 2
Can you set the Parent Account ID for some of the SF records
Umarani Akula 2Umarani Akula 2
how to get the Parent Account ID for some of SF records on Opportunity