Skip to main content
added 361 characters in body
Source Link
Ideasforce
  • 1.2k
  • 2
  • 7
  • 18

[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) {}
        
    }
}
public class GenerateServiceContract {
    @InvocableMethod
    public static void fetchOrderItems(List<Id> orderId) {
        List<Order> relatedOrder = [SELECT Id, AccountId, 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
        );
        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) {}
        
    }
}

[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) {}
        
    }
}
Source Link
Ideasforce
  • 1.2k
  • 2
  • 7
  • 18

The post referenced by @m Peixoto explains exactly the problem you are experiencing. To be more specific:

  1. Product2Id is not writable in ContractLineItem
  2. You need to write PricebookEntryId instead —previously including it in your OrderItem query

Here's how your code should look like:

public class GenerateServiceContract {
    @InvocableMethod
    public static void fetchOrderItems(List<Id> orderId) {
        List<Order> relatedOrder = [SELECT Id, AccountId, 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
        );
        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) {}
        
    }
}