0
trigger ChangeRecordType on Product2 (after update) {
   for(Product2 product: Trigger.new) {
      if(product.Available_for_lease__c == false) {
        product.RecordTypeId = 'Products Archive';     
        }
 }

This is my code for trigger updateleaseforhide

trigger UpdateLeaseForHide on Opportunity ( After update) {

Opportunity[] wonOpps = new Opportunity[0];
    for(Opportunity record: Trigger.new) {
        if(record.StageName == 'closed won') {
            wonOpps.add(record);
        }
    }
        if(wonOpps.isEmpty()) {
            return;
        }
   Product2[] products = [SELECT Id FROM Product2 WHERE Id IN (SELECT Product2Id FROM OpportunityLineItem WHERE OpportunityId IN :wonOpps)];
   for(Product2 product: products) {
     product.Available_for_Lease__c = false;
   }
    update products;

}

My code above was to change the record type of my product into 'product archive' but when i tested it, there was an error. BTW, I also have a trigger for updateleaseforhide that will uncheck the checkbox on the products object via status 'closed won' in the opportunities object.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger UpdateLeaseForHide caused an unexpected exception, contact your administrator: UpdateLeaseForHide: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t61000000eZwTAAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ChangeRecordType: execution of AfterUpdate caused by: System.StringException: Invalid id: Products Archive Trigger.ChangeRecordType: line 4, column 1: []: Trigger.UpdateLeaseForHide: line 16, column 1

enter image description here

2 Answers 2

1

In given line:

product.RecordTypeId = 'Products Archive';

You should pass RecordType ID not its name. It should look like this:

product.RecordTypeId = '124000000043deAeYc';

Or you can use Sobject describe to get ID dynamically

product.RecordTypeId = Schema.SObjectType.Product2.getRecordTypeInfosByName().get('Product Archive').getRecordTypeId();
6
  • where can i find the id for my record type? Sep 25, 2015 at 7:31
  • @gelay upfated my answer. You can find record Type ID on Product2 sobject definition' record type section. Or dynamically just by passing record Type Developer Name
    – Ashwani
    Sep 25, 2015 at 7:33
  • if im going to use record type id, where can i find it? Sep 25, 2015 at 7:39
  • It is mentioned in the answer from @Ashwani it is this line product.RecordTypeId = Schema.SObjectType.Product2.getRecordTypeInfosByName().get('Product Archive').getRecordTypeId(); Sep 25, 2015 at 7:41
  • I have already changed it but the error still persists.. i think it has to do with my updateleaseforhide trigger.. Sep 25, 2015 at 7:45
0

I have already figured it out. Instead of creating another trigger, I inserted this trigger into updateleaseforhide so that when the product is updated, the triggers will be activated. I think the error is that after the update in the first trigger, it will be forced to update again because of the second trigger. Anyways thanks for your help guys :)

    trigger UpdateLeaseForHide on Opportunity ( After update) {
    Opportunity[] wonOpps = new Opportunity[0];
    for(Opportunity record: Trigger.new) {
        if(record.StageName == 'closed won') {
            wonOpps.add(record);
        }
    }
        if(wonOpps.isEmpty()) {
            return;
        }
   Product2[] products = [SELECT Id FROM Product2 WHERE Id IN (SELECT Product2Id FROM OpportunityLineItem WHERE OpportunityId IN :wonOpps)];
   for(Product2 product: products) {
     product.Available_for_Lease__c = false;  
     product.RecordTypeId = '01261000000GpJt';     
     }
    update products;
     }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .