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
Frances Day 9Frances Day 9 

help with invalid id error on trigger

Hey everyone! 
I'm an admin who is trying to move into coding and to translate what I've learned in Visual Flow into code to help me be able to tackle problems I can't resolve in VF.
I could do this in VF, but I'm using it as an example to try to translate what I know, and I'm running into some issues.

Greatly appreciate any help anyone can provide to help me on my first trigger and class.
Class
public class ServiceOrderClass {
    public static void getServiceForOrder(List<Service__c> servicefromTrigger)
 { 
     List<Order> relatedOrder= new List<Order>();
     Order foundorder = new Order();
     for (Service__c svc:servicefromTrigger)  
    {
        if (svc.First_Service_Order__c!=NULL && svc.Service_Order__c=='') 
        {
            relatedOrder=[Select Id, SAP_DocNum__c From Order WHERE SAP_DocNum__c=:svc.First_Service_Order__c]; 
            {System.debug('this is the related order record '+relatedOrder);}
            foundorder=relatedOrder[0];
           svc.Service_Order__c= foundorder.id;
        }
    }
     update servicefromTrigger;
  }
}

Trigger
trigger servicefromTrigger on Service__c (before update) 
{
 ServiceOrderClass.getServiceForOrder(trigger.new);
}
The error message I'm receiving when I try to update a service record is: servicefromTrigger: execution of BeforeUpdate cuased by System.StringException: Invalid Id : Class.ServiceOrderClass.getServiceforOrder: line 8, column 1 Trigger.servicefromTrigger: line 3, column 1

What I'm trying to do is pass the Service__c.Id from the Trigger to the list in the Class, check to see if the First_Service_Order__c field is populated and the Service_Order__c field is NULL, and if those conditions are met, find the order that hasw the same 6 digit order number as the First_Service_Order__c field and write that order to a lookup field Service__c.Service_Order__c

Thanks again for any help.
Frances
SUCHARITA MONDALSUCHARITA MONDAL
Hi Frances,

There are few observation related to your code:

1. Don't put SOQL into FOR loops (line 10) , you'll run into governer limits
2. For BEFORE events your don't have to perform DML (like inset, update, delete etc.), it's taken care by Salesforce it self.

For code.. You can try something as below:


public class ServiceOrderClass {
    public static void getServiceForOrder(List<Service__c> servicefromTrigger)
 {  
    Set<String> serviceOrderSet = new Set<String>(); // to set non-duplicate service order
    for (Service__c svc:servicefromTrigger)  
    {
        if(svc.First_Service_Order__c!=NULL && svc.Service_Order__c==''){
            serviceOrderSet.add(svc.First_Service_Order__c);
        }
    }
    
    // taking Order as custom object  that's whyi it's 'Order__c' instead of 'Order'
     Map<String,Order__c> relatedOrderMap= new Map<String,Order__c>();  
     for(Order__c order: [Select SAP_DocNum__c,Id From Order__c WHERE SAP_DocNum__c IN : serviceOrderSet]){
        relatedOrderMap.put(order.SAP_DocNum__c , order.Id); 
     }
     
    if(relatedOrderMap.size() > 0){  // null check
         for(Service__c svc:servicefromTrigger){
            if(relatedOrderMap.containsKey(svc.First_Service_Order__c)) {
                svc.Service_Order__c = relatedOrderMap.get(svc.First_Service_Order__c);
            }
        }
         
    }
      
     
   }
}

Hope this helps!

Thanks,
Sucharita