5

When the user edits address field in destination Salesforce org, immediately overwrites the field value to put it back to its original value from the source org through salesforce to salesforce.

I wrote a trigger to avoid this, but it is sending error messages emails to connection owners.

ex: if we process 300 leads from source to destination, salesforce 2 salesforce connection is sending 300 error messages emails to connection users. But we don't have any validation rules.

trigger LeadUpdate on Lead (before update) {
    list<Id,boolean> listIds = new map<Id,boolean>();
    for(PartnerNetworkRecordConnection recordConn : [select Status,LocalRecordId from PartnerNetworkRecordConnection where LocalRecordId IN :trigger.new]) {
        if( recordConn.Status.equalsignorecase('Sent') ){ //Lead is connected - outbound
            mapIds.put(recordConn.LocalRecordId,true);
        } 
    }
    for ( integer iCount=0;iCount<trigger.new.size();iCount++ )
    {
        if ( mapIds.get(trigger.new[iCount].Id) != null )
            trigger.new[iCount].addError('You can\'t update this record!');

error message is:

Error updating Lead record(s). Cannot update Lead (Full Name : Smith Thomas). Message: You can't update this record! . StatusCode = FIELD_CUSTOM_VALIDATION_EXCEPTION

Number of records that failed with this error : 1

2

To add onto Daniel's point #3, you can detect the Connection User as the running user using this APEX:

public Boolean isRunningUserConnectionUser() {
   return UserInfo.getUserType() == 'PartnerNetwork' && 
          UserInfo.getName() == 'Connection User'; // localize appropriately 

no need for a custom setting

1

A couple of thoughts.

  1. You probably don't need a Map<Id,boolean> to indicate that the Lead is from S2S. Instead you could use a Set<Id>. The presence of the Leads Id in this map would indicate that the Lead was sourced from S2S.
  2. The check that the PartnerNetworkRecordConnection.Status is 'Sent' could be expressed in SOQL rather than tested seperatly within the for loop.
  3. If you want to avoid the addError in the trigger when the lead is being updated by S2S you could check if the current transaction user is known as the connection user.
    "The records which are copied from the source org (publisher) to the target org (subscriber) are owned by the connection user defined within the S2S configuration in the target org."
    [Ref]
    You could do this with the UserInfo.getUserId() method and something like a custom setting to store the applicable user Id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.