how to assign value to lookup fields in salesforce - salesforce

i have two objects KNDY4__Sales_Order__c and KNDY4__Bill_to__c.these two are linked through lookup relationship.i am trying to insert one order record as follows
`KNDY4__Sales_Order__c order=New KNDY4__Sales_Order__c();
order.KNDY4__Ship_to__c ='a14q0000001LnIeAAK';
order.KNDY4__Bill_to__r.Predicted_External_ID__c ='CN-0222741-Sold To';
order.KNDY4__Company__c ='a0l1N00000BQQKF';
Insert order;`
i am geting error as {"Object reference not set to an instance of an object."}.
how to assign values to lookup field.can anyone help me

When linking a record based on an external id, the relationship field needs to be set to an sObject with the external id, and not just the value of the id itself.
On line 3, since KNDY4__Bill_to__c is a lookup field to a custom object possibly named KNDY4__Customer__c, you should set the value of the lookup to an instance of that object, e.g.
order.KNDY4__Bill_to__r = new KNDY4__Customer__c(KNDY4__Predicted_External_ID__c ='CN-0222741-Sold To');

Related

Insert to Parent and child custom object using external Id

I am trying to insert a record into a child custom object using the external id of the parent custom object. I am almost clear on how to setup the external id field in the parent object, but very confused with how to setup a foreign key in my child object. I am attaching the pics here which has details of the master and child custom objects. What I am i supposed to add to the child custom object to link its records to the parent object.
Also here is the code I got from another post in stackoverflow, but that didn't work in my case
enter image description heresumchans__City_Master__c city = new sumchans__City_Master__c(Name='[![enter image description here][1]][1]Vancouver',sumchans__PROVINCE__c = 'BC', sumchans__City_Name_Ext_Id__c = 'Vancouver');
insert city;
sumchans__City_Stat__c cityStat = new sumchans__City_Stat__c(Name = 'Vancouver', sumchans__ON_BILLINGS__c = 50, sumchans__City_Master__c=new sumchans__City_Master__c(sumchans__City_Name_Ext_Id__c='Vancouver'));
insert cityStat;
Ext. ids are meant to be used in integrations, Data Loader... They can be used in Apex too but the syntax looks bit funny. Use them in Apex if you must save on queries or want to go "dear salesforce, I don't remember if I saved this object already, I know unique identifier is 123, go figure it out yourself whether it needs insert or update".
If you insert parent and child in same transaction - just user the Id generated into parent record. If they're separate, a while ago you did this:
insert new sumchans__City_Master__c city = new sumchans__City_Master__c(Name=Vancouver',
sumchans__PROVINCE__c = 'BC',
sumchans__City_Name_Ext_Id__c = 'Vancouver'
);
Then later in another transaction you can do this without querying:
sumchans__City_Stat__c cityStat = new sumchans__City_Stat__c(Name = 'Vancouver',
sumchans__ON_BILLINGS__c = 50,
sumchans__City_Master__r = new sumchans__City_Master__c(
sumchans__City_Name_Ext_Id__c='Vancouver'
)
);
insert cityStat;
Note that I used __r instead of __c. __c would be your real foreign key, Id field, 15 or 18 characters. __r would be a reference to another object (with ext. id or query result for example), similar to writing SELECT FirstName, Email, Account.Name FROM Contact.
In Apex it has to be as a object referece (so either query or cheat with new keyword, just to create it in memory. You won't actually save that "parent"). In things like data loader you can map values bit like myCsvExtIdColumn -> sumchans__City_Master__r.sumchans__City_Name_Ext_Id__c
Create a lookup field instead. Because External ID field used to reference an ID from another external system.
If you want to relate the foreign key with and relational DBMS, you need to create a lookup relationship type field in the child object. There you need to assign the Parent Object in the Related To field.

How to write multiple relation query in SOQL

I am performing SOQL query to get the contact detail of a customer my parent and child table relationship is as follow.
child_table
parent_table
grandparent_table
I am trying to get contact_name from Contact(Grand Parent) from Event(Child) and I trying something like this.
select Name,
Venue__r.Contact__r.Name
from Event__c;
Can anyone suggest me what I am doing wrong and what is a correct way to get it?
Your query syntax is fine. Make sure that your lookup names are correct. In your linked parent diagram, the object is named 'Vanue'.
Also, it's very important that you use the local field name for each object relationship reference (__r).
Given your example SOQL
SELECT Name, Venue__r.Contact__r.Name FROM Event__c;
we can only assume the following details:
CUSTOM OBJECT Event__c has a STANDARD FIELD 'Name' and a CUSTOM FIELD 'Venue__c'
CUSTOM FIELD Event__c.Venue__c is a lookup to a PARENT or MASTER object which may or may not be named 'Venue__c', so we'll refer to it as [V].
CUSTOM OBJECT [V] has a CUSTOM FIELD 'Contact__c'
CUSTOM FIELD [V].Contact__c is a lookup to a PARENT or MASTER object which may or may not be named 'Contact__c ', so we'll refer to it as [C].
CUSTOM OBJECT [C] has a STANDARD FIELD 'Name'

Pentaho salesforce upsert using externalID

I am trying to insert data in salesforce using upsert, for one field i am using the ExternalId field , i have tried many combinations but it fails...I get the error : the syntax should be object:externalId/lookupField
Any idea what is the exact syntax? Keep in mind i am inserting in table Account and the externalId field refers to Account also
object:externalId/lookupField is not very clear is it. There's a comment hidden away in the Pentaho code:
// We use an external key
// the structure should be like this :
// object:externalId/lookupField
// where
// object is the type of the object
// externalId is the name of the field in the object to resolve the value
// lookupField is the name of the field in the current object to update (is the "__r" version)
Lets say you're populating a Salesforce Object Foo__c, which has a Lookup field to Contact called Contact__c. The 'relationship name' for that lookup field would then be Contact__r.
On Contact lets say you have added an External ID called Legacy_Id__c and thats what you want to use when populating Foo__c.
What Pentaho would want in the Module Field column would then be:
Contact:Legacy_Id__c\Contact__r
The bit to the left of the slash is telling Pentaho which object/external id to map to. To the right of the slash, its telling Pentaho which lookup/relationship on Foo__c to fill in.

XMLport Table Attributes

XMLport objects in NAV appear to support table attributes (i.e. Node Type = Attribute, Source Type = Table). However, when I try to save an XMLport containing a table attribute node in NAV 2013, I get this error message:
The SourceType Table for Tag <MyTableAttribute> can only be assigned to TagType Element.
This (http://msdn.microsoft.com/en-us/library/hh165612(v=nav.70).aspx) page on MSDN seems to confirm that table attributes can be used. What purpose would a table attribute serve? What would be an example of a typical application of table attributes?
Table (record) cannot be source for attribute. Because record is a set of fields, i.e. it has multiple values, when attribute must have concrete single value. A field can be used as source for attribute of element. And record must be source for the element. That is what error message says.

Elementary Apex Object IDs

Quick Question. In the below code, you can see that the for loop (which takes all of the records in newTimecards and puts them as a variable called timecard) and adds the Resource_c to the resourceIds set. I'm confused about how this object is considered an ID data type. When an object is made in Salesforce does it automatically have an ID made, so that it knows Resource_c ID can be added to a set? Note that within the Resource_c Object there is also a field called Resource_ID_c. Resource_c within Timecard_c is a Master-Detail data type. Resource_c is the parent of Timecard_c.
Now that I think about it, resourceIds.add(timecard.Resource_c), does that reference the relationship between the two objects and then searches through Resource_c and adds the ID field Resource_ID_c automactically since it's a unique field?
Thanks for your help.
public class TimecardManager {
public class TimecardException extends Exception {}
public static void handleTimecardChange(List<Timecard__c> oldTimecards,
List<Timecard__c> newTimecards) {
Set<ID> resourceIds = new Set<ID>();
for (Timecard__c timecard : newTimecards) {
resourceIds.add(timecard.Resource__c);
}
Every object instance (and that means EVERY, including factory ones) has a unique organization level ID, whose field name is always Id, is covered by Apex type ID and is a case-sensitive string of 15 characters that also has an 18 character case-insensitive representation. The first three characters are object prefix code (e.g. 500 for a Case) so all instances of the same object share the same prefix. You see these values all across SF (for example in https://na1.salesforce.com/02s7000000BW59L the 02s7000000BW59L in the URL is the ID). When an instance of the object is created using INSERT DML operation, the salesforce automatically assigns unique value based on the prefix and the next available transactional sub ID, it all happens transparently to you.
This is not to be confused with object Name field which is a field you define when you create an object and which can be auto-incremented and so on (e.g. MYOBJ-{00000}) and which can have more meaning to a user than a cryptic ID
When you create a lookup or master-detail relationship it is ID that is being used to link the two instances, not the Name. In the above example Resource__c seems to be that lookup field and it contains Id value of row's master.
What the code does is it enumerates all resources used in timelines and builds a set of their IDs, the purpose of which is most probably to be used via WHERE Id IN :resourceIds clause to load resource details from master table.
mmix's answer is a great overview to what an ID is and where it comes from. To answer what I think is your specific question:
Any time there is a reference from one object to another (like here, between Timecard_c and Resource_c), the field representing the reference will be an ID. So, the for loop that calls resourceIds.add(timecard.Resource__c) is just building up your set of ID's (those 15-character strings). The timecard.Resource__c doesn't look through the Resource__c table to find the ID, timecard.Resource__c is the ID.

Resources