Activity Forums Salesforce® Discussions A Custom Object named “Training__c” has field “Trainer__c”. You have set some default value in that field. Will that default value apply to new record created by Salesforce apex code ?

  • A Custom Object named “Training__c” has field “Trainer__c”. You have set some default value in that field. Will that default value apply to new record created by Salesforce apex code ?

    Posted by Aman on September 22, 2018 at 6:00 pm

    A Custom Object named “Training__c” has field “Trainer__c”. You have set some default value in that field. Will that default value apply to new record created by Salesforce apex code ?

    Parul replied 6 years, 3 months ago 3 Members · 2 Replies
  • 2 Replies
  • shariq

    Member
    September 22, 2018 at 6:01 pm

    After API 20, it should automatically populate However there is known issue for same here, click here if it impacts you.
    Workaround :
    If Default value of field is not getting populated by Apex then we have to use “Dynamic Apex”.  Create instance of object from sObjectType like shown below:

    view source
    print?
    1
    Training__c tr= (Training__c) Training__c.sObjectType.newSObject(null, true);
    2

    3
    //Check if Value in field "Trainer__c" is default value
    4
    System.assertEquals('Jitendra', tr.Trainer__c);

  • Parul

    Member
    September 22, 2018 at 6:08 pm

    You should automatically populate however there is known issue for same here, click here if it impacts you:

    Training__c tr= (Training__c) Training__c.sObjectType.newSObject(null, true);

    //Check if Value in field "Trainer__c" is default value
    System.assertEquals('Jitendra', tr.Trainer__c);

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos