• gaurav arora 33
  • NEWBIE
  • 10 Points
  • Member since 2016
  • Mr.
  • Deloitte

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
I am using the Below code to update the history tracking of lookup field to true
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        List<MetadataService.CustomField> cfList = new List<MetadataService.CustomField>();
                MetadataService.CustomField customField = new MetadataService.CustomField();
                customField.type_x = 'Lookup';
                customField.fullName = 'Case_Contact__c.Account__c';
                customField.label = 'Account';
                customField.relationshipLabel = 'Case_Contacts';
                customField.relationshipName = 'Case_Contacts';
                customField.referenceTo = 'Account';
                customField.trackHistory = true;
                cfList.add(customField);
                List<MetadataService.SaveResult> results = service.updateMetadata(cfList);

The code works fine. but the problem is if that lookup field has any lookup filter then that would get  deleted.
is there any way to dynamically update the history tracking of lookup field and keeping the lokkup filters intact.
I want to update the update the history tracking for Owner fields. but i am not able to do that. the below code is not working.
try{
    MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();
    service1.SessionHeader = new MetadataService.SessionHeader_element();
    service1.SessionHeader.sessionId = UserInfo.getSessionId();
    MetadataService.MetadataPort service = service1;
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'AG_PCM_Issue_Code__c.OwnerId';
    customField.type_x = 'Lookup';
    //customField.label = 'Owner';
    customField.relationshipLabel = 'Owner';
    customField.relationshipName = 'Owner';
    customField.referenceTo = 'User';
    customField.trackHistory = true;
    List<MetadataService.SaveResult> results = service.updateMetadata(new MetadataService.Metadata[] { customField });
    if(results[0].errors!=null)
    {
        List<String> messages = new List<String>();
        messages.add(
            (results[0].errors.size()==1 ? 'Error ' : 'Errors ') +
            'occured processing component ' + results[0].fullName + '.');
        for(MetadataService.Error error : results[0].errors)
            messages.add(
                error.message + ' (' + error.statusCode + ').' +
                ( error.fields!=null && error.fields.size()>0 ?
                 ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
        if(messages.size()>0)
            system.debug('errorstoh:::'+String.join(messages, ' '));
    }
}
catch(Exception e){
    system.debug('error: '+e.getMessage());
}
But if i use the same code for any custom lookup field then it is working.
try{
    MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();
    service1.SessionHeader = new MetadataService.SessionHeader_element();
    service1.SessionHeader.sessionId = UserInfo.getSessionId();
    MetadataService.MetadataPort service = service1;
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'AG_Issue_Code__c.AG_Issue_Category__c';
    customField.type_x = 'Lookup';
    customField.label = 'Issue Category';
    customField.relationshipLabel = 'AG_Issue_Codes';
    customField.relationshipName = 'AG_Issue_Codes';
    customField.referenceTo = 'AG_Issue_Category__c';
    customField.trackHistory = true;
    List<MetadataService.SaveResult> results = service.updateMetadata(new MetadataService.Metadata[] { customField });
    if(results[0].errors!=null)
    {
        List<String> messages = new List<String>();
        messages.add(
            (results[0].errors.size()==1 ? 'Error ' : 'Errors ') +
            'occured processing component ' + results[0].fullName + '.');
        for(MetadataService.Error error : results[0].errors)
            messages.add(
                error.message + ' (' + error.statusCode + ').' +
                ( error.fields!=null && error.fields.size()>0 ?
                 ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
        if(messages.size()>0)
            system.debug('errorstoh:::'+String.join(messages, ' '));
    }
}
catch(Exception e){
    system.debug('error: '+e.getMessage());
}

 
Hi,

We have  and automated code for updating the page layout through Apex Metadata API for example in like in below link.
https://trailhead.salesforce.com/modules/apex_metadata_api/units/apex_metadata_api_updates

Can we have some similar code for enabling the field history tracking of some fields.

Thanks
 
I have a Code in which i am using apex:inputField for lookup field. when i am getting this field value in script it gives me the name of the record selected.
I want to get the ID of the record selected either in javascript variable or in apex controller.
Page------------------------------->>

<apex:page Controller="ApexParamController">
    <script>
    function fire(){
        var fr=document.getElementById('{!$Component.frm.acc}').value;
        alert('test:: '+fr);
    }
    </script>
    <apex:form id="frm">
        <apex:inputfield value="{!contact.AccountId}" id="acc"/>
        <apex:commandButton onclick="fire();" value="Save" action="{!print}" />
    </apex:form>
</apex:page>
 
Class------------------->>

public class ApexParamController {
    Public Contact contact{get;set;}
    public void print(){
        contact = new Contact();
        system.debug('Account::'+contact.AccountID);
    }
}

 
I have a Code in which i am using apex:inputField for lookup field. when i am getting this field value in script it gives me the name of the record selected.
I want to get the ID of the record selected either in javascript variable or in apex controller.
Page------------------------------->>

<apex:page Controller="ApexParamController">
    <script>
    function fire(){
        var fr=document.getElementById('{!$Component.frm.acc}').value;
        alert('test:: '+fr);
    }
    </script>
    <apex:form id="frm">
        <apex:inputfield value="{!contact.AccountId}" id="acc"/>
        <apex:commandButton onclick="fire();" value="Save" action="{!print}" />
    </apex:form>
</apex:page>
 
Class------------------->>

public class ApexParamController {
    Public Contact contact{get;set;}
    public void print(){
        contact = new Contact();
        system.debug('Account::'+contact.AccountID);
    }
}

 
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, too small amount for Opprtunity: []