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
SFmaverickSFmaverick 

Can I make two records of the same object type the same with "="?

I'm asking if this will work...

 

trigger ConsecutiveShifts on Shift__c (before insert) {

 

Integer Counter = 1;


for(Shift__c a : trigger.new)
{
    for(Integer I = a.Consecutive_Shifts; I > 1; I--){
        Shift__c b = New Shift__c();
        b = a;

        b.date__c += Counter;

        b.Consecutive_Shifts = 1;

        Counter++;

        insert(b);

    }

    a.Consecutive_Shifts = 1;

}

 

Basically, the user is going to enter a lot of information to create a record for the Object Shift. The field Consecutive shift will by default be one, so by default only one shift will be created. However, if the user enters something higher than 1, then I want this trigger to take all the information they put in intially and create another object of type Shift for each number over 1. The only difference in these records will be the date, each record will have a date value of one day more than the last.

 

My idea is that the line:

b = a;

Will transfer everything in a and put it in b. Afterwards, the only fields I don't want the same in b are date and consecutive shifts, so I change them after inserting the values of a. I create the shift, and then I test the loop counter again.

Best Answer chosen by Admin (Salesforce Developers) 
SFmaverickSFmaverick

I figured everything out :).

 

I had no luck with the clone function or with a direct assignment of record a to record b.

 

Thanks for the help!

All Answers

Paul.FoxPaul.Fox

If I remember correctly, this will work but you have to add the following:

b.Id = null;

 

Since a already has an ID, you have to remove it before creating b.

_Prasu__Prasu_

you can clone record "a" and then assign it to "b".

 

clone methods take one argument which is boolean, depending upon its value Id's / lookup fields value are preserved in the cloned record.

SFmaverickSFmaverick

I'm having trouble getting it to go through the Loop at all.

 

Consecutive_Shifts__c is a number field that only allows a single digit to be entered. It's put to 0 decimal places.

 

However, I can't manage to get the For loop to run. I've tried a few different methods.

 

Shouldn't this work...

 

trigger ConsecutiveShifts on Shift__c (after insert) {
Integer Counter = 1;
for(Shift__c a : trigger.new)
{
    for(Integer I = integer.valueOf(a.Consecutive_Shifts__c); I > 1; I--){
        Shift__c b = New Shift__c();
        b = a;
        b.date__c += Counter;
        b.Consecutive_Shifts__c = 1;

        b.id = null;
        Counter++;
        insert(b);
    }
    a.Consecutive_Shifts__c = 1;

}
}

 

It gives the error message, "ConsecutiveShifts: execution of AfterInsert caused by: System.TypeException: Invalid integer: 2.0: Trigger.ConsecutiveShifts: line 5, column 21"

SFmaverickSFmaverick

Well scratch that, I just made it into a While loop instead.

 

Now it's throwing me off at

b.date__c += Counter;
With the error message, "ConsecutiveShifts: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.ConsecutiveShifts: line 8, column 9"

 

Date is not read only. It's a required field and it's editable by everyone. What would make this error message occur?

SFmaverickSFmaverick

I figured everything out :).

 

I had no luck with the clone function or with a direct assignment of record a to record b.

 

Thanks for the help!

This was selected as the best answer