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
uuuu 

Update and insert new records

Hello Guys,

I want to check whether the records is already exist if yes then update it with new data if not insert new record into custom field.


currentRecord = [SELECT ID,Base_Yearly_Package__c FROM  Offers_Appraisals__c WHERE Id = :'a12M000000MMF6k'];

    List<Monthly_Salary__c> SalList = new List<Monthly_Salary__c>();
    SalList=[SELECT ID,Name,From_Date__c FROM Monthly_Salary__c WHERE Resource__c=:currentRecord.Resource__c];
    
                Monthly_Salary__c new_records2=new Monthly_Salary__c();
                                new_records2.Base_Yearly_Package__c=currentRecord.Base_Yearly_Package__c;
                                new_records2.Resource__c=currentRecord.Resource__c;

    for(Monthly_Salary__c s:SalList){
                                        if(s.Name!=new_records3.Name) {
                                            insert new_records3;
                                            break;
                                        } else {update new_records3;}
                           }


Above code working.Not even inserting nor updating the records.
Please help me.
where i am wrong?
Thank you
AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

Does the 'SalList' return any records? I see you are trying to retrieve Resource__c (currentRecord.Resource__c) via SOQL without querying it from Offers_Appraisals__c object

My suggestion is to add sytem.debug statements within your code and print the lists to see if they are returning any values. Also, before using the list, please do a size check

Using the upsert operation, you can either insert or update an existing record in one call

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm

Anudeep
uuuu
Hii Anudeep,
Monthly Salary has lookup with Resource.
and Offer & Apparisal has  lookup with Resource.
i want to access all Monthly Salary Records for a particular Resouce.
To check whether the Monthly Salary Records for that Resource is exists or not.
(I want to check it with the Monthly_Salary__c.Name)
If name exists then i want ot update it with new value.
If not then insert new record for that Resource.

Thank you.
RahulRahul
Is Offer & Apparisal is a Object?

To check whether the Monthly Salary Records for that Resource is exists or not.
-You want to check it for all the  Existing Records?

 
uuuu
Hii Rahul ,
Yes exactly Offer & Appraisal is custom object.
i want access all existing Monthly Salary Records for that Resource 
And want to check whether it exists or not.
Thank you
 
RahulRahul
how many existing Monthly Salary Records in total? because if there are so many records you need to process them in batches.

Do let me know so that I can write code  for you