• Mohit Mohan
  • NEWBIE
  • 25 Points
  • Member since 2007

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies

Greetings! I am new to the SalesForce world. I am starting up a small project that will use the Web Services API.  So far I've had good luck with accessing the service, pulling data, etc. 

 

Now I've dabbled with creating a new record.  I have it working but I was hoping that someone could look at my code sample and tell me if this is most efficient way.  I fear that I have over-complicated things.

 

My SF object is a simple table (is this even the right word in the salesforce world??) called test with just one field called Name.  Standard stuff.  So to create/insert I am doing the following in c#:

 

        Test__c[] newStuff = new Test__c[1];

        newStuff[0] = new Test__c();
        newStuff[0].Name = "My Test Name";

        String[] createdItemIds = new String[10];
        SaveResult[] sr = binding.create(newStuff);

        for (int i=0;i<sr.Length;i++) {

            if (sr[i].success) {

                Response.Write("<hr/>" + sr[i].id.ToString() + "<hr/>");
                createdItemIds[i] = sr[i].id;

            }

        }

 

 

I built this from some c# sample code that I found on here.  Like I said everything works but the sample code I found was for inserting multiple records at once.  I am curious if there is a cleaner, more direct way to insert a SINGLE record and get the new unique ID back from the webservice.

 

Thanks in advance for any tips or advice!

 

- Jasper

 

 

I have the following trigger / @future callout which works terrific in the UI.

==============================================================
trigger afterInactivateUser on User (after update) {
 Set<Id> inactivatedParterUsers = new Set<Id>();

 for(Integer i = 0; i < Trigger.new.size(); i++){
  User oldUser = Trigger.old.get(i);
  User updatedUser = Trigger.new.get(i);
  if(updatedUser.ContactId != null && updatedUser.IsActive == false && oldUser.IsActive == true){
   inactivatedParterUsers.add(updatedUser.id);
  }
 }
 if(!inactivatedParterUsers.isEmpty()){
  PartnerUtils.createInactivateUserPartnerRequest(inactivatedParterUsers);
 }
}
==============================================================
==============================================================
public class PartnerUtils {
 @future
 public static void createInactivateUserPartnerRequest(Set<Id> ids){
  List<User> users = new List<User>([select id, ContactId from User where id in :ids]);
  List<MyCustomObject__c> requestList = new List<MyCustomObject__c>();
  MyCustomObject__c request = null;
  for(User user : users){
   request = new MyCustomObject__c();
   request.Contact__c = user.ContactId;
   requestList.add(request);
  }
  insert requestList;
 }

}
==============================================================

However, the following test throws a MIXED_DML_OPERATION when the @future createInactivateUserPartnerRequest() method tries to insert a non-User object:
==============================================================
@IsTest
private class PartnerUtilsTest {
private static testmethod void testAfterInactivateUserTrigger(){
User user = [select id from user where IsActive = true and ContactId != null limit 1];
user.IsActive = false;
update user;
}
}
==============================================================

Any ideas on a workaround that won't fail?