1

i have 2 custom fields named color__c in the object Case and Account, before i create a case, i would like to copy the value in Account.color__c to Case.color__c in the trigger before inserting whitout creating a formula field, just by calling a copyColorField method and using the accountId relationship, i would like to know if it's possible without using another soql in the after insert trigger for exemple ?

public static void copyColorField(List<Case> caseListToEdit){
    

}


// exemple with two accounts having black and red color value in color__c field
Case c1 = new case(Subject = 'Test 1', AccountId = '0011l0000101uM9854');
Case c2 = new case(Subject = 'Test 2', AccountId = '0011l0000101uM9844');
Case [] inputCases = new case[]{c1,c2};
insert inputCases; 

// expected result after insert
--> Case:{Subject='Test 1', AccountId=0011l0000101uM9854, color__c='black'}, Case:{Subject='Test 2', AccountId=0011l0000101uM9844, color__c='red'}

1 Answer 1

6

i would like to know if it's possible without using another soql in the after insert trigger for exemple ?

No.

A trigger only includes the field values of the record you're on, not any records it links to. To get values off parent records, you have to perform a SOQL.

If you want to avoid a SOQL query, you'll need to use a formula field. You can have a formula field on the case that you can use in the trigger to populate a text field.

BTW, this is logic you'll want to perform in a before trigger, not an after trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.