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
Bob_zBob_z 

Update Opportunity Field with child ID

Is it possible to populate a custom text field (polgid__c) on an opportunity  with a child record (PO_Log__r with a trigger? 

 
Best Answer chosen by Bob_z
Bob_zBob_z
hI 
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. 

 
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; 

}

 

All Answers

Gururaj BGururaj B
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
 
Bob_zBob_z
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;   
}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Bob,
Try this code:
 
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.

Let me know if it helps.
Thanks!
Bob_zBob_z
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; 

}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Bob,

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; 

}
Bob_zBob_z
Hi  Narender,
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. 
 
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; 

}

 
Narender Singh(Nads)Narender Singh(Nads)
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; 

}

Thanks!
Bob_zBob_z
hI 
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. 

 
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; 

}

 
This was selected as the best answer