+ Start a Discussion
fourfourfunfourfourfun 

Simple apex trigger update query

Hi, 

 

I have an established piece of code. What it is doing is writing, on save, from one record (week__c) in an object, and creating/updating a series of records in another one (PCM_Time_Record__c).

 

The only bugbear is that it is writing the other records as whoever is triggering the Apex. I need it to assign the written records as the owner of the source.

 

The code looks like this:

 

 List<PCM_Time_Record__c> insertBlocks = new List<PCM_Time_Record__c>
     {new PCM_Time_Record__c(Time_Block_Name__c='Monday AM1', Type__c=week.Monday_AM1__c,      Date__c=week.Week_Commencing__c, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),

 

As you can see, it is doing a simple create.

 

 

I assumed it was as simple as adding another clause to the bracket that said OwnerID = week__c.OwnerID, but that is throwing up an "Expect ID error".

 

 

 

Here is the full trigger in case anyone needs it:

 

 

trigger createTimeBlocks on Week__c (after insert, after update) {
   
    //hours in the day, split into quarters
    decimal hours = 7.5/4;
    String recordTypeId = [Select Id from RecordType where Name = 'Time Block' and SobjectType = 'PCM_Time_Record__c' limit 1].id;
    PCM_Time_Record__c timeBlock;
        
    for (Week__c week : Trigger.new)
    {
     //For each week record being saved
     if(Trigger.isUpdate)
     {
        //If the Week record has been saved previously, then check for existing timeBlocks. These should also exist.
        List<PCM_Time_Record__c> timeBlocks = [select id from PCM_Time_Record__c where Weekly_Time__c = :week.Id];
        if(!timeBlocks.isEmpty())
        {
            delete timeBlocks;
        }   

     }
     //Create timeblocks.
     List<PCM_Time_Record__c> insertBlocks = new List<PCM_Time_Record__c>
        {new PCM_Time_Record__c(Time_Block_Name__c='Monday AM1', Type__c=week.Monday_AM1__c, Date__c=week.Week_Commencing__c, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Monday AM2', Type__c=week.Monday_AM2__c, Date__c=week.Week_Commencing__c, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Monday PM1', Type__c=week.Monday_AM2__c, Date__c=week.Week_Commencing__c, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Monday PM2', Type__c=week.Monday_AM2__c, Date__c=week.Week_Commencing__c, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Tuesday AM1', Type__c=week.Tuesday_AM1__c, Date__c=week.Week_Commencing__c+1, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Tuesday AM2', Type__c=week.Tuesday_AM2__c, Date__c=week.Week_Commencing__c+1, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Tuesday PM1', Type__c=week.Tuesday_PM1__c, Date__c=week.Week_Commencing__c+1, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Tuesday PM2', Type__c=week.Tuesday_PM2__c, Date__c=week.Week_Commencing__c+1, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Wednesday AM1', Type__c=week.Wednesday_AM1__c, Date__c=week.Week_Commencing__c+2, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Wednesday AM2', Type__c=week.Wednesday_AM2__c, Date__c=week.Week_Commencing__c+2, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Wednesday PM1', Type__c=week.Wednesday_PM1__c, Date__c=week.Week_Commencing__c+2, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Wednesday PM2', Type__c=week.Wednesday_PM2__c, Date__c=week.Week_Commencing__c+2, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Thursday AM1', Type__c=week.Thursday_AM1__c, Date__c=week.Week_Commencing__c+3, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Thursday AM2', Type__c=week.Thursday_AM2__c, Date__c=week.Week_Commencing__c+3, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Thursday PM1', Type__c=week.Thursday_PM1__c, Date__c=week.Week_Commencing__c+3, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Thursday PM2', Type__c=week.Thursday_PM2__c, Date__c=week.Week_Commencing__c+3, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Friday AM1', Type__c=week.Friday_AM1__c, Date__c=week.Week_Commencing__c+4, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Friday AM2', Type__c=week.Friday_AM2__c, Date__c=week.Week_Commencing__c+4, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Friday PM1', Type__c=week.Friday_PM1__c, Date__c=week.Week_Commencing__c+4, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Friday PM2', Type__c=week.Friday_PM2__c, Date__c=week.Week_Commencing__c+4, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Saturday AM1', Type__c=week.Saturday_AM1__c, Date__c=week.Week_Commencing__c+5, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Saturday AM2', Type__c=week.Saturday_AM2__c, Date__c=week.Week_Commencing__c+5, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Saturday PM1', Type__c=week.Saturday_PM1__c, Date__c=week.Week_Commencing__c+5, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Saturday PM2', Type__c=week.Saturday_PM2__c, Date__c=week.Week_Commencing__c+5, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Sunday AM1', Type__c=week.Sunday_AM2__c, Date__c=week.Week_Commencing__c+6, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Sunday AM2', Type__c=week.Sunday_AM2__c, Date__c=week.Week_Commencing__c+6, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),
         new PCM_Time_Record__c(Time_Block_Name__c='Sunday PM1', Type__c=week.Sunday_AM2__c, Date__c=week.Week_Commencing__c+6, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25),  
         new PCM_Time_Record__c(Time_Block_Name__c='Sunday PM2', Type__c=week.Sunday_PM2__c, Date__c=week.Week_Commencing__c+6, Weekly_Time__c = week.Id,RecordTypeId=recordTypeId, Duration_hours__c=hours, Proportion_Of_Day__c=0.25)
        };
     insert insertBlocks;
    }
}

 

 

Thanks for any help!

ManoharSFManoharSF
Try Owner = week__c.OwnerID
fourfourfunfourfourfun

Yeah, that is what I thought, but I get:

Error: Compile Error: Field is not writeable: Owner at line 23 column 41

If I try OwnerID, I get:

Error: Compile Error: Invalid initial expression type for field PCM_Time_Record__c.OwnerId, expecting: Id at line 23 column 43

Avidev9Avidev9

Well you are doing one basic mistake.

 

Have a close look at the below

 

 OwnerID = week__c.OwnerID

 

Week__c seems to be a object. But to get the value of OwnerId, You need a record. Something like 

//this is just an example

List<Week__c> weeks = [SELECT Id,OwnerId FROM Week__c];

 OwnerID = weeks[0].OwnerID

fourfourfunfourfourfun

Ok, I'll have to go and have a play in Sandbox. My skills extend to editing others work, once it gets into doing things like this, I get lost very easily!