1

When trying to deploy some code to my production org, I was getting a test failure error and after some debugging, discovered that the issue is related to the fact that when the test records are created, the record type being assigned is getting ignored, and the record ends up being created with the incorrect record type, which is why the test is failing.

I'm able to duplicate the same using the developer console when I create records with anonymous apex. The RecordTypeId I specify is being ignored and some other record type gets assigned.

String poRecId;
String budgetRecId;

for(RecordType rt : [SELECT Id, Name, DeveloperName, IsActive, SObjectType FROM RecordType WHERE SObjectType = 'Purchase_Order__c' AND (DeveloperName = 'Budget' OR DeveloperName = 'Purchase_Order')]){
  system.debug(rt.DeveloperName);
  if(rt.DeveloperName == 'Purchase_Order'){
    poRecId = rt.Id;
  }
  else{
    budgetRecId = rt.Id;
  }
}
system.debug('budget rtid: ' + budgetRecId);
system.debug('po rtid: ' + poRecId);

Purchase_Order__c po = new Purchase_Order__c( 
  Amount__c=1000, 
  Financial_Due_Date__c=Date.today(),
  RecordTypeId = budgetRecId,
  Type__c = 'Catering-Food',
  Purchase_Description__c = 'Test PO 2'
);
insert po;

The debugs are showing the correct record type ids, but then the created PO record has the wrong type. I can change it manually after the fact.

Any clues?

  • Does the user you are using for the deployment , has access to this record type ? Also why do you have two different variables in the record type assignment . – Mohith Shrivastava Jul 23 '17 at 23:26
  • Yes, of course. The user doing the deploy has access to everything, and is the same user that I'm running this code in anonymous apex, which is the same user that is allowed to change the record type manually after the fact to the correct one. Don't worry about the other variable. This is only a section of the code. Thanks – MoDiggity Jul 23 '17 at 23:33
  • Assuming that the budget record type is the one you want to assign, the most likely cause of the record type being different than what you specific would be some other code, process builder, or workflow rule setting the record type explicitly based on some condition on the record you are inserting. Turning on workflow rule debugging and completely reviewing the logs will help you track it down – Eric Jul 24 '17 at 3:42
  • Thanks @Eric, funny because in a sandbox that i just created, everything works fine, with the same workflows and processes as production. I was wondering the same thing but i don't think there are any workflows or processes that are changing it – MoDiggity Jul 25 '17 at 3:37
  • 1
    @Eric you were right. There was a process builder shoved in there that was editing the record. Turns out, I wasn't able to see it in the logs, though. I was able to track it down by creating a validation rule to block the field from getting reset, and then the logs revealed the stack trace when I tried to create a record. That is a neat trick a friend showed me. – MoDiggity Jul 26 '17 at 23:57
1

PO is getting created with wrong recordtypeId because you have assigned wrongly.

Purchase_Order__c po = new Purchase_Order__c( 
  Amount__c=1000, 
  Financial_Due_Date__c=Date.today(),
  RecordTypeId = poRecId, //correct assignment of recordTypeId
  Type__c = 'Catering-Food',
  Purchase_Description__c = 'Test PO 2'
);

Secondly, no need to query recordtype by SOQL. You can get the recordtypeId based on Name as follows:

Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('<provide correct Name>').getRecordTypeId();
| improve this answer | |
  • 1
    note that using developerName is somewhat more robust and less likely to change over time than record type label; unfortunately, there is no describe method to get recordTypeId by recordType DeveloperName. – cropredy Jul 24 '17 at 3:34
  • Did I miss the OP say it should be the po record type and not the budget? I would assume that the OP wanted the budget but a different record type was assigned. Maybe your right though and it was a simple mistake. – Eric Jul 24 '17 at 3:43
  • Thanks Santanu but the budgetRecId is the one I want – MoDiggity Jul 25 '17 at 3:37
0

Turns out, Eric was right, that there was a rogue process builder that was updating the record type, but it wasn't showing in the logs.

I was able to reveal the offending process by creating a validation rule to block the record type from being updated, thereby exposing the process in the logs through a more robust stack trace. Led me right to it.

I figured I'd post that here as the answer since I know it will be a useful debugging technique for many out there.

| 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.