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
daniel1110daniel1110 

Trigger: Update record of different record type with same field values

Hi. I am trying to update some fields with values from another record with a different record type. Nothing happens when I try the following code. Pls help

trigger createOrderforABFSCB1 on Stem_Cell_Bank__c (after update) {

	// Collect all updated SCB Ids
	Set<Id> setSCBIds = new Set<Id>();
    
	// Collect all SCB Id which has stem cell banked date populated
	for(Stem_Cell_Bank__c scb : Trigger.new) {
		if (scb.Stage__c == 'Stem Cells Banked' && Trigger.oldMap.get(scb.Id).Stage__c != 'Stem Cells Banked')  { 
			setSCBIds.add(scb.Id);
        }
    }
    
    // List for collecting Orders to update
    List<ChargentOrders__ChargentOrder__c> colist = new List<ChargentOrders__ChargentOrder__c>();
    
	// Iterate all the Orders of SCB
	for(ChargentOrders__ChargentOrder__c o : [SELECT Id, Account__c, Stem_Cell_Bank__c, ChargentOrders_Expiration_Month__c, ChargentOrders_Number__c,
                                         ChargentOrders_Type__c, ChargentOrders__Payment_Method__c, RecordTypeId 
										 FROM ChargentOrders__ChargentOrder__c 
										 WHERE Stem_Cell_Bank__c IN: setSCBIds]) {
        if(o.RecordTypeId == '012E0000000VPOC') {
    		colist.add(new ChargentOrders__ChargentOrder__c(Id = o.Id, RecordTypeId = '012E0000000VPOH', ChargentOrders_Number__c = o.ChargentOrders_Number__c));
        }
    }
	if(!colist.isEmpty()) {
		update colist;
    }

}


James LoghryJames Loghry
Have you done much debugging on this so far?  E.g. Have you printed out the results from the query in line 17 to see if any records are returned?  If anything actually gets added to the list at line 22?

Also, it's a horrible idea to hardcode Ids of any type, RecordType or otherwise.  Verify that the Ids are correct, but I would stick them in a custom setting so that you can change them easily between production and sandbox instances if need be.
daniel1110daniel1110
Hi James - Thank you for your suggestions. I do receive an output on line 22.

I tried to taking out the hardcode Ids, but now left with an error: 16:58:22:285 USER_DEBUG [47]|DEBUG|System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: a0QE0000003NfYt: []

Here's my updated code. Any other suggestions?
trigger createOrderforABFSCB1 on Stem_Cell_Bank__c (after update) {
    
    // Query for Order record types
    List<RecordType> otypes = [SELECT Name, Id FROM RecordType WHERE sObjectType = 'ChargentOrders__ChargentOrder__c' and isActive=true];
   
	// Collect all updated SCB Ids
	Set<Id> setSCBIds = new Set<Id>();
    
	// Collect all SCB Id which has stem cell banked date populated
	for(Stem_Cell_Bank__c scb : Trigger.new) {
		if (scb.Stage__c == 'Stem Cells Banked' && Trigger.oldMap.get(scb.Id).Stage__c != 'Stem Cells Banked')  { 
			setSCBIds.add(scb.Id);
        }
    }
    
    // List for collecting Orders to update
    List<ChargentOrders__ChargentOrder__c> colist = new List<ChargentOrders__ChargentOrder__c>();
    
	// Iterate all the Orders of SCB
	for(ChargentOrders__ChargentOrder__c o : [SELECT Id, Account__c, Stem_Cell_Bank__c, ChargentOrders__Card_Expiration_Month__c, 
        								 ChargentOrders__Card_Expiration_Year__c, ChargentOrders__Card_Number__c, ChargentOrders__Card_Security_Code__c, 
                                         ChargentOrders__Card_Type__c, ChargentOrders__Payment_Method__c, RecordTypeId 
										 FROM ChargentOrders__ChargentOrder__c 
										 WHERE Stem_Cell_Bank__c IN: setSCBIds]) {
                                            // system.debug(o);
        // Create a map between the record type name and Id for easy retrieval
    	Map<String, String> orderRecordTypes = new Map<String, String>{};
    	for(RecordType rt: otypes)
        	orderRecordTypes.put(rt.Name, rt.Id);
            
            if(o.RecordTypeId==orderRecordTypes.get('Stem Cell Bank Enrollment Fee')) {
    		colist.add(new ChargentOrders__ChargentOrder__c(Id = o.Id, RecordTypeId = '012E0000000VPOH', ChargentOrders__Card_Number__c = o.ChargentOrders__Card_Number__c));
        }
    }
	if(!colist.isEmpty()) {
		update colist;
    }

}