Activity Forums Salesforce® Discussions What is the difference between Id(objString) and Id.valueOf(ObjString) in Salesforce?

  • What is the difference between Id(objString) and Id.valueOf(ObjString) in Salesforce?

    Posted by Mohit on September 15, 2016 at 2:08 pm

    Hi All,

    What is the difference between Id(objString) and Id.valueOf(ObjString)?

    I want to typecast the ObjString into the Id which way is better to do that?

    Please give suggestion

    Abhinav replied 6 years ago 2 Members · 1 Reply
  • 1 Reply
  • Abhinav

    Member
    September 15, 2016 at 4:59 pm

    Hi Mohit,

    The first is casting. The second is calling a static method on the Id class.

    One major advantage of the former is that it is more null safe.

    Id idVal1 = (Id)null; // works
    Id idVal2 = Id.valueOf(null); // blows up

    First one is better.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos