function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akis AthanasiadisAkis 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"
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
User-added image
SEKAR RAJ.SEKAR RAJ.
Hi Akis,
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
Akis AthanasiadisAkis Athanasiadis
if you suggest me to do this in this order, of course this will not work. 
If the syntax is the following:
trigger Vendor on Contract (before insert,before update) {
    
    for (Contract c : Trigger.new) {
        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
        if (c.RecordTypeId==devRecordTypeId) 
        {
            c.AccountId=c.Maintenance_Contract__r.AccountID;
        }
    }
}

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"
 
SEKAR RAJ.SEKAR RAJ.
Hi Akis,
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
Akis AthanasiadisAkis Athanasiadis
Hi,
The create page is the following:
User-added image
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:
trigger Vendor on Contract (before insert,before update) {
    
    for (Contract c : Trigger.new) {
        Id devRecordTypeId = Schema.SObjectType.Contract.getRecordTypeInfosByName().get('Vendor-Purchases').getRecordTypeId();
        if (c.RecordTypeId==devRecordTypeId) 
        {
            c.AccountId=c.Maintenance_Contract__r.AccountID;
        }
    }
}

As you saw in teh first screenshot, i get error
 
Akis AthanasiadisAkis Athanasiadis
A small correction on the above code:
 
trigger Vendor on Contract (before insert,before update) {
    
    for (Contract c : Trigger.new) {
        Id devRecordTypeId = Schema.SObjectType.Contract.getRecordTypeInfosByName().get('Vendor-Purchases').getRecordTypeId();
        if (c.RecordTypeId=='0120Q0000004gQO') 
        {
            c.AccountId=c.Maintenance_Contract__r.AccountID;
        }
    }
}

 
SEKAR RAJ.SEKAR RAJ.
Hi Akis,
Please make the above suggestion as best answer if it's fixed your issue.

Thanks,
SEKAR RAJ
Akis AthanasiadisAkis Athanasiadis
i believe you have read my last posts. if not, please check'em again