2

What I'm doing wrong here? I'm creating a new record which is work_order__c and once I created I'm getting the name of the newly created row and updating the vendor__c object

vendor__c.Current_work_order__c is a lookup on work_order__c object

Anybody can show me what I'm doing wrong here?

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger vendorTrigger caused an unexpected exception, contact your administrator: vendorTrigger: execution of AfterUpdate caused by: System.StringException: Invalid id: WO-0000411: Class.vendorController.PopulateWorkOrderBy: line 161, column 1

I have trigger on vendor__c object

  trigger vendorTrigger on vendor__c (after insert, after update) 
  {
    vendorController vencont = new  vendorController();
    vencont.PopulateWorkOrderBy(Trigger.new);
  }

public with sharing class vendorController
{       
      public void PopulateWorkOrderBy(List<vendor__c> vendor)
      {
         //run some business rules/validations
         //...//

         //insert into WO
         Work_Order__c wo = new Work_Order__c();
         wo.Est_Hours__c = 89;
         wo.Instructions__c = 'this is a test';
         insert wo; 

         //lets get the vendor id first:
         string venId
         for(vendor__c ven : vendor)
         {
           //get the vendor id
           venId = ven.Id;
         }

         //its time to update the vendor field
         Vendor__c vendorObj = [select Id, Name, current_work_order__c from vendor__c where id =: venId];
         vendorObj.Current_work_order__c = wo.Name //name of the newly created work_order__c Name

         //update the vendor object now
         update vendorObj;
      }
}
2

Change this:

vendorObj.Current_work_order__c = wo.Name;

To this:

vendorObj.Current_work_order__c = wo.Id;

There seem to be other problems with your code that could cause problems - I'm not sure if they are because you cut the code down or if they are real, but I'd look closely at this code:

string venId
for(vendor__c ven : vendor)
{
    venId = ven.Id;
}

This is going to save only the last vendor id - is that what you were intending?


EDIT You don't need any of the query at the end either. You need to make this a before update, before insert trigger

Ie, remove this:

Vendor__c vendorObj = [select Id, Name, current_work_order__c from vendor__c where id =: venId];
vendorObj.Current_work_order__c = wo.Name 
//update the vendor object now
update vendorObj;

Define your trigger like this:

trigger vendorTrigger on vendor__c (before insert, before update) 

Add this:

vendor.Current_work_order__c = wo.Id;

The reason for this is that before insert/update triggers implicitly update the trigger - you are modifying the trigger before it has been saved to the database and it will be saved soon after your trigger runs.

5
  • getting this error if i changed to wo.id ` execution of AfterUpdate caused by: System.FinalException: Record is read-only: `
    – Nick Kahn
    May 27 '15 at 2:08
  • This needs to a before update trigger May 27 '15 at 2:09
  • after i changed I'm getting execution of AfterInsert caused by: System.FinalException: Record is read-only
    – Nick Kahn
    May 27 '15 at 2:13
  • 1+ Thank you so much, I really appreciate your help
    – Nick Kahn
    May 27 '15 at 2:25
  • No worries - good luck with the trigger. May 27 '15 at 2:39

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.