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
Chiyaan AnanthChiyaan Ananth 

Update account page layout programatically

Hello Everyone,

 

I am trying to update accounts page layout with a custom link.  I managed to create a custom link which is great.  Now I am able to retrieve the page layout through retrieve call.  Which gives a zip file,  is there a way I could update the layout through the Metadata API?  So far I have tried this,

 

     Layout co = new Layout();

        co.setFullName("Account-Trial2");

        

        LayoutSection layoutSection = new LayoutSection();

        layoutSection.setStyle(LayoutSectionStyle.CustomLinks);

        layoutSection.setCustomLabel(true);

        layoutSection.setLabel("Custom Links");

        LayoutColumn layoutColumn = new LayoutColumn();

        LayoutItem[] layoutItemList = new LayoutItem[1];

        LayoutItem layoutItem = new LayoutItem();

        layoutItem.setCustomLink("Infosmart_Results");

        layoutItemList[0] = layoutItem;

        layoutColumn.setLayoutItems(layoutItemList);

        LayoutColumn[] layoutColumnList = new LayoutColumn[1];

        layoutColumnList[0] = layoutColumn;

        layoutSection.setLayoutColumns(layoutColumnList);

        LayoutSection[] layoutSectionList = new LayoutSection[1];

        layoutSectionList[0] = layoutSection;

        co.setLayoutSections(layoutSectionList);

        

        UpdateMetadata updateMetadata = new UpdateMetadata();

        updateMetadata.setMetadata(co);

        updateMetadata.setCurrentName("Trial");   // co.getFullName(); 

 

This code leads to the following exception

 

Error status code: FIELD_INTEGRITY_EXCEPTION

Error message: Layout must contain an item for required layout field: Name

The object state is Error

 

What am I missing here.  Any help will be greatly appreciated.

 

Regards

Ananth Chellathurai

Blog 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Chiyaan AnanthChiyaan Ananth

Hi All,

 

Managed to code this some how.  Using the retrieve call got all layout files parsed through it using dom4j and updated a new custom link to it.  Make sure to remove the emply <layoutColumn\> while adding a new link.

 

Also the <style> has to be the last element in the layout section.

 

Regards

Ananth Chellathurai

Blog 

All Answers

SuperfellSuperfell
You'll need to read the current layout object, add your new item to it then call update, you can't just make an update call with the addition to the layout.
Chiyaan AnanthChiyaan Ananth

Hello Simon,

 

Thanks for the reply.  How do I read the current layout?  In the metadata API read is only through retrieve() call I believe.  Which brings a zip file,  do I have to read the layout file from the zip?  Can you help me with an URL or sample code?

 

Thanks in advance.

 

Ananth Chellathurai

Blog 

Chiyaan AnanthChiyaan Ananth

Hi All,

 

Managed to code this some how.  Using the retrieve call got all layout files parsed through it using dom4j and updated a new custom link to it.  Make sure to remove the emply <layoutColumn\> while adding a new link.

 

Also the <style> has to be the last element in the layout section.

 

Regards

Ananth Chellathurai

Blog 

This was selected as the best answer
GingaCodeMonkeyGingaCodeMonkey


Hi Ananth,

 

Hopefully you will still be able to help.

Could you please upload a version of this working so that we have a complete reference to how you completed the task.

 

Thanks,

 

Adrian