I am writing a testclas for a trigger of a flow but get the error: System.StringException: Invalid id: Test In my system debug I get an Id for my Pricebook so what am I missing?


trigger StartFlow on Order__c (after update ) 
    Set<Id> opptyIds = new Set<Id>();
    public Flow.Interview.Create_Renewal_OppLineItems_from_Invoice_record DummyFlow {get; set;}
    if(Trigger.isAfter && Trigger.isUpdate)
        for(Order__c o : Trigger.New) 
            if((o.Opportunity_Name__c!=Null) && (o.Opportunity_Name__c != Trigger.oldMap.get(o.Id).Opportunity_Name__c)){

            string value0 = o.Account_Name__r.Customer_Profile_Pricebook__c;
            date   value1 = o.End_Date__c;
            double value2 = o.Year_Enddate_1_day__c;
            string value3 = o.Journal_Reader_Code__c;
            double value4 = o.Number_of_Users__c;
            double value5 = o.Renewal_Number__c;
            Id     value6 = o.Opportunity_Name__c;
            Id     value7 = o.Product__c;
            string value8 = o.Delegate_Admin__c;
            string value9 = o.CurrencyIsoCode;

            Map<String, Object> myMap = new Map<String, Object>();
            myMap.put('VarCustomerProfile', value0);//Customer_Profile_Pricebook__c    
            myMap.put('VarInvoiceEnddate', value1);  //End_Date__c
            myMap.put('VarInvoiceEnddateYear', value2);  //Year_Enddate_1_day__c
            myMap.put('VarInvoiceJRC', value3); //Journal_Reader_Code__c
            myMap.put('VarInvoiceNumberOfUsers', value4); //Number_of_Users__c
            myMap.put('VarInvoiceRenewalNumber', value5);//Renewal_Number__c
            myMap.put('VarOppId', value6);//OpportinityID
            myMap.put('VarProductId', value7);//ProductId
            myMap.put('VarDelegateAdmin', value8);//Delegate_Admin__c
            myMap.put('VarInvoiceCurIsoCode', value9);//CurrencyIsoCode

            DummyFlow = new Flow.Interview.Create_Renewal_OppLineItems_from_Invoice_record(myMap);


public class StartFlowTestClass {
    private static void StartFlowTestClass(){

        Product2 prod = new Product2(Name = 'Laptop X200', 
        Family = 'Hardware');
        insert prod;

        Id pricebookId = Test.getStandardPricebookId();

        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;

        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;

        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;

        Account acct = new Account ( Name='Testaccount',Customer_Profile__c='Wo',Billingstreet='Palmboomstraat 23', BillingCountry='Netherlands',BillingPostalcode='3500AA', BillingCity='Utrecht',IsBillingAddress__c=true,
        Shippingstreet='Palmboomstraat 23', ShippingCountry='Netherlands',ShippingPostalcode='3500AA', ShippingCity='Utrecht');
        insert acct;
        system.debug('acct.Id '+acct.Id);
        Opportunity opp = new Opportunity(Name = 'TestOpp', Type='Renewal',AccountId=acct.Id, StageName='DMU check',CloseDate= System.today().addMonths(1),Pricebook2Id=customPB.Id);
        insert opp;
        system.debug('opp.Pricebook2Id '+opp.Pricebook2Id);
        Contact cont = new Contact(Lastname='Test', Initials__c='A.A.', AccountId=acct.Id, Email='testcontact@test.nl', Title='Consultant' , Department='Academic');
        insert cont;
        Order__c ord = new Order__c(Account_Name__c=acct.Id, Delegate_Admin__c=cont.LastName, Number_of_Users__c=5,  CurrencyIsoCode='EUR - Euro',Order_Amount__c=1500,Opportunity_Name__c=Opp.Name);
        insert ord;

  • From the answers you've gotten so far (and the comments on them), it appears that you're getting a different error message than the one you currently indicate in your question. Please edit your question to include the full text of the error message you're getting verbatim. – Derek F Oct 11 '17 at 13:45
    Order__c ord = new Order__c(Account_Name__c=acct.Id, Delegate_Admin__c=cont.LastName, 
        Number_of_Users__c=5,  CurrencyIsoCode='EUR - Euro',Order_Amount__c=1500,

It looks like Delegate_Admin__c is a lookup field (type Id), but you assigned the LastName field from the contact to this field. It should probably instead be Delegate_Admin__c=cont.Id. The same is probably also true for Opportunity_Name__c=Opp.Name, but it should probably be Opportunity_Name__c=opp.Id.

In the future, I strongly suggest you avoid naming lookup fields with the word "name", because that suggests that it is not an Id field. This is probably the source of your confusion.

  • @sfdfox I tried your suggestions but still get :USER_DEBUG [16]|DEBUG|assigning pricebook id null to opportunity – Jenssen Oct 11 '17 at 11:57

You need to change this line

 Id     value6 = o.Opportunity_Name__c;


 string     value6 = o.Opportunity_Name__c;

You are assigning string value into Id. Thats why you are getting this error. You need to use string variable here.

  • I tried that but have the same error – Jenssen Oct 11 '17 at 9:59
  • @Jenssen Try for string value7 = o.Product__c; as well. As we are not sure if thats a lookup field or text field. – Tushar Sharma Oct 11 '17 at 10:01
  • Tried that also but no relief: USER_DEBUG [16]|DEBUG|assigning pricebook id null to opportunity – Jenssen Oct 11 '17 at 10:03
  • @Jenssen are you getting same original error message? Also what are you getting here system.debug('opp.Pricebook2Id '+opp.Pricebook2Id); – Tushar Sharma Oct 11 '17 at 10:12
  • @Jenssen why did you downvoted the asnwer? – Tushar Sharma Oct 11 '17 at 10:38

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.