Andy in the Cloud

From BBC Basic to Force.com and beyond…

119 thoughts on “Scripting the Apex Metadata API and Batch Apex Support

  1. mahesh10811's avatar

    Hi Andrew Fawcett,

    Here how to add my object related pagelayout

    MetadataService.Layout layout =
    (MetadataService.Layout) service.readMetadata(‘Layout’,
    new String[] {”rajesh__’+ objectname+’__c’-objectname Layout’ }).getRecords()[0];//Here only problem i think

    My object like:’rajesh__’+ objectname+’__c’ and
    Object creation time created page layout name is: objectname Layout

    help me……..

    • Andrew Fawcett's avatar

      Are you sure the layout name is correct? Check the name with those listed in the Salesforce Workbench, it can list metadata types.

  2. mahesh10811's avatar

    Hi Andrew Fawcett,

    Object name:’rajesh__’+ objectName+’__c’//i added but this name also not accepted( __ not accepted)
    pagelayoutname :-objectname layout

    But object null error will comming.please once add my object name below code
    MetadataService.Layout layout =
    (MetadataService.Layout) service.readMetadata(‘Layout’,
    new String[] { ‘Test__c-Test Layout’ }).getRecords()[0];

    System.NullPointerException: Attempt to de-reference a null object
    Error is in expression ‘{!createField}’ in component in page rajesh:createfields

    Class.rajesh.MetadataController11.updateFieldpageLayout: line 882, column 1

    line 882: layout.layoutSections[0]. layoutColumns[0].layoutItems.add(layoutItem);

    Class.rajesh.MetadataController11.createField: line 243, column 1
    line 243: updateFieldpageLayout();//i call above code using this method

    help me …..

  3. mahesh10811's avatar

    Hi Andrew Fawcett,

    I saw Layout name is like below

    objectname__c-objectname Layout

    createdById: 00590000002M4B3AAK
    createdByName: rajesh k
    createdDate: 2014-06-26T08:07:27.000Z
    fileName: layouts/objectname__c-objectname Layout.layout
    fullName: Ramobj__c-objectname Layout

  4. mahesh10811's avatar

    Hi Andrew Fawcett,

    please help me this is only last.

    • Andrew Fawcett's avatar

      I have a feeling it won’t be, let’s move this chat over to a GitHub issue, it will be much easier to chat there. Thank you!

  5. mahesh's avatar

    Hi Andrew Fawcett,
    How to perform List operation on MetadataService.LayoutItem .In my layout more than one fields are there that’s why attempt to dereference null object error will came

  6. AngM's avatar

    Hi Andy, the new zip folder does not consist of the MetadataCreateJob.cls. I got this error while trying to work the code for batch.
    Error: Compile Error: Invalid type: MetadataCreateJob.Item

    • Andrew Fawcett's avatar

      This class is no longer required as create operation is now a sync process and not async. So the class is not needed. You can remove references to it and instead in your code call the new create method which will perform the operation immediatly.

  7. Haroon Ahmad's avatar

    Andy,
    This approach works fine for synchronous Apex (VF Controller, or anonymous Apex), but if I try to use this in asynchronous apex (Batch Apex), then I get the following error:

    Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor=

    Looks like it doesn’t like the Session ID, it gets using UserInfo.getSessionID(), in Batch Apex.

    Thanks for your help!

    -Haroon

    • Andrew Fawcett's avatar

      You need to pass your session id into the batch Job

      • Haroon Ahmad's avatar

        That worked beautifully, Andy. I have a need to invoke this batch from an email service. When I try that, I run into the same issue. Can you suggest anyway, I can invoke this successfully, from an email service?

        Thanks!

      • Andrew Fawcett's avatar

        You will need to login again via login Api to get a session, or use an oauth token, or try named credentials. The later I have not tried but could be the best option, might try it this week and blog if it works

  8. Haroon Ahmad's avatar

    Thanks, Andrew! Will look forward to that, and try it myself as well.

    -Haroon

  9. vijay's avatar

    Can i know how to build this for metadata picklist field values to sync to other system from batch process . in this blog batch job class show not available.

Leave a comment