You need to sign in to do that
Don't have an account?
CodeHeartsSFDC
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;
}
}
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;
}
}
Try the following code it may be helpful for you:
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
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
Try the following code it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
The solution did not work.
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);
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!
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!
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
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm