1

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(); 


                }

}
1

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(); 


                }

}
  • 1
    Thank you, you are always trying to help me. I will try it and let you know – Carlos Apr 2 '14 at 22:28
  • 1
    And for the issue above, I am not an expert, only done 1 or 2 integrations and can't recall the syntax exactly in C# but something like sforce.sObject[] nrProductsToUpdate = new sforce.sObject[] should create and initialise the list and then you need to add all your objects in this list and do a single update. I will try to find out the correct syntax in the meantime... – Boris Bachovski Apr 3 '14 at 1:49
  • 1
    Are you getting any errors or the values are not getting through? – Boris Bachovski Apr 3 '14 at 3:19
  • 1
    Try adding fieldname__cspecified = true; for each of the fields after you assign the values. E.g. nrProduct.Amazon_Rule_ID__cspecified = true; – Boris Bachovski Apr 3 '14 at 3:26
  • 1
    Man, you made it... that works. Thank you thank you so much. You have no idea how much appreciate your help! – Carlos Apr 3 '14 at 3:35
2

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 ";
}
  • keith, I will edit my question with what am trying to accomplish here. Thank you. – Carlos Apr 2 '14 at 21:31
  • @Carlos Sounds like you are making progress. If you no longer need an answer to your original question please make that clear. – Keith C Apr 2 '14 at 21:37
  • Keith, I still need help, I just edited the question so it is more clear where I need help – Carlos Apr 2 '14 at 21:41
  • @Carlos OK. But personally I don't understand what "modify this code so it updates only the records of the SQL" means. Can you be clearer about that? – Keith C Apr 2 '14 at 21:43
  • Ok, the NRCode that are in the SQL Table(vendors) exist in salesforce object(NRProducts__c), so if there any way I can update these NRCodes and their fields(Amazon_Listing_Level, Amazon_Rule_ID, and ESTShipPrice) using the api? please let me know if this is clear enough – Carlos Apr 2 '14 at 21:46

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.