-2

for work order object create a record data type as 'Parent Work Order' ,for Work Order object create a field 'Parent Order -data type(self lookup) i have to run a batch which associates all the work orders of same toplevel(this is a field in work order object) under single project work order.if the parent work order is there then use it,else create a new parent work order

global class WorkOrderBatch implements Database.Batchable<sObject>
    {  
      global Database.QueryLocator start(Database.BatchableContext bc)
        {
            return Database.getQueryLocator('select id,name,SVMXC__Top_Level__c,Parent_Work_Order__c from SVMXC__Service_Order__c where SVMXC__Top_Level__c <> null');
        }

      global void execute(Database.BatchableContext bc,list<SVMXC__Service_Order__c> batch)
        {   List<string> namelist =new List<string>();
          for(SVMXC__Service_Order__c na:batch)
          {
              namelist.add(na.name);
          }
            List<SVMXC__Service_Order__c> updatelist=new List<SVMXC__Service_Order__c>();
            List<SVMXC__Service_Order__c> serviceorderlist=new List<SVMXC__Service_Order__c>();
            for(SVMXC__Service_Order__c serviceorder :[select RecordTypeId,name,Parent_Work_Order__c
                                                       from SVMXC__Service_Order__c where name in:namelist ])
            {   
                serviceorder.Parent_Work_Order__c=serviceorder.RecordTypeId;
                updatelist.add(serviceorder);

            }
           update updatelist;
        }
     global void finish(Database.BatchableContext bc)
     {

     }

}

in mycode there is no error but the parent work field does not populate

5
  • 2
    Why are you assigning a Record Type Id to the Parent_Work_Order__c field, which I assume is a lookup relationship? That should simply fail. Do you want the Id instead, to create a parent-child relationship?
    – David Reed
    Commented May 9, 2018 at 18:17
  • in the record type id i have 2 picklist parent and child .for each work order i want to give the particular record tyoe id(parent,child) Commented May 9, 2018 at 18:20
  • record type is a diff layout in detail page Commented May 9, 2018 at 18:21
  • Your question is about populating a lookup relationship. Are you trying to do that, or change record type, or both? Please edit your question to make that clear - this looks like a very solvable problem, but we need some help from you to get to an answer.
    – David Reed
    Commented May 9, 2018 at 18:22
  • yes i have to populate the Parent_Work_Order__c field with ids which are record type Commented May 9, 2018 at 18:24

1 Answer 1

1

You seem to be mixing two different concepts here, so let me try to add a little bit of clarity, even thought your question is not 100% clear to me.

Lookup relationships connect one object to another object. The lookup field on the child object is populated with the Salesforce Id of the parent object.

Record types help define what sort of data a specific record of one object contains. Record types are a special kind of lookup; they connect not to other records of the same type but to the RecordType object that's maintained in Setup.

These two concepts are fundamentally separate from one another. You can build object hierarchies by establishing parent-child relationships, AND you can set the record type on any of the records that are involved, but those two changes have nothing to do with one another by their nature.

Your code is currently assigning a Record Type Id to a Lookup relationship:

serviceorder.Parent_Work_Order__c=serviceorder.RecordTypeId;

This is not legal, and if it does not cause an exception I find that rather surprising. If, as it appears to be and as your question, this field is a self-lookup, you should only be assigning the Ids of other SVMXC__Service_Order__c records to this field. If you want to assign the record type, you must assign to the RecordTypeId field.

You don't seem to have implemented the code to complete your requirement having to do with designating a single parent record for specific groups of work order records. To do that, you would need to either query for or create the parent record, and then assign its Id to the Parent_Work_Order__c field of the records that should be its children.

5
  • Id batchJobId =Database.executeBatch(new WorkOrderBatch()); isthis correct ? Commented May 9, 2018 at 18:54
  • The Id of your batch job has nothing to do with this issue.
    – David Reed
    Commented May 9, 2018 at 18:55
  • okay . what i wanted to tell u is that i have created two new record type of name parent and one child.so whenever have to create a new work order i need to select one of them and i want that id(parent/child) to populate in Parent_Work_Order__c. Commented May 9, 2018 at 18:59
  • 1
    You've written a batch class, not a trigger. Assignment of record types at the time of record creation can be handled through the user interface or other automation, but a batch class is not the way one would do this. It's also not what you describe in your question. I don't think I can help any more unless you can communicate more clearly about your problem. You may want to work with Trailhead to build your fundamentals.
    – David Reed
    Commented May 9, 2018 at 19:02
  • im running the batch after creating the records like for example i create 10 records first then i run the batch Commented May 9, 2018 at 19:05

Not the answer you're looking for? Browse other questions tagged .