[Edit: added Pricebook2Id after OP solution comment]
public class GenerateServiceContract {
@InvocableMethod
public static void fetchOrderItems(List<Id> orderId) {
List<Order> relatedOrder = [SELECT Id,
AccountId,
Pricebook2Id,
Order_Name__c
FROM Order
WHERE Id = :orderId];
ServiceContract scontract = new ServiceContract(
Order__c = relatedOrder[0].Id,
Name = relatedOrder[0].Order_Name__c,
AccountId = relatedOrder[0].AccountId,
Pricebook2Id = relatedOrder[0]. Pricebook2Id
);
try {
insert scontract;
List<OrderItem> oproduct = [SELECT Id, PricebookEntryId, Quantity, Contact_Name__c, UnitPrice FROM OrderItem WHERE OrderId = :orderId];
List<ContractLineItem> citemsToInsert = new List<ContractLineItem>();
for (OrderItem oitem: oproduct) {
citemsToInsert.add(new ContractLineItem(
ServiceContractId = scontract.Id,
PricebookEntryId = oitem. PricebookEntryId,
Quantity = oitem.Quantity,
UnitPrice = oitem.UnitPrice
));
}
insert citemsToInsert;
} catch (Exception e) {}
}
}