0

I am using this function to update one record in SalesCloud. I am experiencing a strange behavior: the function is updating several records.

My code is retrieving one record, and then I am using the returned record ID for updating the same record in this way:

Set @result = UpdateSingleSalesforceObject("MultipleCouponCode__c", @Id,"Sent__c","true") 

I have added a DE to monitor and register the code execution but in the DE I have only one record inserted so apparently working well, but when going to see the SalesCloud object I can see several updates.

Can anyone help me on that ?

Full code is:

%%[ var @rs, @row, @Id, @couponcode, @result, @campaignID

Set @rs= RetrieveSalesforceObjects("MultipleCouponCode__c", "Id, Code__c, Sent__c", "Sent__c", "=", "False")
SET @SubscriberKey = _subscriberkey
SET @Email = emailaddr
SET @campaignID = '7011r000000isj4AAA'
IF RowCount(@rs) > 0 THEN
  Set @row = ROW(@rs,1)
  Set @Id = FIELD(@row,"Id")
  Set @couponcode = FIELD(@row, 'Code__c')
  Set @result = UpdateSingleSalesforceObject("MultipleCouponCode__c", @Id,"Sent__c","true")

  IF @result == 1 THEN
    InsertDE('CustomerCampaignCodes2','SubscriberKey',@SubscriberKey, 'CampaignID',@campaignID, 'EmailAddress', @Email, 'CouponCode', @couponcode, 'Status', @result, 'CouponID', @Id)
    ELSE
    InsertDE('CustomerCampaignCodes2','SubscriberKey',@SubscriberKey, 'CampaignID',@campaignID, 'EmailAddress', @Email, 'CouponCode', @couponcode, 'Status', @result, 'CouponID', @Id)    
    ENDIF

ENDIF
]%% 
5
  • Can you post the other parts of your code? May 27, 2019 at 15:00
  • Sure ! You will see the code in the body of the request. Thanks May 27, 2019 at 15:09
  • Your code seems to be ok. Did you check on Sales Cloud if there is a workflow/trigger doing the other updates? Also, check on the lastmodifiedby field if it's the System user integrated to Marketing Cloud on all updated records. May 27, 2019 at 15:22
  • Thanks for your comments. I will investigate in this direction and let you know. May 27, 2019 at 15:35
  • Hi @RachidMamai, I noticed that when uncheking the option in the Journey Send Email Activity "Send Tracking Results to SalesCloud", is updating only one record; the right record. Is there any best practice to locate the AMPScript code inside the Email to avoid this kind of behaviour ? Or this is just a bug from the MC Connector ? Thanks again May 27, 2019 at 16:08

1 Answer 1

3

There are a few issues with the way you're putting together this solution.

  1. Depending on your account and whether your email's a triggered send or not, OMM spins up a load of slot servers to send your email. When sending at volume, you'll maybe have something like 16 or 32 threads working on the send (more if Performance Engineering has thrown more slot servers at your account). Each will be trying to access that MultipleCouponCode__c object. In production, this'll give rise to a couple of specific issues around concurrency:

    a. You'll likely end up with errors on the send where AMPScript's locking the CRM object

    b. You have multiple threads reading and writing the same Coupon Code record - one reads the code and uses it while the other thread's doing exactly the same thing. Both threads then go and mark the code as sent.

    IN SUMMARY: I'd only recommend writing to CRM from emails where you're doing a low-volume triggered send and are unlikely to experience contention/concurrency issues.

  2. If this is a low volume triggered send, remember that your AMPScript will execute in various contexts, you'll only want to execute it once and probably only for the "SEND" message context - do you want to issue new coupon codes for the View As Web Page? To get around this, tie down when your AMPScript executes to just the SEND context. Also, bare in mind that both your HTML and Plain Text versions of the email will execute the AMPScript separately - you can get around this by wrapping your AMPScript in HTML comments, so...

    <!--
       %%[
          IF _messagecontext == 'SEND' AND IndexOf(list_,'_HTML') > 1 THEN
             /* Do Your Updates Here */
          ENDIF
       ]%%
    -->
    
  3. If your use case is for a higher volume send where contention is likely, opt for not writing directly to CRM from your email, import your coupon data into SFMC and use AMPScript's ClaimRowValue(). This function fulfils much of what you're trying to achieve and takes care of concurrency issues as the Data Extension row containing your Coupon Code is locked by the function while the row is "Claimed" to ensure other OMM threads do not use the same coupon code. If you need to write the codes claimed by SFMC back to CRM, you could consider using Journey Builder to do this by using the Coupon Data Extension as the basis for your entry data into a Journey Builder Interaction that uses a Salesforce CRM Update Object Activity to mark the code as "Sent".

1
  • Hi Macca, thanks a lot for your clear and precise explanation. Adding your context condition is working perfectly now. The use case is not for high volume of transactions so it should be fine in this way. Thanks again ! May 28, 2019 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .