0

I am practicing SOSL queries. I tried to find the matching records of given Email ID across all objects and update it to new Email ID using SOSL query.

My Code:

public class Soslxmpl{
    public List<List<Sobject>> updateEmail(){
        String emailid = 'richie@gmail.com';
        List<List<SObject>> Sobj = [FIND 'richie@gmail.com' 
                                    IN email Fields
                                    Returning 
                                    Case(CaseNumber,SuppliedEmail),
                                    Candidate__C(Name,Email_Id__c),
                                    Contact(Name),
                                    Lead(Name)];
        Case[] cases = (Case[])Sobj[0];
        Candidate__C[] cand = (Candidate__C[])Sobj[1];
        Contact[] cont = (Contact[])Sobj[2];
        Lead[] leads = (Lead[])Sobj[3];
        for(Case Casse : Cases)        
        {
            Casse.SuppliedEmail= emailid;
        }
        for(Candidate__C Candi : cand)
        {
            Candi.Email_Id__C = emailid;
        }
        For(Contact con : Cont)
        {
            con.Email = emailid;
        }
        return Sobj;
        update Sobj;
    } }

I don't know how to update generic objects.Please help me to update email with new mail id.

0

You have two options for the DML. Single DML calls to each collection of SObject type, Single DML call to a grouped collection of SObject types.

For single DML calls to each collection of SObject type, as you have shown the SOSL return order is:

  1. Case
  2. Candidate__C
  3. Contact
  4. Lead

This allows you to cast your results to SObjects which have been stored in the List<List<sObject>> using indexing ([]).

Now that you have collections of known SObject type, you need to perform DML operations specific to that collection.

    ...
    
    Case[] cases = (Case[])Sobj[0];
    Candidate__C[] cand = (Candidate__C[])Sobj[1];
    Contact[] cont = (Contact[])Sobj[2];
    Lead[] leads = (Lead[])Sobj[3];
    for(Case Casse : Cases)        
    {
        Casse.SuppliedEmail= emailid;
    }
    for(Candidate__C Candi : cand)
    {
        Candi.Email_Id__C = emailid;
    }
    For(Contact con : Cont)
    {
        con.Email = emailid;
    }

    update cases;
    update cand;
    update cont;
    update leads;

    return Sobj;
    
    ...

In your original code, the return statement is before the attempted DML which would never have been reached. In saying this, you should also have been faced with a compile time Single DML calls to each collection of SObject type error.

For a single DML call to a grouped collection of SObject types you can use the addAll() method from the List class.

    ...

    Case[] cases = (Case[])Sobj[0];
    Candidate__C[] cand = (Candidate__C[])Sobj[1];
    Contact[] cont = (Contact[])Sobj[2];
    Lead[] leads = (Lead[])Sobj[3];
    for(Case Casse : Cases)        
    {
        Casse.SuppliedEmail= emailid;
    }
    for(Candidate__C Candi : cand)
    {
        Candi.Email_Id__C = emailid;
    }
    For(Contact con : Cont)
    {
        con.Email = emailid;
    }

    List<SObject> groupedSobjects = new List<SObject>();
    groupedSobjects.addAll(cases);
    groupedSobjects.addAll(cand);
    groupedSobjects.addAll(cont);
    groupedSobjects.addAll(leads);
    update groupedSobjects;

    return Sobj;

    ...
| improve this answer | |
0

You can update them all in a single transaction, just add them all in a list of SObjects.

List<SObject> updateSobjects = new List<SObject>();
updateSobjects.addAll(cases);
updateSobjects.addAll(cand);
updateSobjects.addAll(cont);
updateSobjects.addAll(leads);
update updateSobjects;

I just noticed that you are searching and updating the same email address in those records, it will not update anything except the LastModifiedDate on the records, just take care of that.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.