You need to sign in to do that
Don't have an account?
Akis Athanasiadis
Update field from the field of the record it is related with
Hello,
I have the contract standard object which has several record types.
in case i have the record type vendor purchases the user will need to insert another contract code. So contract standard obejct has a look up field to itself.
I would like in case i have the record type vendor-purchases, when i insert the contract code it is related with, to automatically insert the Account id.
This is what i've dove done but i get error " A non foreign key field cannot be referenced in a path expression: Maintenance_Contract__c at line 8 column 27"
An example of the layout
I have the contract standard object which has several record types.
in case i have the record type vendor purchases the user will need to insert another contract code. So contract standard obejct has a look up field to itself.
I would like in case i have the record type vendor-purchases, when i insert the contract code it is related with, to automatically insert the Account id.
This is what i've dove done but i get error " A non foreign key field cannot be referenced in a path expression: Maintenance_Contract__c at line 8 column 27"
trigger Vendor on Contract (before insert,before update) { for (Contract c : Trigger.new) { if (c.RecordTypeId=='0120Q0000004gQO') { c.AccountId=c.Maintenance_Contract__c.AccountID; } } }
An example of the layout
Please try below code.You have use __r relationship when fetching 2 or 3 level record values and you should not hard code your recordtypeId for good practice.
For fetching recordtype id:
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
trigger Vendor on Contract (before insert,before update) {
for (Contract c : Trigger.new) {
if (c.RecordTypeId==devRecordTypeId) {
c.AccountId=c.Maintenance_Contract__r.AccountID;
}
}
}
Thanks,
SEKAR RAJ
If the syntax is the following:
i get the following error
"Vendor: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Vendor: line 4, column 1"
Please add record type Name and which object in the below syntax. Highlighted in the bold letters.
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
Thanks,
SEKAR RAJ
The create page is the following:
So when i insert the maintenance contract code, i need teh trigger to be able to do the assignment of the account on the account name.
The code according to what you suggested is the following:
As you saw in teh first screenshot, i get error
Please make the above suggestion as best answer if it's fixed your issue.
Thanks,
SEKAR RAJ