function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike @ PartnersMike @ Partners 

Add Product to Opportunity

I am trying to adda product to an opportunit that was just created. I came up with this code:

Code:
$fieldsToCreate = array('Name'=>$OpName,'Type'=> $OpType,'LeadSource'=>$OpSource,'CloseDate'=>$PWDate,'StageName'=>$OpStage,'CampaignId'=>$CampaignId,'Amount'=>$BillAmount,'AccountId'=>$AccountId,'Public_Workshop__c'=>$POWid,'RecordTypeId'=>$RecordTypedID,'AccountId'=>$AccountId);
$sObject1 = new SObject();
$sObject1->fields = $fieldsToCreate;
$sObject1->type = 'Opportunity';
$updateRespons = $mySforceConnection->create(array($sObject1));
var_dump($updateRespons);
$updateResponsid = $updateRespons->id;

$fieldsToCreate2 = array('OpportunityId'=> $updateResponsid,'UnitPrice'=> $salesprice,'ProductId'=>'00j30000000mflNAAQ','PricebookEntryId'=>'01u30000000BCVIAA4','Quantity'=>$numAtten,'ServiceDate'=>$PWDate,'Location__c'=>$PWLocation,'Description'=>$lineDisc);
$sObject2 = new SObject();
$sObject2->fields = $fieldsToCreate2;
$sObject2->type = 'OpportunityLineItem';
$updateRespons2 = $mySforceConnection->create(array($sObject2));
var_dump($updateRespons2);
return $updateResponsid;

}


This code creats the opportunit then it is supose to add the correct product to the opportunit that was just created. The opportunit  gets create but i get this error when it trys to add the product:

object(stdClass)#11 (3) { ["errors"]=> object(stdClass)#12 (3) { ["fields"]=> NULL ["message"]=> string(16) "assertion failed" ["statusCode"]=> string(17) "UNKNOWN_EXCEPTION" } ["id"]=> NULL ["success"]=> bool(false) }

Any ideas on where im going wrong?
thanks for the help.
Mike
 

Tran ManTran Man
What does the following print?
 var_dump($updateRespons);


Mike @ PartnersMike @ Partners
This is the full var dump i get for both calls;

object(stdClass)#8 (3) { ["errors"]=> NULL ["id"]=> string(18) "00660000007wSBBAA2" ["success"]=> bool(true) }
object(stdClass)#11 (3) { ["errors"]=> object(stdClass)#12 (3) { ["fields"]=> NULL ["message"]=> string(16) "assertion failed" ["statusCode"]=> string(17) "UNKNOWN_EXCEPTION" } ["id"]=> NULL ["success"]=> bool(false) }


The success var dump is the creation of the Opportunity. The second object , the one that is false is the  where im trying to add  the  product.
Thanks for your help,
mike

SuperfellSuperfell
That's a fairly useless error message, but i think you need to set the priceBook on the opportunity firsst.
Mike @ PartnersMike @ Partners
I know, that error tells me nothing.

I changed the code to this and i still get the error,

Code:
global $mySforceConnection;
$fieldsToCreate = array('Name'=>$OpName,'Type'=> $OpType,'LeadSource'=>$OpSource,'CloseDate'=>$PWDate,'StageName'=>
$OpStage,'CampaignId'=>$CampaignId,'Amount'=>$BillAmount,'AccountId'=>
$AccountId,'Public_Workshop__c'=>$POWid,'RecordTypeId'=>
$RecordTypedID,'AccountId'=>$AccountId);
$sObject1 = new SObject();
$sObject1->fields = $fieldsToCreate;
$sObject1->type = 'Opportunity';
$updateRespons = $mySforceConnection->create(array($sObject1));
var_dump($updateRespons);
$updateResponsid = $updateRespons->id;
$fieldsToCreate2 = array('OpportunityId'=> $updateResponsid,'PricebookEntryId'=>$PricebookEntryId,'UnitPrice'=>
$salesprice,'ProductId'=>$ProductId,'Quantity'=>
$numAtten,'ServiceDate'=>$PWDate,'Location__c'=>
$PWLocation,'Description'=>$lineDisc);
$sObject2 = new SObject();
$sObject2->fields = $fieldsToCreate2;
$sObject2->type = 'OpportunityLineItem';
$updateRespons2 = $mySforceConnection->create(array($sObject2));
var_dump($updateRespons2);

But i cant see why there would be a diifentes. All i did was change where the Pricing book id is with-in the array. When you say  i need to 'set the priceBook on the opportunity first' Do you mean i need to run another call then run this call? What would that call look like?
Thank you for youe help,
Mike
 

Mike @ PartnersMike @ Partners
This might help. Here are my IDs  for the pricbook and the Product .  Now i  got these ids using  the  excel plug in. I  tryed to get them by viewing source of the edit page when you add a product or chose the pricing book, but the IDs i got form doing it that way returned an error saying the IDs were not correct. So maybe there is something worng with these IDs. The thin is, i dont get the error saying the IDs arent correct. Any ideas?

Here are the IDs:

Code:
$RecordTypedID= '0123000000009yd';
$lineDisc = 'Auto Generated';
$PricebookEntryId = '01u30000000BCVIAA4';
$ProductId = '00j30000000mflNAAQ';
Thanks for your help,
Mike
 

SuperfellSuperfell
There's a priceBook2 field on the Opportunity (not the line item), which you need to set before you can add line Items (you can set this when you create the opportunity). (see OpportunityLineItem docs)

Message Edited by SimonF on 08-21-2006 10:21 AM

Mike @ PartnersMike @ Partners
I keep getting an error saying my IDs arent correct. Do you know where i can go to ge the correct IDs for the Pricing book and product?
Thanks,
Mike
Mike @ PartnersMike @ Partners
You were right. You need to add the pricing book to the Opertunity first, then add items. Also you need to make sure you use the pricingbook2Id instead of the pricingbookId. It seems that one is being phased out. Also on the line item item, i was trying to add the pricing book and the product, but all you need to add is the productId, and the productId goese to the feild pricingbookentryId, not the productId feild.

Here is the working code,
Mike

Code:
$PricebookId = '01s300000000KdIAAU';
$ProductId = '01u30000000BCVI';
//-----------
global $mySforceConnection;
     $fieldsToCreate = array('Name'=>$OpName,'Type'=> $OpType,'LeadSource'=>$OpSource,'CloseDate'=>$PWDate,'StageName'=>$OpStage,'CampaignId'=>$CampaignId,'Amount'=>$BillAmount,'AccountId'=>$AccountId,'Public_Workshop__c'=>$POWid,'RecordTypeId'=>$RecordTypedID,'AccountId'=>$AccountId,'Pricebook2Id'=>$PricebookId);
     $sObject1 = new SObject();
     $sObject1->fields = $fieldsToCreate;
     $sObject1->type = 'Opportunity';
     $updateRespons = $mySforceConnection->create(array($sObject1));
 var_dump($updateRespons);
 $updateResponsid = $updateRespons->id;

 $fieldsToCreate2 = array('OpportunityId'=> $updateResponsid,'PricebookEntryId'=>$ProductId,'UnitPrice'=>$salesprice,'Quantity'=>$numAtten,'ServiceDate'=>$PWDate,'Location__c'=>$PWLocation,'Description'=>$lineDisc); $sObject2 = new SObject(); $sObject2->fields = $fieldsToCreate2; $sObject2->type = 'OpportunityLineItem'; $updateRespons2 = $mySforceConnection->create(array($sObject2)); var_dump($updateRespons2); }

 
Thanks for the help,
Mike