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
CodeHeartsSFDCCodeHeartsSFDC 

Not able to update parent object fields

Requirement: I have 2 objects "Contact" and a custom object "OrderApi__Subscription__c". When the custom object record is inserted/updated, I want to update the certain information present on custom object record on Contact object when a lookup field on custom object = 'ABC'

Below is the trigger I wrote which is not giving me any error, but it is not updating the fields on contact

trigger updateContact on OrderApi__Subscription__c (after insert, after update) {

    Set<String> contactIds = new Set<String>();
    List<Contact> conupdateList = new List<Contact>();
    Integer count = 0;
    
    for(OrderApi__Subscription__c sub: Trigger.new) {
    contactIds.add(sub.OrderApi__Contact__c);
    }
    
    List<Contact> contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds];
    
    List<OrderApi__Subscription__c> subList = [Select id, OrderApi__Status__c , OrderApi__Item__c , OrderApi__Item__r.Name, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Current_Term_Start_Date__c, OrderApi__Current_Term_End_Date__c from OrderApi__Subscription__c where id IN: contactIds];
    
    List<Contact> contactList2 = new List<Contact>();
    for(OrderApi__Subscription__c sub: subList) {
    if(sub.OrderApi__Item__r.Name == 'ABC') {
    count++;
    }
    }
    
    for(Contact con:contactList) {
    OrderApi__Subscription__c membership = new OrderApi__Subscription__c();
    con.Chapter_Member_Status__c = membership.OrderApi__Status__c;
    String activatedDate = string.valueof(membership.OrderApi__Activated_Date__c);
    con.Chapter_Member_Activated_Date__c = activatedDate;
    String paidThroughDate = string.valueof(membership.OrderApi__Paid_Through_Date__c);
    con.Member_Paid_Through_Date__c = paidThroughDate;
    String termEndDate = string.valueof(membership.OrderApi__Current_Term_End_Date__c);
    con.Member_Term_End_Date__c = termEndDate;
    String termStartDate = string.valueof(membership.OrderApi__Current_Term_Start_Date__c);
    con.Member_Term_Start_Date__c = termStartDate;
    contactList2.add(con);
    }
    
    if(count > 0) {
    update contactList2;
    }   
}

 
Best Answer chosen by CodeHeartsSFDC
Ajay K DubediAjay K Dubedi
Hi,

Try the following code it may be helpful for you:
trigger updateContact on OrderApi__Subscription__c (after insert, after update) {

    Set<Id> contactIds = new Set<Id>();
    List<Contact> contactList = new List<Contact>();
    Map<Id,Contact> conIdVSContactMap=new Map<Id,Contact>();
    List<Contact> contactList1 = new List<Contact>();
    Integer count = 0;
    
    for(OrderApi__Subscription__c sub: Trigger.new) {
    contactIds.add(sub.ContactId);
    }
    
    if(contactIds.size()>0){
    contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds LIMIT 10000];
    }
    if(contactList.size()>0){
        for(Contact con:contactList){
                conIdVSContactMap.put(con.Id,con);
        }
    }
    if(conIdVSContactMap.size()>0){
    for(OrderApi__Subscription__c sub:Trigger.new){
        if(sub.OrderApi__Item__r.Name == 'ABC' && conIdVSContactMap.containsKey(sub.ContactId){
            Contact newCon=conIdVSContactMap.get(sub.ContactId);
            newCon.Chapter_Member_Status__c=sub.OrderApi__Status__c;
            newCon.Chapter_Member_Activated_Date__c=string.valueof(sub.OrderApi__Activated_Date__c);
            newCon.Member_Paid_Through_Date__c=string.valueof(sub.OrderApi__Paid_Through_Date__c);
            newCon.Member_Term_End_Date__c=string.valueof(sub.OrderApi__Current_Term_End_Date__c);
            newCon.Member_Term_Start_Date__c=string.valueof(sub.OrderApi__Current_Term_Start_Date__c);
            contactList1.add(newCon);
        }
    }
    }
    
    if(contactList1.size()>0) {
    update contactList1;
    }   
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

AbhimanyuAbhimanyu
Hi,

Replace 
List<OrderApi__Subscription__c> subList = [Select id, OrderApi__Status__c , OrderApi__Item__c , OrderApi__Item__r.Name, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Current_Term_Start_Date__c, OrderApi__Current_Term_End_Date__c from OrderApi__Subscription__c where id IN: contactIds];

with 
List<OrderApi__Subscription__c> subList = [Select id, OrderApi__Status__c , OrderApi__Item__c , OrderApi__Item__r.Name, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Current_Term_Start_Date__c, OrderApi__Current_Term_End_Date__c from OrderApi__Subscription__c where id IN: Trigger.new];

Thanks
Ajay K DubediAjay K Dubedi
Hi,

Try the following code it may be helpful for you:
trigger updateContact on OrderApi__Subscription__c (after insert, after update) {

    Set<Id> contactIds = new Set<Id>();
    List<Contact> contactList = new List<Contact>();
    Map<Id,Contact> conIdVSContactMap=new Map<Id,Contact>();
    List<Contact> contactList1 = new List<Contact>();
    Integer count = 0;
    
    for(OrderApi__Subscription__c sub: Trigger.new) {
    contactIds.add(sub.ContactId);
    }
    
    if(contactIds.size()>0){
    contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds LIMIT 10000];
    }
    if(contactList.size()>0){
        for(Contact con:contactList){
                conIdVSContactMap.put(con.Id,con);
        }
    }
    if(conIdVSContactMap.size()>0){
    for(OrderApi__Subscription__c sub:Trigger.new){
        if(sub.OrderApi__Item__r.Name == 'ABC' && conIdVSContactMap.containsKey(sub.ContactId){
            Contact newCon=conIdVSContactMap.get(sub.ContactId);
            newCon.Chapter_Member_Status__c=sub.OrderApi__Status__c;
            newCon.Chapter_Member_Activated_Date__c=string.valueof(sub.OrderApi__Activated_Date__c);
            newCon.Member_Paid_Through_Date__c=string.valueof(sub.OrderApi__Paid_Through_Date__c);
            newCon.Member_Term_End_Date__c=string.valueof(sub.OrderApi__Current_Term_End_Date__c);
            newCon.Member_Term_Start_Date__c=string.valueof(sub.OrderApi__Current_Term_Start_Date__c);
            contactList1.add(newCon);
        }
    }
    }
    
    if(contactList1.size()>0) {
    update contactList1;
    }   
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer
CodeHeartsSFDCCodeHeartsSFDC
@Abhimanyu

The solution did not work.
CodeHeartsSFDCCodeHeartsSFDC
@Ajay Dubedi

The code saved after I made the below changes marked in bold, but it did not work. The contact is not updating with the information from subscription :(

 contactIds.add(sub.OrderApi__Contact__c);

 if(sub.OrderApi__Item__r.Name == 'Collegiate' && conIdVSContactMap.containsKey(sub.OrderApi__Contact__c)){

Contact newCon=conIdVSContactMap.get(sub.OrderApi__Contact__c);


 
CodeHeartsSFDCCodeHeartsSFDC
@Ajay Dubedi

It worked with your solution, the ABC condition was the issue. It is not recognizing the value correctly which is causing the issue. sub.OrderApi__Item__r.Name == 'ABC'. Thank you so much!
AbhimanyuAbhimanyu
Is Trigger Active?
CodeHeartsSFDCCodeHeartsSFDC
@Ajay Dubedi

Do you have ay idea why it doesn't work when I add the condition? It works without the condition. Can you please help. Thanks!
AbhimanyuAbhimanyu
Just add the debug log to see what name is being saved.
CodeHeartsSFDCCodeHeartsSFDC
@Anil Dubedi @Abhimanyu

I have another requirement. Help would be highly appreciated!! Thank you

Requirement: I have 2 objects "Account" and custom object "Api__Subscription__c". The custom object has a Term_end_date__c field. When the custom object record is inserted/updated, I want to update certain information on Account from custom object record having the latest/most recent Term_end_date__c field value.

I wrote the below code, which is giving me an error on the highlighted line. Error pasted below. Also I'm not sure if I'm updating the Account with the right values from record having the most recent term end date.

Error: Compile Error:
Term_End_Date__c FROM Api__Subscription__c
^

ERROR at Row:1:Column:200
Didn't understand relationship 'Api__Subscription__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 11 column 3
  1. trigger updateAccount on Api__Subscription__c (after insert, after update) {
  2.  
  3.     Map<ID, Account> parentAcc; 
  4.     List<Id> listIds = new List<Id>();
  5.     List<Account> accountList = new List<Account>();
  6.     
  7.     for (Api__Subscription__c childObj : Trigger.new) {
  8.     listIds.add(childObj.Api__Account__c);
  9.     }
  10.     
  11.     parentAcc = new Map<Id, Account>([SELECT id, Term_Start_Date__c, Term_End_Date__c, Status__c, Activated_Date__c, (SELECT ID, Term_End_Date__c FROM Api__Subscription__r WHERE Term_End_Date__c != NULL Order By Term_End_Date__c desc) FROM Account WHERE ID IN :listIds]);
  12.     
  13.     for(Api__Subscription__c sub:Trigger.new){
  14.     
  15.             Account newAcc = parentAcc[0].get(sub.Api__Account__c);
  16.  
  17.             //newAcc.Status__c=sub[0].Status__c;
  18.  
  19.             newAcc.Activated_Date__c=sub[0].Activated_Date__c;
  20.  
  21.             newAcc.Term_End_Date__c=sub[0].Term_End_Date__c;
  22.  
  23.             newAcc.Term_Start_Date__c=sub[0].Term_Start_Date__c;
  24.  
  25.             accountList.add(newAcc);
  26.         
  27.     }
  28.         
  29.     if(accountList.size()>0) {
  30.  
  31.     update accountList;
  32.  
  33.     }   
  34.     
  35. }

 
AbhimanyuAbhimanyu
Check the child relationship name on Api__Account__c field, and I assume it is Api__Subscriptions__r but still check it in Field Settings

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm