0

I recently came across an Issue when running a Test class. In my Test class I Insert a custom object. And by the time the record is inserted, there is a workflow rule to fire and populate the Id value to a custom field of the same object. This value will be later used in some other processing. (Please note that this field is an External Id, unique and case sensitive)

But looks like the workflow rule is not triggered and value is not getting updated. Any idea what could go wrong?

Here is the code I'm trying to execute,

Platform_Org__c org = new Platform_Org__c(); org.Name = 'Test org 3'; org.Parent_Account__c = '0018A000008LUFK'; insert org; system.debug('org.dup_id__c-'+org.dup_id__c);

And the WF Rule, i have to update the dup Id, Criteria evaluates on : Created and everytime its edited. Rule Criteria : Platform Org : Dup Id equals '' If the criteria matches the Dup Id will be default to Record Id

LB

  • Are you sure you set the rule to active? If it is, could you post the workflow rule and field update code? – Mauricio Oliveira Nov 4 '16 at 11:17
  • Well the Rule is Active. And the WF fires when we create the custom record in UI. – L Mudiyanse Nov 4 '16 at 11:26
  • Can you check the rule criteria for a workflow to get fired? You need to create similar data in test class for workflow to get executed. – Devendra Nov 4 '16 at 11:29
  • 1
    As this is an ExternalId, does your workflow rule call Outbound Messaging in order to obtain the externalId or is a formula used to create the Id? If a callout of some kind is used, you won't be able to test it without setting up a mock. – crmprogdev Nov 4 '16 at 11:31
  • We are using a formula to create the Id, No outbound messages. Actually the value isn't updated even I execute the code in Anonymous block. Here is the Code, Platform_Org__c org = new Platform_Org__c();<br/> org.Name = 'Test org 3'; <br/> org.Parent_Account__c = '0018A000008LUFK'; <br/> insert org; <br/> system.debug('org.dup_id__c-'+org.dup_id__c); <br/> and here is the WF rule i have to update the dup Id, Criteria evaluates on : Created and everytime its edited. Rule Criteria : Platform Org : Dup Id equals '' If the criteria matches the Dup Id will be default to Record Id – L Mudiyanse Nov 4 '16 at 14:53
0

Workflow rules will be running after all other executions like before/after triggers, apex classes etc... are completed. So only way that we can get these certain details is via a Query.

So in my scenario, after I insert the Org, I'll have to Query my Platform_Org__c custom object to retrieve values.
system.debug([Select Id, dup_id__c From Platform_Org__c where Id = :org.Id]);

Cheers,
Lahiru

| improve this answer | |
  • Thats not why but the solution is the same. Did you miss my answer to did our posts cross – Eric Nov 16 '16 at 6:13
0

You need to requery the record with the field in the query after insert. New fields / update external to the local class do not just appear in the class sObject after DML

| improve this answer | |

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.