How to use Assignment Rules with pyax ?

Asked by Ray C Horn

I am trying to use Assignment Rules for Leads with pyax and I am using the following code:

     sfdc.apex.cxn.svc.context.use_default_assignment_rule = True
     sfdc.apex.cxn.svc.context.assignment_rule_id = '01Q3000000000vFEAQ'

However pyax is complaining whenever a new Lead is being created.

Can you give me any code samples that show how to use Assignment Rules with pyax ?

I was given the following Java code to help guide my development effort: (It seems SalesForce loves Java...)

 package com.sforce;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.rmi.RemoteException;

 import com.sforce.soap.enterprise.LoginResult;
 import com.sforce.soap.enterprise.QueryResult;
 import com.sforce.soap.enterprise.SaveResult;
 import com.sforce.soap.enterprise.SforceServiceLocator;
 import com.sforce.soap.enterprise.SoapBindingStub;
 import com.sforce.soap.enterprise._AssignmentRuleHeader;
 import com.sforce.soap.enterprise._SessionHeader;
 import com.sforce.soap.enterprise.fault.LoginFault;
 import com.sforce.soap.enterprise.fault.UnexpectedErrorFault;
 import com.sforce.soap.enterprise.sobject.Lead;
 import com.sforce.soap.enterprise.sobject.SObject;

 public class LeadAssignment {

  static LeadAssignment _leadAssignment;

  public static void main(String[] args) {
  _leadAssignment = new LeadAssignment();
  try {
  _leadAssignment.CreateLead();
  } catch (Exception e) {
  e.printStackTrace();
  }
  }

  public void CreateLead() throws UnexpectedErrorFault, LoginFault,
   RemoteException {
  //Create the proxy binding and login
  SoapBindingStub binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
  LoginResult lr = binding.login("<email address hidden>", "secret");

  //Reset the binding to use the endpoint returned from login
   binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_POINT,
    loginResult.getServerUrl());

  //Create the session id header, and add it to the proxy binding
  _SessionHeader sh = new _SessionHeader();
  sh.setSessionId(lr.getSessionId());
  binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),
   "SessionHeader", sh);

  //Create a new case and assign various properties
  Lead lead = new Lead();

  lead.setFirstName("Joe");
  lead.setLastName("Smith");
  lead.setCompany("ABC Corporation");
  lead.setLeadSource("API");
  //The lead assignment rule will assign any new leads that
  //have "API" as the LeadSource to a particular user

  //Create the assignment rule header and add it to the proxy binding
  _AssignmentRuleHeader arh = new _AssignmentRuleHeader();

  //In this sample we will look for a particular rule and if found
  //use the id for the lead assignment. If it is not found we will
  //instruct the call to use the current default rule. You cannot use
  //both of these values together.
  QueryResult qr = binding.query("Select Id From AssignmentRule where Name =
 'Mass Mail Campaign' and RuleType = 'leadAssignment'");
  if (qr.getSize() == 0) {
  arh.setUseDefaultRule(new Boolean(true));
  } else {
  arh.setAssignmentRuleId(qr.getRecords(0).getId());
  }

  binding.setHeader(new
  SforceServiceLocator().getServiceName().getNamespaceURI(), "AssignmentRuleHeader",
    arh);

  // Every operation that results in a new or updated case, will
  // use the specified rule until the header is removed from the
  // proxy binding.
  SaveResult[] sr = binding.create(new SObject[] {lead});
  for (int i=0;i<sr.length;i++) {
  if (sr[i].isSuccess())
  System.out.println("Successfully creaeted lead with id of: " +
    sr[i].getId().getValue() + ".");
  else
  System.out.println("Error creating lead: " +
    sr[i].getErrors(0).getMessage());
  }

  // This call effectively removes the header, the next lead will
  // be assigned to the default lead owner. Remember to add the
  // session header back in.
  binding.clearHeaders();
  binding.setHeader(new
  SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);

  }
 }

Question information

Language:
English Edit question
Status:
Solved
For:
pyax Edit question
Assignee:
No assignee Edit question
Solved by:
Ray C Horn
Solved:
Last query:
Last reply:
Revision history for this message
Ray C Horn (mrpython94596) said :
#1

And now to answer my own question:

sfdc.context.use_default_assignment_rule = True

This was not intuitive because it was based on a guess but it seemed to work so now I am happy once again with pyax.