Find your content:

Search form

You are here

Update Salesforce records from .Net(c#) based on SQL query

 
Share

I need to update some Salesforce records from .Net(c#) based on the SQL query. Below is my table (vendors in sql). I need to update Salesforce Object(NRProducts__c) with the data in the SQL.

  The SQL table vendors has the following columns:

      Id                 Amazon_Listing_Level     Amazon_Rule_ID     ESTShipPrice 
  a0C4000000DKIlj                    15                   102           2.25
  a0C4000000DKkhy                    1                    222           5.36
  a0C4000000DKIdr                    10                   562           10.25
  a0C4000000Djjut                    20                   521           8.00

I was having some issues passing the last 2 values which are not String in Salesforce. Here is the code, I hope you find it useful!

    protected void Button1_Click(object sender, EventArgs e)
    {

        //Updating Salesforce
        sforce.SforceService service = new sforce.SforceService();
        sforce.LoginResult lr = service.login("username", "passw+token");
        service.Url = lr.serverUrl;
        service.SessionHeaderValue = new sforce.SessionHeader();
        service.SessionHeaderValue.sessionId = lr.sessionId;



        //SQL Query
        String queryString = "Select id, Amazon_Listing_Level__c, Amazon_Rule__ID__c, ESTShipPrice__c from vendors2";
        SqlDataReader dr = classdb.LoadReader(queryString) as SqlDataReader;

        sforce.NRProducts__c nrProduct = new sforce.NRProducts__c();
                while (dr.Read())
                {

                nrProduct.Id = dr[0].ToString();
                nrProduct.Amazon_Listing_Level__c = dr[1].ToString();

     //Here is the problem I could not pass these 2 values to salesforce
     //nrProduct.Amazon_Rule_ID__c is type Number(4,0)in SF
     //nrProduct.ESTShipPrice__c is type Currency(16, 2) in SF

                nrProduct.Amazon_Rule_ID__c = Convert.ToInt32(dr[2]);
                nrProduct.Amazon_Rule_ID__cSpecified = true;
                nrProduct.ESTShipPrice__c = Convert.ToDouble(dr[3]);
                nrProduct.ESTShipPrice__cSpecified = true;



                }
                sforce.SaveResult[] update_result = service.update(new sforce.sObject[] { nrProduct });

                dr.Close(); 


                }

}

Attribution to: Carlos

Possible Suggestion/Solution #1

PS The answer below related to the original version of the question. Though the code changes should in my opinion be made in any case (particularly as the error reporting may reveal the underyling problem), they do not address the specifics of the later versions of the question.


You are not clear on what problem you have: a compilation problem or a runtime-problem. You should supplement your question with that information.

Working without that context I have two suggestions:

  1. The update call should probably be done outside the loop after all the data has been changed
  2. You should check the update results

I'm afraid I can't comment about the specifics of C# syntax (except to say that it would probably be better to use foreach) and what I have below may be syntactically wrong but hopefully it illustrates the above two points:

sforce.QueryResult query = service.query("SELECT NRCode__c FROM NRProducts__c ");
if (query.records != null && query.records.Length > 0) {
    for (int i = 0; i < query.records.Length; i++) {
        sforce.NRProducts__c updateAccount = query.records[i] as sforce.NRProducts__c;
        updateAccount.Amazon_Listing_Level__c = "  ";
        updateAccount.Amazon_Rule_ID__c = " ";
        updateAccount.ESTShipPrice__c = " ";
    }
    Label2.Text = "";
    sforce.SaveResult[] results = service.update(query.records);
    for (int i = 0; i < results.length; i++) {
        sforce.SaveResult r = results[i];
        if (!r.success) {
            for (int j = 0; j < r.errors.length; j++) {
                Label2.Text += r.errors[j].message + " ";
            }
        }
    }
    if (Label2.Text == "") {
        Label2.Text = "records updated successfully";
    }
} else {
    Label2.Text = "no records found ";
}

Attribution to: Keith C

Possible Suggestion/Solution #2

Since you have already got the salesforce ID's in your SQL database, I think you just need to create instances for each record you need to update and fire off an update:

List <sforce.NRProducts__c> nrProductsToUpdate = new List <sforce.NRProducts__c> ();

while (dr.Read())
{
    sforce.NRProducts__c nrProduct = new sforce.NRProducts__c ();
    nrProduct.Id = dr.GetString(0);
    nrProduct.Amazon_Listing_Level__c = dr.GetString(1);
    nrProduct.Amazon_Rule_ID__c = dr.GetString(2);
    nrProduct.ESTShipPrice__c = dr.GetString(3);
    nrProductsToUpdate.Add(nrProduct);
}

sforce.SaveResult[] update_result = service.update(nrProductsToUpdate);

The above code might not work, I just typed it to give you an idea, haven't actually tested it. Let me know how you go.

I have updated my code but I am having some issues passing the last 2 values which are not String in Salesforce. any ideas?

   protected void Button1_Click(object sender, EventArgs e)
    {

        //Updating Salesforce
        sforce.SforceService service = new sforce.SforceService();
        sforce.LoginResult lr = service.login("username", "passw+token");
        service.Url = lr.serverUrl;
        service.SessionHeaderValue = new sforce.SessionHeader();
        service.SessionHeaderValue.sessionId = lr.sessionId;

        //create list
       // List<sforce.sObject> nrProductsToUpdate = new List<sforce.sObject>();
        //SQL Query
        String queryString = "Select id, Amazon_Listing_Level__c, Amazon_Rule__ID__c, ESTShipPrice__c from vendors2";
        SqlDataReader dr = classdb.LoadReader(queryString) as SqlDataReader;

        sforce.NRProducts__c nrProduct = new sforce.NRProducts__c();
                while (dr.Read())
                {

                nrProduct.Id = dr[0].ToString();
                nrProduct.Amazon_Listing_Level__c = dr[1].ToString();

     //Here is the problem I can not pass these 2 values to salesforce
     //nrProduct.Amazon_Rule_ID__c is type Number(4,0)in SF
     //nrProduct.ESTShipPrice__c is type Currency(16, 2) in SF

                nrProduct.Amazon_Rule_ID__c = Convert.ToInt32(dr[2]);
                nrProduct.ESTShipPrice__c = Convert.ToDouble(dr[3]);



                }
                sforce.SaveResult[] update_result = service.update(new sforce.sObject[] { nrProduct });

                dr.Close(); 


                }

}

Attribution to: Boris Bachovski
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31682

My Block Status

My Block Content