+ Start a Discussion
Jay reddyJay reddy 

updating records after deleting one record

Hello,

I'm stuck with updating the records when one of the records is deleted from the list. Below is the code which updates the terms and Conditions related list in the quote when a quote line item is added to the quote. I'd like to delete the related Terms and Conditions of a product from the T&C Related list once the product is removed from the quote. Could someone help on that, please?
 
public string updateTAC(string errorMsg) {
           try {
            list < String > productIDs = new list < string > ();
            list < Id > produc_code = new list < Id > ();
            list < String > produc_c = new list < String > ();
            map < Id, Terms_and_Conditions_Quote__c > tacToInsert = new map < Id, Terms_and_Conditions_Quote__c > ();

            for (integer index = 0; index < quoteLineItems.size(); index++) {
                productIDs.add(quoteLineItems[index].productCode);
            }
            for (Product2 Pcode: [select id from Product2 where ProductCode in : productIDs]) {
                produc_code.add(Pcode.id);
            }

            for (Terms_and_Conditions_Product__c tac: [SELECT CreatedById, CreatedDate, Terms_and_Conditions__c, CurrencyIsoCode, Id, IsDeleted, LastModifiedById, LastModifiedDate, Name, OwnerId, Product__c
                    FROM Terms_and_Conditions_Product__c where Product__c in : produc_code
                ])
                tacToInsert.put(tac.Terms_and_Conditions__c, new Terms_and_Conditions_Quote__c(Quote__c = quoteID, Terms_and_Conditions__c = tac.Terms_and_Conditions__c));

            if (!tacToInsert.IsEmpty()) { //now we need to compare to avoid duplicats           
                map < Id, Terms_and_Conditions_Quote__c > TACToCompare = new map < Id, Terms_and_Conditions_Quote__c > ([select id, Description__c, Terms_and_Conditions__c
                    from Terms_and_Conditions_Quote__c where quote__c = : quoteID
                ]);

                for (Terms_and_Conditions_Quote__c tempTAc: tacToInsert.values()) {
                    for (Terms_and_Conditions_Quote__c OldTact: TACToCompare.values()) {
                        if (OldTact.Terms_and_Conditions__c == tempTAc.Terms_and_Conditions__c) {
                            //strForTestingissuesMsg += tempTAc.Terms_and_Conditions__c;
                            tacToInsert.remove(tempTAc.Terms_and_Conditions__c);
                            break;
                        }
                    }
                }

                insert tacToInsert.values();
            }

        } catch (Exception e) {
            errorMsg = e.getMessage();
        } 
        return errorMsg; 
    }
Thanks,
GR