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.
- The Object type I'm trying to update (Opportunity here).
- The external Id of the lookup object(External Id of the Opp Code).
- 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!
Oppoortunity_Code__r
) must be of a concrete type, so I don't know why you need a dynamic cast.Code__C
object, why are you usingsObject
? 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.