0

Scenario: I have to update Parent 'Proposal' object and its Child 'Proposal Line Item' objects if the Status of Proposal is 'Published' and its 'End Date' is modified.

Below is my Trigger Method.

Public Static Void UpdatePLIonDateUpdateAfterPublish(List<Apttus_Proposal__Proposal__c> OldProposalList , List<Apttus_Proposal__Proposal__c> NewProposalList)
    {
        List<Apttus_Proposal__Proposal_Line_Item__c> PLIList = [Select Id,Apttus_QPConfig__StartDate__c,Apttus_QPConfig__EndDate__c,Apttus_Proposal__Proposal__c FROM Apttus_Proposal__Proposal_Line_Item__c WHERE Apttus_Proposal__Proposal__c =: NewProposalList];
        System.debug('---PLIList---'+PLIList.size());

        List<Apttus_Proposal__Proposal_Line_Item__c> PLIToUpdate = New List<Apttus_Proposal__Proposal_Line_Item__c >();

        FOR(Apttus_Proposal__Proposal__c OProp : OldProposalList)
        {        
            FOR(Apttus_Proposal__Proposal__c NProp : NewProposalList)
            {
                IF((NProp.APTPS_Contract_Expiration_Date__c != OProp.APTPS_Contract_Expiration_Date__c) && (NProp.Pricing_Agreement_Status__c == 'Published') && (OProp.Id == NProp.Id))
                {
                    NProp.Pricing_Agreement_Status__c = 'Ready To Publish'; //Not working
                }
            }
        }

        FOR(Apttus_Proposal__Proposal__c OProp : OldProposalList)
        {        
            FOR(Apttus_Proposal__Proposal__c NProp : NewProposalList)
            {
                FOR(Apttus_Proposal__Proposal_Line_Item__c PLI : PLIList)
                {
                    IF((NProp.APTPS_Contract_Expiration_Date__c != OProp.APTPS_Contract_Expiration_Date__c) && (NProp.Pricing_Agreement_Status__c == 'Published') && (OProp.Id == NProp.Id) && (NProp.Id == PLI.Apttus_Proposal__Proposal__c))
                    {
                        Apttus_Proposal__Proposal_Line_Item__c APLI = New Apttus_Proposal__Proposal_Line_Item__c() ;

                        APLI.Id = PLI.Id;
                        APLI.Apttus_QPConfig__EndDate__c = NProp.APTPS_Contract_Expiration_Date__c ;  //Working and updating records
                        APLI.Publishing_Status__c = 'To Be Published'; //Not working                     
                        PLIToUpdate.Add(APLI);
                        System.debug('---PLIToUpdate---'+PLIToUpdate.size());
                    }
                }
            }
        }
        Update PLIToUpdate;
    }

I am updating the records in the 'Before trigger context'. But it is only updating the 'End date' of all the Child(Proposal Line Items) records but it is not updating the 'Publishing Status' field of the same records. All it is not updating the 'Pricing_Agreement_Status__c' field of the parent(Proposal) record.

I have checked the field API names and they are correct.

Please help me in resolving the issue.

Thanks!

2

You need a few things to update both child + parent records in the same trigger :

1)parent MUST be inserted before child. 2)there MUST be a relation between child and parent before you insert them. 3)some sort of “temporary holder” for the parent ID before it is associated with the child (here he has used external IDs)

1)create a parent with an external ID. 2)create a child with same external ID as parent’s. 3)insert the parent FIRST and then the child.

Try this,i think this may help you.

  • @forecetalks we'd recommend you include the actual answer within your posted answer. Else we're going to have to consider this advertising for your blog and it may be removed. – Samuel De Rycke Sep 3 '16 at 9:55
  • Hi, Thank you so much for your recommendation, I just updated my answer. – Forcetalks Sep 5 '16 at 8:32
0

Yes it is!!

Refer to this EXCELLENT write up by our very own Bob Buzzard!

You need a few things to update both child + parent records in the same trigger :

  1. parent MUST be inserted before child.
  2. there MUST be a relation between child and parent before you insert them.
  3. some sort of "temporary holder" for the parent ID before it is associated with the child (here he has used external IDs)

TL;DR :

  1. create a parent with an external ID.
  2. create a child with same external ID as parent's.
  3. insert the parent FIRST and then the child.

EDIT : I am still NOT sure myself, just why external ID's are used. Any help in this part will be super appreciated.

  • 2
    Please expand the answer a bit with a relevant quote from the blog post - see How to Answer - link only answers are not favoured here. – Dave Humm Aug 17 '16 at 11:16
  • Sorry, will do! – fraglord Aug 17 '16 at 11:24
  • @fraglord : The issue is not with the insert. The records are already in place. I need to update both the Parent/Child records if one of the fields(End Date) of the parent record is modified. I need to change the status then for both the objects and replicate the 'end date' to all the Child objects. The weird thing that is happening now is that the dates are replicated to all child records but 'Status' is changed only for one of the child record. Also the Parent record status is not getting modified. – Ruchi Aug 17 '16 at 11:45
  • You can replace insert by update. If STILL there's a problem, then we have to look in other places to fix it. – fraglord Aug 17 '16 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.