0

I'm trying to populate a lookup field through apex, though am currently testing by populating a simple text field after a form is submitted, activated through the process builder. Below is my code, which does not work.

public class populateLookup {
    @InvocableVariable(label='Case')
    public Case c;
        @InvocableMethod
        public static void populateField(List<Case> cases) {
        // testing by trying to populate text field
        cases[0].Store_Order_Number__c = 'hi';        
    }
}

Is there a workaround if this is not possible? For example, without Apex and simply through the process builder or formstack? I feel as though I've tried everything other than thoroughly getting into Apex. Thanks.

Edit: I was able to figure this out by creating a trigger. Below is my code:

trigger populateOrderInfo on Case (before insert) {
    for (Case c : Trigger.new) {
        Order__c o = [SELECT Id FROM Order__c WHERE Name = 
        :c.Store_Order_Number__c];
        string orderID = o.Id;
        c.Order__c = orderId;
    }   
}
  • 3
    Welcome to SFSE. Can you edit your question to add some details on how this isn't working? What behavior do you see, or specific error messages? – David Reed Aug 21 '18 at 22:21
  • 3
    You can definitely populate a lookup field in Process Builder, and you don't actually need an invocable method to do so (although it doesn't hurt anything as such). – David Reed Aug 21 '18 at 22:22
  • @DavidReed I have been unable to do so receiving errors stating "Invalid ID" when I try. When attempting to do it through Process Builder, I was updating a record lookup field called "Order" with the value of a Text field. Is this possible, and I'm doing it incorrectly? – Bobbis Aug 21 '18 at 22:25
  • @DavidReed When attempting to set the new field value for the lookup field, I am unable to field reference the single specific field that contains the value I need. Are there some restrictions? Can I get around this through formstack? Thanks for your time. – Bobbis Aug 21 '18 at 22:40
  • @Bobbis Lookup refer to the id. If you query your child record with lookup field included, you will get field value as the id of the parent record. You cannot refer the text value for lookup. – sanket kumar Aug 22 '18 at 0:35
0

You need to add an "update" statement to your code.

public class populateLookup {
    @InvocableVariable(label='Case')
    public Case c;
        @InvocableMethod
        public static void populateField(List<Case> cases) {
        // testing by trying to populate text field
        cases[0].Store_Order_Number__c = 'hi';

        update cases; // Add this line
    }
}

As stated there is probably a non-code based way to address this issue. In your comment you mention that the error is "invalid Id". This tells me that the text field contains an ID that is not valid. There are an unfortunately large number of reasons for why this could be invalid for instance, it must be exactly 18 or 15 characters long or it could be referencing an object type that is different from the field you are trying to put the ID in (such as trying to put a caseId into a contact lookup).

You could probably make a validation rule on the object so that the text field is always a valid ID.

It's worth noting that APEX cannot override the ID validation on lookups so if it's invalid in the PB it's probably invalid in the code.

  • Thanks, though I am still receiving an error stating "An unhandled fault has occurred in this flow An unhandled fault has occurred while processing the flow." – Bobbis Aug 21 '18 at 22:32
  • If Store_Order_Number__c is a lookup field, you can't put 'hi' into it. The value has to be a legit Salesforce record Id. – David Reed Aug 21 '18 at 22:33
  • @DavidReed Store_OrderNumber__c is a text field, just using it for testing now. – Bobbis Aug 21 '18 at 22:34
  • 1
    Absolutely david, the OP did say he was testing with a normal text field in his code though. He got the invalid ID error in his PB which is a slightly different issue. I also updated my answer to include some info about invalid IDs – gNerb Aug 21 '18 at 22:34
  • @Bobbis If you get an error like that for undhandled fault in the flow you can troubleshoot this a couple ways. Generally you should get an email explaining the error. If you provide me with that email I can try to help solve that issue. – gNerb Aug 21 '18 at 22:41
0

To update the field you can update record action of process builder. As you said that you already have process builder on it.

using this you can update any field like (Store_Order_Number__c) to some value.

In this no need to write any code.

  • That field doesn't show up for me, is there any way to use the value of a field that isn't a child? Thanks. – Bobbis Aug 22 '18 at 15:47
  • @Bobbis Please check the field level security for this. than field will appear in process builder. No the object is not in relation than you can not access that value. – Chandra Kiran Aug 22 '18 at 15:51
  • Is there no way around this, if it is not in relation? – Bobbis Aug 22 '18 at 17:17
  • At that time you definitely use the Apex execution from the process builder. Like create a @invocableMethod and than put your logic in that – Chandra Kiran Aug 22 '18 at 17:40
  • Would the code I posted be able to accomplish that with a few changes? Or am I going about it wrong? – Bobbis Aug 22 '18 at 17:53

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.