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
Oleg IshchukOleg Ishchuk 

Failed deployment due to Metadata.Operation.retrieve

Hello, everyone! I have following part of code:
User-added imageWhen I validate code during deployment I get errors. Errors are - 'System.ListException: List index out of bounds: 0' on line 77. When I try to use this code using an anonymous block, the accountLayout gets the first component. But during deployment, it seems like Metadata.Layout isn't visible. How can I solve this issue? 
AbhishekAbhishek (Salesforce Developers) 
Hi Oleg,

Salesforce have an official document regarding this error "System.ListException: List index out of bounds: 0"

https://help.salesforce.com/articleView?id=000329067&type=1&mode=1

If you need further suggestions to go through the below blogs,

https://salesforce.stackexchange.com/questions/13670/solve-system-listexception-list-index-out-of-bounds-0

https://developer.salesforce.com/forums/?id=906F000000092gOIAQ

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Oleg IshchukOleg Ishchuk
Yeah, I know about why this issue usually happens. Probably, I created my question little bit incorrectly. Why is there nothing in list which is called components? I should get there metadata from a specific layout but don't get anything.
David Zhu 🔥David Zhu 🔥
Is the error happening at running test method?
do you add custom metadata type layout in the deployment package? Or has it already on the destination org?
Oleg IshchukOleg Ishchuk
The test runs successfully. 
No, I have no custom metadata type.
I have several page layouts. Metadata.Operation.retrieve gets a specific layout which I specify in LAYOUT_NAME constant. You can see it in the code above. But it is not custom metadata, it is standard metadata. And when I test this code in sandbox, everything is properly. But during deployment, I have an error. I resolved this issue using Test.isRunningTest. But it is not a good solution. 
David Zhu 🔥David Zhu 🔥
If you resovled the error by using  Test.isRunningTest, it means this issue is caused by test method. You may check if the layout specified in Layout_name exists in your deployment package or if it is already on the org.
 
Oleg IshchukOleg Ishchuk
This layout already exists in prod. It seems like I should emulate layout on test.