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
snovvblindsnovvblind 

Creating a record in an unrelated object using Triggers

I'm a quick learner. I'd just like help this one time, and I'll pick up on the material pretty quickly.

 

Here's the business logic, I'd like:

 

  1. Check whether a date on a field on the Inventory Object is equal to or greater than today().
  2. If its equal to or greater than today(), create a record in an unrelated custom object (Tasks Object), and insert a whole bunch of pre-populated fields
  3. Upon creation of this new record in the Tasks Object, if I set the status field to "Done", I'd like another trigger to first increment a number field in the Inventory Object.
  4. Delete the Tasks Object that was created, after the increment to the Inventory Object is completed.

If you could help, that'd be great.

snovvblindsnovvblind

Well, I got this far, but I really need some help:

 

Error:Apex trigger CreateNewRecurring caused an unexpected exception, contact your administrator: CreateNewRecurring: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.CreateNewRecurring: line 13, column 1

 

trigger CreateNewRecurring on Fuel_Gauge__c (Before Insert,before update) {
    List <Fuel_Gauge__c> RecToInsert = new List <Fuel_Gauge__c> ();
    for (Fuel_Gauge__c FG : Trigger.new) {
    Mind_Wrap_6_Stardust__c MW6S = new Mind_Wrap_6_Stardust__c ();
        if(FG.Status__c == 'Maintenance') {
            MW6S.Name = FG.Name;
            MW6S.IGPS__c = 'a00E0000002D0hYIAS';
            MW6S.T_Net_Hours_Required__c = 1;
            RecToInsert.add(FG);
        }
    }
   try {
        insert RecToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    } 
}

 

Jerun JoseJerun Jose
You said you wanted to insert a new task, but you are trying to insert a new Fuel_Gauge__c record.
snovvblindsnovvblind

Yep, dually noted.

 

I fixed it with the following code:

 

trigger CreateNewRecurring on Fuel_Gauge__c (after update) {
    
    List <Mind_Wrap_6_Stardust__c> RecToInsert = new List <Mind_Wrap_6_Stardust__c> ();
    for (Fuel_Gauge__c FG : Trigger.new) {
        if (FG.Status__c == 'Maintenance') {
            Mind_Wrap_6_Stardust__c MW6S = new Mind_Wrap_6_Stardust__c ();
            MW6S.Name = FG.Name;
            MW6S.IGPS__c = 'a00E0000002D0hYIAS';
            MW6S.T_Net_Hours_Required__c = 1;
            MW6S.Launch_Date__c = FG.Refresh_Date__c;
            RecToInsert.add(MW6S);
        }
    }
    try {
        insert RecToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

 

Goku ZeusGoku Zeus
 Một nhà cái uy tín phải được cấp phép và quản lý bởi cơ quan có thẩm quyền. Điều này đảm bảo rằng nhà cái đang hoạt động hợp pháp và họ tuân theo các tiêu chuẩn cao về tính công bằng và minh bạch.
 https://bossnhacai.me (https://bossnhacai.me)