+ Start a Discussion
DeptonDepton 

API - Web Services Vs Triggers and rules

Hi,

 

I am having the issue that when using Web Services (PHP) triggers and assignment rules are not executed.

 

2 examples:

 

Lead assignment:

 

When I created a Lead assignment rules are not working, all the fields are correctly populated but the assignment rules are not working.

 

The same with opportunity triggers, I have added a user exception:

 

If the user that modifies the record is “XXXXX” then the trigger shouldn´t work, but the trigger keeps getting fired??

 

Any ideas why is this happening?

 

Thank you!!

 

sales4cesales4ce

Asignment rules are not fired implicitly when leads are inserted via API. You need to invoke them explicitly.

 

Use the database.DMLOptions class to provide extra information during a transaction, for example, specifying the truncation behavior of fields or assignment rule information. DMLOptions is only available for Apex saved against API versions 15.0 and higher.

 

Here is how you would do:

 

If you want to invoke your default rule:

Database.DMLOptions dmo = new Database.DMLOptions();

dmo.assignmentRuleHeader.useDefaultRule= true;



Lead l = new Lead(company='ABC', lastname='Smith');

l.setOptions(dmo);



insert l;

If you want to specify your assignmentruleId:
Database.DMLOptions dmo = new Database.DMLOptions();

dmo.assignmentRuleHeader.assignmentRuleId= '01QD0000000EqAn';

Lead l = new Lead(company='ABC', lastname='Smith');

l.setOptions(dmo);



insert l;

The trigger gets fired no matter what the user does. but you need to encapsulate your logic.
if the user criteria matches, the logic in your trigger would get executed and if the criteria did not match, the trigger would not execute your logic.

 

Hope this helps.

 

Sales4ce



DeptonDepton

Thank you!!!

 

I think i understand,  just to be sure, from the PHP API there is nothing we can do, we must create a trigger for it right?

 

sales4cesales4ce

Trigger is created on the Lead object in Salesforce. So when you perform your DML operation the trigger would fire.

Was that helpful?

 

Sales4ce

SuperfellSuperfell

From the SOAP API you would set the AssignmentRuleHeader to enable the assignment rules to run.

DeptonDepton

Thanks!!

 

So I can do it through the API!! right?



I have found this, I think it will help me to set the Assignmentruleheader

 

<?php
class Lib_Test_Partner_AssigmentRuleHeaderTest extends Lib_Test_TestAbstractPartner
{
        public function getTestName()
        {
                return 'AssigmentRuleHeader';
        }
        
        protected function _run()
        {
                $header = new AssignmentRuleHeader('01Q300000005lDg', false);
                $this->_mySforceConnection->setAssignmentRuleHeader($header);
        
                $createFields = array (
                        'FirstName' => 'John',
                        'LastName' => 'Doe',
                        'Email' => 'johndoe@salesforce.com',
                        'Company' => 'Some Company',
                        'LeadSource' => 'PHPUnit2',
                        'City' => 'Tokyo',
                        'Country' => 'Japan'
                );
                $sObject1 = new SObject();
                $sObject1->fields = $createFields;
                $sObject1->type = 'Lead';
        
                $createResponse = $this->_mySforceConnection->create(array($sObject1));
                print_r($createResponse);
        }
}

 

 

Thank you!!