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
Akshata ShahAkshata Shah 

Hii Please help me

Hii Friends,
I want code for upsert records
I have oject Timesheet__c custom object.
Which has following filed.
Name
Month__c(Master_detail with Month__c object)
Resource__c(Lookup with Resource__c object)

i want code for if the Timesheet for that Month is already exist then update that records if not then create new record.
this is my code
Timesheet__c ts=new Timesheet__c();
                    ts.Month__c=new_records3.Id;
                    ts.Resource__c=currentRecord.Resource__c;
                    List<Timesheet__c> tlist=new List<Timesheet__c>();
                    tlist=[SELECT ID,Name,Month__r.Name,Resource__r.Name 
                           FROM Timesheet__c
                           WHERE Month__r.Name=: new_records3.Name AND Resource__r.Name=:currentRecord.Resource__r.Name];
                    if(tlist.size()>0){
                        for(Timesheet__c t:tlist){
                            update ts t.ID;
                        }
                        
                    }
                    else
                        insert ts;

Above not updating existing records.It creating new records.
Please help me as soon as possible. Its very urgent
Thanks in advance
Best Answer chosen by Akshata Shah
Annu ChoudharyAnnu Choudhary
Hi Akshata,
Please use below peace of code and check the result. Hopefully by this change your issue is resolved.
and please give response.
If it work fine then please select as the best answer.
 
Timesheet__c ts=new Timesheet__c();
                    ts.Month__c=new_records3.Id;
                    ts.Resource__c=currentRecord.Resource__c;
                    List<Timesheet__c> tlist=new List<Timesheet__c>();
                    tlist=[SELECT ID,Name,Month__r.Name,Resource__r.Name 
                           FROM Timesheet__c
                           WHERE Month__c =: new_records3.id AND Resource__c =: currentRecord.Resource__c];
                    if(tlist.size()>0){
                        for(Timesheet__c t:tlist){
                            ts.id = t.id;
                            update ts ;
                        }
                    }
                    else
                        insert ts;
Thanks,
Annu Choudhary

 

All Answers

Annu ChoudharyAnnu Choudhary
Hi Akshata,
Can you please explain what is currentRecord here and how you get this. Can you show me that soql from which you get currentRecord .
Annu ChoudharyAnnu Choudhary
HI Akshata,
There are Month__c and Monthaly_salary__c is two different object right ???
Akshata ShahAkshata Shah
Hii Annu
No both are same
On Timesheet__c object  Month__c is name of Master -detail relationship with Monthly_Salary__c object.
Annu ChoudharyAnnu Choudhary
Hi Akshata,
Please use below peace of code and check the result. Hopefully by this change your issue is resolved.
and please give response.
If it work fine then please select as the best answer.
 
Timesheet__c ts=new Timesheet__c();
                    ts.Month__c=new_records3.Id;
                    ts.Resource__c=currentRecord.Resource__c;
                    List<Timesheet__c> tlist=new List<Timesheet__c>();
                    tlist=[SELECT ID,Name,Month__r.Name,Resource__r.Name 
                           FROM Timesheet__c
                           WHERE Month__c =: new_records3.id AND Resource__c =: currentRecord.Resource__c];
                    if(tlist.size()>0){
                        for(Timesheet__c t:tlist){
                            ts.id = t.id;
                            update ts ;
                        }
                    }
                    else
                        insert ts;
Thanks,
Annu Choudhary

 
This was selected as the best answer
Akshata ShahAkshata Shah
Thank you so much Annu
It works. :)
Regards,
Akshata