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
abu saleh khan 8abu saleh khan 8 

Hi, I have a use case.I have three objects Student,Account and Contact."Student is parent to Account and Account is parent to Contact.When ever i update Tenure field in contact,Students "Tenure" field should also be updated.I am having issues wth code.

Hi, I have a use case.I have three objects Student,Account and Contact."Student is parent to Account and Account is  parent to Contact.When ever i update Tenure field in contact,Students "Tenure" field should also be updated.I am having issues wth code.When I am tring to update a field in contact this isthe error, I am getting.<<< ERROR :    Review the following errors
StudentTenure: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Trigger.StudentTenure: line 14, column 1 :>>>

Program:

trigger StudentTenure on Contact (After Update) {
    List<Student__c> stulst = new List<Student__c>();
    List<Contact> cst = new List<Contact>();
    cst = [SELECT id,Name,Tenure__c,Account.Student__r.Tenure__c FROM Contact WHERE id IN :Trigger.old];
    System.debug('Data is cst: '+cst);
    for(Contact c : cst){
    
        Student__c s = new Student__c();
        if(c.Tenure__c == 'OLD'){
            s.Tenure__c = 'OLD';
        }
        stulst.add(s);
    }
    update stulst;
}

 
Best Answer chosen by abu saleh khan 8
Waqar Hussain SFWaqar Hussain SF
Hi Abu Saleh,

try below code.
trigger StudentTenure on Contact (After Update) {
    List<Student__c> stulst = new List<Student__c>();
    List<Contact> cst = new List<Contact>();
    cst = [SELECT id,Name,Tenure__c,Account.Student__r.Tenure__c, Account.Student__c FROM Contact WHERE id IN :Trigger.old];
    System.debug('Data is cst: '+cst);
    for(Contact c : cst){
    
        if(c.Tenure__c == 'OLD' && c.Account.Student__c != null){
			Student__c s = new Student__c();
            s.Tenure__c = 'OLD';
			s.Id = c.Account.Student__c;
			stulst.add(s);
        }
        
    }
	if(stulst.size() > 0)
    update stulst;
}

 

All Answers

kamala swarnalathakamala swarnalatha
Hi,

To call the Id,

if(c.Tenure__c == 'OLD'){
            s.Tenure__c = 'OLD';
        }

Here you have given the values not mention the Id to match which record to be store values.For that you have assign the Id for the Record So
write the code like this in the below code

for(Engineer_Checklist__c ssli:ssnewList)
{
SiteContracts__c newsc = new SiteContracts__c();
newsc.id=ssli.Site_Contract__c; //i hope this is the relation ship field between both the objects
newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
newsc.X3L_Water_Mist__c=ssli.X3L_Water_Mist_Qty__c;
newsc.X6L_Water_Mist__c=ssli.X6L_Water_Mist_Qty__c;
newsc.X9L_Water_Mist__c=ssli.X9L_Water_Mist_Qty__c;

scnewlist.add(newsc);
}
update scnewlist;


In the above code it to be mention in the Bold character.

Here you will assign the student id =the lookup field name.


Hope this Helps!

Thanks,
K.Kamala,
Sweet Potato Tec.

 
Waqar Hussain SFWaqar Hussain SF
Hi Abu Saleh,

try below code.
trigger StudentTenure on Contact (After Update) {
    List<Student__c> stulst = new List<Student__c>();
    List<Contact> cst = new List<Contact>();
    cst = [SELECT id,Name,Tenure__c,Account.Student__r.Tenure__c, Account.Student__c FROM Contact WHERE id IN :Trigger.old];
    System.debug('Data is cst: '+cst);
    for(Contact c : cst){
    
        if(c.Tenure__c == 'OLD' && c.Account.Student__c != null){
			Student__c s = new Student__c();
            s.Tenure__c = 'OLD';
			s.Id = c.Account.Student__c;
			stulst.add(s);
        }
        
    }
	if(stulst.size() > 0)
    update stulst;
}

 
This was selected as the best answer
abu saleh khan 8abu saleh khan 8
Thankyou Waqar Hussain SF and kamala swarnalatha..
Waqar Hussain SFWaqar Hussain SF
Please don't forget to mark Best Answer for the most helpful answer, so it is removed from the no answered questions.