0

I have and object 'Car__c' in a package with namespace 'car'. There is a field name 'Price__c' and a lookup field name 'Car_Segment__c'.

To initialize 'Car__c' and assign values to its fields from a class in 'car' package, I am doing something like -

Car__c carobj = new Car__c();
carobj.Price__c = 1000000;
carobj.Car_Segment__c = '0034R00003bVDo1QAG'; // id assignment in this way is for illustration purpose only

There is another package with namespace 'tyre' having an object 'Tyre__c'.

Now I have extended the 'Car__c' object in the other package with namespace 'tyre' by adding an object 'car__Car__c there. There is a requirement to add a lookup to object 'Tyre__c' so I have also added it as a field in my extended object with referenceTo value as -

<referenceTo> tyre__Tyre__c </referenceTo>

I want to assign values to this field too. I am doing something like -

    Car__c carobj = new Car__c();
    carobj.Price__c = 1000000;
    carobj.Car_Segment__c = '0034R00003bVDo1QAG';
    
    //need to check whether the lookup field exists before assigning values
    SObject so = Schema.getGlobalDescribe().get('Car__c').newSObject();
    if (so.getSobjectType().getDescribe().fields.getMap().containsKey('tyre__Tyre__c')) {
       carobj.put('tyre__Tyre__c', '1034R08603bVDo1QAc');
    }
   insert carobj;

In the second scnario, is this the correct way to initialize the object and assign the values ? If not then can you please suggest a correct and efficient way ?

2
  • Are you having a problem or receiving an error of some kind? Your approach seems generally proper, but it's easier to help if we know what the problem is. Jun 4, 2021 at 12:49
  • @Jeremy I have edited the second part of code slightly. What I am doing is directly refering fields like (carobj.Car_Segment__c = '0034R00003bVDo1QAG';) for the fields in the same package whereas for the field in the other package I am using put operation before running an insert on carobj. I didn't notice any error as such but since I am very new to salesforce, I am not sure if this is the best way. Also, is assigning values using a direct reference to some fields while assigning using 'put' to others, a good practice ? Jun 4, 2021 at 13:19

1 Answer 1

1

Using newInstance is kind of redundant because you don't use created SObject instance.

Here are a few options which you can use

I think this one is the best option:

Car__c carobj = new Car__c();
carobj.Price__c = 1000000;
carobj.Car_Segment__c = '0034R00003bVDo1QAG';

//need to check whether the lookup field exists before assigning values

String fieldName = 'tyre__Tyre__c';
SObjectField field = Car__c.fields.getMap().get(fieldName);
if (field != null) {
    carobj.put(field, '1034R08603bVDo1QAc');
}
insert carobj;

Here is another option if you want something more dynamic

    Car__c carobj = new Car__c();
    carobj.Price__c = 1000000;
    carobj.Car_Segment__c = '0034R00003bVDo1QAG';
    
    //need to check whether the lookup field exists before assigning values
    if (Schema.getGlobalDescribe().get('Car__c').getDescribe().fields.getMap().containsKey('tyre__Tyre__c')) {
       carobj.put('tyre__Tyre__c', '1034R08603bVDo1QAc');
    }
   insert carobj;
3
  • Sounds good but could you please elaborate a little on the comment "Using newInstance is kind of redundant because you don't use created SObject instance." ? Jun 4, 2021 at 13:34
  • 1
    sure, to get info about fields you need either SObjectType or DescribeSobjectResult. In you code your full chain is Schema.getGlobalDescribe().get('Car__c').newSObject().getSobjectType().getDescribe().fields.getMap().containsKey('tyre__Tyre__c')), in it you can remove .newSObject().getSobjectType() and nothing will change. Is it more clear now?
    – ytiq
    Jun 4, 2021 at 14:13
  • Yes, I got it. Thank you so much for the answer and elaboration ! Jun 4, 2021 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.