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
huskerwendyhuskerwendy 

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);
      		    		     	
        }  
           
	 }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Hengky IlawanHengky Ilawan

Hi,

 

Instead of looping thru the keyset of vdDocs, I loop thru the values, it's more straighforward.

 

 

trigger UpdateVersaDocDocument on Opportunity (after insert, after update) {
    if (Trigger.isInsert){
        map<String, Date> mapWOIds = new map<String, Date>();

        // 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);  
            }           
        }
        // 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 :mapWOIds.keySet()
        ]);

        // iterate over the list of versadoc Documents and assign the invoice date
        for (VersaDocDocuments__c vdDoc : vdDocs.values()) {
            vdDoc.Invoice_Date__c = mapWOIds.get(vdDoc.Work_Order_ID__c);                                
        } 
        update(vdDocs.values());
           
     }
}

Btw, the workOrderIds set is not necessary since you can use mapWOIds.keySet() in the SOQL query.

 

Regards,

Hengky

 

 

All Answers

Hengky IlawanHengky Ilawan

Hi,

 

Instead of looping thru the keyset of vdDocs, I loop thru the values, it's more straighforward.

 

 

trigger UpdateVersaDocDocument on Opportunity (after insert, after update) {
    if (Trigger.isInsert){
        map<String, Date> mapWOIds = new map<String, Date>();

        // 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);  
            }           
        }
        // 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 :mapWOIds.keySet()
        ]);

        // iterate over the list of versadoc Documents and assign the invoice date
        for (VersaDocDocuments__c vdDoc : vdDocs.values()) {
            vdDoc.Invoice_Date__c = mapWOIds.get(vdDoc.Work_Order_ID__c);                                
        } 
        update(vdDocs.values());
           
     }
}

Btw, the workOrderIds set is not necessary since you can use mapWOIds.keySet() in the SOQL query.

 

Regards,

Hengky

 

 

This was selected as the best answer
huskerwendyhuskerwendy

Thanks so much Hengky. It works perfectly!