I was able to get your code to work by changing Set to List. The working code is below. Thank you so much for helping me. I really appreciate it.
trigger updateopppo on PO_Log__c ( after update)
{
List<Opportunity> oppIds = new List<Opportunity>();
for ( PO_Log__c obj: trigger.new ){
if(obj.Opportunity__c!=NULL){
opportunity o=new opportunity();
o.id=obj.Opportunity__c;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
}
update oppIds;
}
Yes. You must create a "After update" trigger on child object. check the old field value and new field value of "PO_Log__r" and if it’s different then get the parent record id (Opportunity Id). Query on opportunity with that this id and get the record and update.
But you can achieve this simple requirement using Process builder. Create a process builder on child object with criterion as on update of the field and in action update the opportunity field. This is more recommended approach than trigger. If this helped you in any ways please mark as best answer
I am getting an error with my trigger. Error: Compile Error: A value cannot be stored to polgid__c in type Opportunity at line 7 column 54. I can't seem to get it to work. Any thoughts?
trigger updateopppo on PO_Log__c ( after update)
{
Set<Id> oppIds = new Set<Id>();
for ( PO_Log__c evnt : [SELECT Id FROM PO_Log__c
WHERE Id IN :Trigger.new] ) oppIds.add( evnt.Id );
List<Opportunity> opptoUpdate = [SELECT polgid__c FROM Opportunity WHERE Id IN :oppIds];
for ( Opportunity opp: opptoUpdate ) Opportunity.polgid__c = oppIds;
update opptoUpdate;
}
trigger updateopppo on PO_Log__c ( after update)
{
Set<Id> oppIds = new Set<Id>();
for ( PO_Log__c obj: trigger.new ){
opportunity o=new opportunity();
o.id=obj.<PUT_HERE_THE_NAME_OF_THE_CUSTOM_FIELD_WHICH_CONTAINS_ASSOCIATED_OPPORTUNITY_ID>;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
update oppIds;
}
Note: Your opportunity record might have more than one child, in that case you will have to modify your code in accordance to how you want to store those IDs and where you want to store them.
I want the Id of PO_Log__c record associated with the opportunity to go in the polgid__c on the opportunity. This code seems like we are posting the opportunity id to the opportunity field polgid__c. Thank you for your help
trigger updateopppo on PO_Log__c ( after update)
{
Set<Id> oppIds = new Set<Id>();
for ( PO_Log__c obj: trigger.new ){
opportunity o=new opportunity();
<b>o.id=obj<i>.<I want the Id of PO_Log__c record associated with the opportunity to go to in the polgid__c >;</i></b>
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
update oppIds;
}
The code which I have submitted will give you the results according to your requirement provided that PO_Log__c object record is associated with an opportunity. I suggest you do a NULL check in your code. After NULL check your trigger will look something like this.
trigger updateopppo on PO_Log__c ( after update)
{
Set<Id> oppIds = new Set<Id>();
for ( PO_Log__c obj: trigger.new ){
if(obj.NAME_OF_THE_CUSTOM_FIELD_ON_PO_Log__c_Object_WHICH_CONTAINS_ASSOCIATED_OPPORTUNITY_ID!=NULL){
opportunity o=new opportunity();
o.id=obj.NAME_OF_THE_CUSTOM_FIELD_ON_PO_Log__c_Object_WHICH_CONTAINS_ASSOCIATED_OPPORTUNITY_ID;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
}
update oppIds;
}
Error: Compile Error: Method does not exist or incorrect signature: void add(Opportunity) from the type Set<Id> at line 10 column 20.
I just added the opportunity lookup field to your code.
trigger updateopppo on PO_Log__c ( after update)
{
Set<Id> oppIds = new Set<Id>();
for ( PO_Log__c obj: trigger.new ){
if(obj.Opportunity__c!=NULL){
opportunity o=new opportunity();
o.id=obj.Opportunity__c;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
}
update oppIds;
}
Hi, Sorry, my bad. I forget to change the datatype of list. Try running this code:
trigger updateopppo on PO_Log__c ( after update)
{
Set<Opportunity> oppIds = new Set<Opportunity>();
for ( PO_Log__c obj: trigger.new ){
if(obj.Opportunity__c!=NULL){
opportunity o=new opportunity();
o.id=obj.Opportunity__c;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
}
update oppIds;
}
I was able to get your code to work by changing Set to List. The working code is below. Thank you so much for helping me. I really appreciate it.
trigger updateopppo on PO_Log__c ( after update)
{
List<Opportunity> oppIds = new List<Opportunity>();
for ( PO_Log__c obj: trigger.new ){
if(obj.Opportunity__c!=NULL){
opportunity o=new opportunity();
o.id=obj.Opportunity__c;
o.polgid__c=obj.id; //This is the child record of that opportunity record
oppIds.add(o);
}
}
update oppIds;
}
Narender,
I was able to get your code to work by changing Set to List. The working code is below.
Thank you so much for helping me. I really appreciate it.
All Answers
But you can achieve this simple requirement using Process builder. Create a process builder on child object with criterion as on update of the field and in action update the opportunity field. This is more recommended approach than trigger.
If this helped you in any ways please mark as best answer
Error: Compile Error: A value cannot be stored to polgid__c in type Opportunity at line 7 column 54. I can't seem to get it to work. Any thoughts?
Try this code:
Note: Your opportunity record might have more than one child, in that case you will have to modify your code in accordance to how you want to store those IDs and where you want to store them.
Let me know if it helps.
Thanks!
The code which I have submitted will give you the results according to your requirement provided that PO_Log__c object record is associated with an opportunity.
I suggest you do a NULL check in your code.
After NULL check your trigger will look something like this.
I am getting the following error.
Error: Compile Error: Method does not exist or incorrect signature: void add(Opportunity) from the type Set<Id> at line 10 column 20.
I just added the opportunity lookup field to your code.
Sorry, my bad. I forget to change the datatype of list.
Try running this code:
Thanks!
Narender,
I was able to get your code to work by changing Set to List. The working code is below.
Thank you so much for helping me. I really appreciate it.