My question is pretty much the same as here. But the difference is I'll have to do it generically. As in the object type is sent as text and I'll have to typecast in the code. Eg.,

//create an in-memory instance of code, with the external id set
//Suggested Idea -> Code__c code = new Code__c(External_Id__c = 'EXTERNAL_ID_HERE');

//Not sure of this
sObject obj = new sObject();
obj.put('External_Id__c' ,'EXTERNAL_ID_HERE');

//create an instance of Opportunity, and point to the instance of code created above
Opportunity op = new Opportunity(Name = 'Test Op', StageName = 'Open', ... );

//Not sure of this either, but this is what we are trying to achieve
op.put('Oppoortunity_Code__r', ('OBJECT_TYPE_AS_STRING')  obj);

upsert op;

I'm unable to typecast since I've have it here as a string variable.

[EDIT]: Let me clarify my scenario.

I've three things in hand.

  1. The Object type I'm trying to update (Opportunity here).
  2. The external Id of the lookup object(External Id of the Opp Code).
  3. The field to be updated(using GetDescribe and the Object Type).

I need to identify the Opp code matching the external Id and then update its value in Opp. Now, it needs to be generic. As in it should work for any lookup field update on any object(provided there is a lookup field and an external Id in the child Object).

Thanks in advance!

  • I'm not sure I understand what you're trying to achieve here. The relationship field (Oppoortunity_Code__r) must be of a concrete type, so I don't know why you need a dynamic cast. Sep 17, 2014 at 10:22
  • @AlexTennant: I've updated my code. Some of them are what I'm trying to achieve. I need to populate the lookup fields(Opportunity Code) in any object(Opportunity here) with just the external ID in hand. Let me know if you need more inputs. Sep 17, 2014 at 10:33
  • Since you know the object is going to be a Code__C object, why are you using sObject? Without more context this doesn't make a great deal of sense since as far as I can see you know exactly what type the object is so the cast is unnecessary. Sep 17, 2014 at 10:53
  • I don't want this to be a specific Object say (code here). Here we are updating Opporunity's Opp Code Lookup. I'm trying to update an sobject(meaning it should support all objects). What I'm trying is, I should be able to update any object with any of its Lookup fields. with just External Id and the Object type as string. Sep 17, 2014 at 11:00
  • Ok, let's try a different approach. How much information do you know up front? You know the External Id and the Type of the object with the external Id. Do you know any information about the child object (Opportunity in the example above) such as it's type, or what field the parent object (Code__c above) reference should be stored in? What I'm trying to say is that based on the information and code you've provided you've not shown why you need to use sObject over Code__c and if you even have the information to be able to do so. Sep 17, 2014 at 11:32

2 Answers 2


Now I've got a better idea of what you're trying to achieve, here is an attempt based on the information you say you have to hand.

Note: This is untested, but it should work.

String objectToUpdate = 'Opportunity';
String externalId = 'EXTERNAL-ID';
Schema.DescribeFieldResult fieldToUpdateDescribe = // You mention in your question you already have this, I assume it's Opportunity.Opportunity_Code__r based on the example
SObject parentObject; // Code__c, we need to create this

    // This field only refers to one sObject type, find it, Code__c in the case in the example
    Schema.sObjectType parentType = fieldToUpdateDescribe.getReferenceTo()[0]; // We know this only has one value since isNamePointing is true

    // Find the External ID field
    String  externalIdFieldName;

    for(Schema.sObjectField field parentType.getDescribe().fields.getMap().values())
        DescribeFieldResult fieldDescribe = field.getDescribe();

            parentObject = parentType.newSObject();
            parentObject.put(fieldDescribe.getName(), externalId);
            break; // I'm assuming there is only one External Id field
    // This field refers to more than one sObject type, handle this here

// Update the child object, Opportunity in the case of your example
SObject childObject = Schema.getGlobalDescribe().get(objectToUpdate).newSObject();
childObject.putSObject(fieldToUpdate.getRelationshipName(), parentObject); 
upsert childObject;
  • Awesome! That was what I was looking for! I was not aware of "parentType.newSObject()". Thanks a ton! Sep 18, 2014 at 12:42

I agree with Alex's comment

Since you know the object is going to be a Code__C object, why are you using sObject? Without more context this doesn't make a great deal of sense since as far as I can see you know exactly what type the object is so the cast is unnecessary.

However, you can do something like this to create an object dynamically using an Id

Id objectId = 'External Id Here';

SObject newObject = objectId.getSObjectType().newSObject(objectId); //instantiates an object of given sobject type with provided id

and finally put it in your object.

op.put('Oppoortunity_Code__r',  obj); //you don't need to cast.
  • The External Id I've is not the Object id. Say in our example, Opp Code has an external Id field, that is all I've and I want to update the Lookup field in Opp(to Opp Code) using this. Sep 17, 2014 at 11:26
  • What about using Schema.getGlobalDescribe().get(<Your Object Type>).newSObject(); to create your object dynamically
    – Learner
    Sep 17, 2014 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .