Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)
edited by

Okay, so here's my question. I'm working with a save result from the Salesforce API, and I am learning LINQ. It may not be the best thing to use here, I don't know, but this is what I'm trying to do, just more eloquently.

BTW, here's the saveresult[] class shrunk down to definitions:

public partial class SaveResult {

    private Error[] errorsField;

    private string idField;

    private bool successField;

    [System.Xml.Serialization.XmlElementAttribute("errors")]

    public Error[] errors

    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]

    public string id

    public bool success 

}

And here's what I'm trying to do. Check for errors, return them if there are any in any of the returned save results, or return null if no errors.\

string errors = null;

        foreach (SaveResult s in saved)

        {

            if (s.success) continue;

            else

            {

                foreach (Error e in s.errors)

                {

                    errors += String.Format("Errors on object: {0}. Error Code is: {1}. Error Message: {2}",

                                            s.id,e.statusCode.ToString(),e.message);

                }

            }

        }

So far I have:

return saved

   .Select(i => i.errors

      .Select(j => new { j.statusCode, j.message })

      .Distinct()

      .ToList()

      ).ToString();

I'm pretty sure I will need an anonymous function in there to evaluate for errors before I go on.

Anyway, that's it. Thanks for the help (or the links pointing me to help!)

1 Answer

0 votes
by (32.1k points)
edited by

Using the query syntax below, you will be able to evaluate errors before going on further:

return string.Join("",

    from s in saved

    where ! s.Success

    from e in s.Errors

    select string.Format(

        "Errors on object: {0}. Error Code is: {1}. Error Message: {2}",

        s.id, e.statusCode);

This is translated to a SelectMany behind the scenes.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by Krishna (2.6k points)
...