You need to sign in to do that
Don't have an account?
huskerwendy
Trigger to update related record not related by a Salesforce ID
I'm trying to write a trigger on opportunities that when an opportunity is closed, it will update the invoice date field on a custom object. The records are related based on a unique field on the custom object that is in the description field on the opportunity. The field is called Work_order_id_c on the custom object. I don't know how to assign the CloseDate from the mapWOIds map to the vdDocs InvoiceDate using the WorkOrderID. Any help would be appreciated.
trigger UpdateVersaDocDocument on Opportunity (after insert, after update) { if (Trigger.isInsert){ map<String, Date> mapWOIds = new map<String, Date>(); set<String> workOrderIds = new Set<String>(); list<VersaDocDocuments__c> vdDocsUpdate = new list<VersaDocDocuments__c>(); // loop through trigger and add WorkOrderID and Invoice date to map for (Opportunity o : trigger.new){ if (o.StageName == 'Closed/Won' && o.Type == 'Customization' && o.Description.startsWith('WorkOrderID:')) { mapWOIds.put(o.Description.substringAfter(': '), o.CloseDate); workOrderIds.add(o.Description.substringAfter(': ')); } } // create a map a VersaDoc Documents map<String, VersaDocDocuments__c> vdDocs = new map<String, VersaDocDocuments__c>([Select Work_Order_ID__c, Id, Invoice_date__c from VersaDocDocuments__c Where Work_Order_ID__c in :workOrderIds]); // iterate over the list of versadoc Documents and assign the invoice date for (String vd : vdDocs.keySet()) { System.debug('in for loop vddocs.key is ' + vdDocs.get(vd)); vd.Invoice_Date__c = mapWOIds.get(vdDocs.get(vd)); // vdDocsUpdate.add(vd); } } }
Hi,
Instead of looping thru the keyset of vdDocs, I loop thru the values, it's more straighforward.
Btw, the workOrderIds set is not necessary since you can use mapWOIds.keySet() in the SOQL query.
Regards,
Hengky
All Answers
Hi,
Instead of looping thru the keyset of vdDocs, I loop thru the values, it's more straighforward.
Btw, the workOrderIds set is not necessary since you can use mapWOIds.keySet() in the SOQL query.
Regards,
Hengky
Thanks so much Hengky. It works perfectly!