function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OhadiosOhadios 

Weird - Cannot set custom object's record type upon creation...

Hi Guys.

 

I created a class to create an "Orders__c" record from opportunities.

So far everythis looks and works great, with the exception of the recordtype selection.

As you can see in the code below, I am finding the Opportunity's record type (either "VMS Opportunity" or "Transportation Opportunity")

and then based on the I assign "VMS Orders" or "TAM Orders" record type for the new Order record.

I've originally used a simple MAP to match these, but when this did not work - I tried loading the RecordTypes into individual variables, and then assigning them.

 

When I degub - I can see in the log that the newOrder.RecordType actually gets assigned with the CORRECT RecordTypeID, BUT

Once I open the new record - it is always showing the "VMS Orders" record type, regardless of what is assigned in the code.

I've even tried inserting the record first, and then updating the recordtype as a secondary operation... still no dice.

 

I've looked at my triggers and workflows and cannot see anything that should change the recordtype...

 

Any help figuring this out would be greatly appreciated.

I feel like I am missing something stupid.

 

CODE: (Just the relevant snippet from my class)

 public pagereference createOrder(){        
        if (acctAsEndCustomer && this.Opp.End_User__c==null) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must select a Customer'));
            PageReference pageRef = ApexPages.currentPage();
            return pageRef;
        }
        Opportunity myOpp = [select id, Name, AccountID, RecordTypeID from Opportunity where id = :this.opp.ID];

        Orders__c newOrder = new Orders__c();
        IF( acctAsEndCustomer ) {
            newOrder.Customer__c = this.Opp.End_User__c;
            newOrder.End_Customer__c = myOpp.AccountID;
        } Else {
            newOrder.Customer__c = myOpp.AccountID;
            newOrder.End_Customer__c = this.Opp.End_User__c;
        }  
 
        newOrder.Originating_Opportunity__c = this.opp.ID;
        newOrder.Summary__c= myOpp.Name;
        newOrder.Status__c = 'Created';
        newOrder.Date_of_Order__c = date.Today();
        newOrder.Required_Delivery_Date__c = reqDlvDate;
        newOrder.Requested_Ship_date__c = reqShipDate;
        newOrder.Type__c='Order';
        newOrder.Shipping_Charges__c = selShippingCharges;
        newOrder.Travel_Charges__c = selTravelCharges;
        newOrder.Purchase_Type__c = selPurchaseType;        
        
        insert newOrder; // insert the new order into DB

       //instantiate map to hold Order Record Types so we can create the correct type
//        map<id, recordtype> recordTypes = new map<id, recordtype>([select id, name from recordtype where sobjecttype='Opprtunity']);
        RecordType vmsOppRT = [ Select ID, Name from RecordType where SobjectType='Opportunity' and Name='VMS Opportunity' LIMIT 1];
        RecordType tranOppRT = [ Select ID, Name from RecordType where SobjectType='Opportunity' and Name='Transportation Opportunity' LIMIT 1];
        RecordType vmsOrdRT = [ Select ID, Name from RecordType where SobjectType='Orders__c' and Name='VMS Orders' LIMIT 1];
        RecordType tranOrdRT = [ Select ID, Name from RecordType where SobjectType='Orders__c' and Name='TAM Orders' LIMIT 1];
        System.debug('VMS Opp RT ID = '+vmsOppRT.id+', Trans Opp RT ID = '+tranOppRT.id);
        System.debug('This Opportunitys RT ID = '+myOpp.RecordTypeID);
        System.Debug('VMS Order RT ID = '+vmsOrdRT.id+', TAM Orders RT IT = '+tranOrdRT.id);
        IF (myOpp.RecordTypeID==vmsOppRT.id) {
//            newOrder.RecordType=[Select id FROM RecordType where SobjectType='orders__c' and name='VMS Orders' LIMIT 1];    
            newOrder.RecordType=vmsOrdRT;
            System.Debug('New Order Type (S/B VMS) = '+newOrder.RecordType);            
            update newOrder;
//        } ELSE IF (myOpp.RecordTypeID==recordTypes.get('Transportation Opportunity').id) {
        } ELSE IF (myOpp.RecordTypeID==tranOppRT.id){
//            newOrder.RecordType=[Select id FROM RecordType where SobjectType='orders__c' and name='TAM Orders' LIMIT 1];    
            neworder.RecordType=tranOrdRT;
            System.Debug('New Order Type (S/B TAM) = '+newOrder.RecordType);
            update newOrder;
        }