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
sdetweilsdetweil 

trigger control, help needed

I am synchronizing a custom object with a remote system. I developed a trigger to do this.

 

the trigger runs great, and the remote system updates as expected..

changes on the remote side update the record as well.

 

but.. I need to differentiate between the two systems updating the same object.

to stop trigger recursion.

 

I have a flag field on the record, and the remote api sets this field to true.

 

the trigger fires and checks the field.. if set, do NOT send updates.. as we are processing because of inbound updates

 

changing_object__c c = Trigger.new[i];
if(c.isApi__c==false)

however, when the SF UI changes the record, I DO want to send updates..

how can I tell (before update) which source caused the change?

 

thanks..

 

Sam

Best Answer chosen by Admin (Salesforce Developers) 
sdetweilsdetweil

thank you  for kicking me to think about this again.. I had the control right there and missed it..

 

in the trigger I ask if the control field is checked.. if checked I exit the trigger as these changes are initiated by the remote system.

 

I didn't think thru that while still IN the trigger, BEFORE the changes are committed, I could change the flag ..

 

works perfectly.. and I don't need any fancy save intercept..

 

So the trigger logic that works is

 

trigger(before update) (remote side does not insert records)

if incoming pending change (in memory)  control field checked=false (ui initiated change)

    process update and send to remote system

else

    set control field checked = false

(object updated on storage occurs here)

 

Sam

All Answers

Cory CowgillCory Cowgill

There are a couple ways you could do this.

 

This is probably the easiest.

 

In SF UI, you could simply override the Save action button with a small VF/Extension. It would manually set the isApi__c = false than perform the normal SAVE DML.

 

So only the UI SAVE action would be setting the API to false, all other DML operations would remain the same (remoate WS Updates, etc).

sdetweilsdetweil

thanks.. not much SF UI experience.. I don't see the Save button definition anywhere

and am not sure how to 'override' it.. 

 

searching I found saveUrl and retURL and an example

 

{window.parent.location.href = "{!URLFOR($Action.Problem__c.Edit , Problem__c.Id ,
 [saveURL=URLFOR("/apex/testjazzconnect",Problem__c.Id,null,true), retURL=URLFOR($Request.retURL,Problem__c.Id,null, true)] ,true)}"
}

 

but I get "Invalid target parameter for function URLFOR" in the second URLFOR()

 

I'm not a browser wizard, and so don't know the context and states where page linking  goes on.

 

sam

 

and if I take out the retUrl=....

 

then the button works, and I get the default edit page, and pressing Save takes me to the

first URLFor() target page.. 

 

sdetweilsdetweil

and I can get retURL to work now..

 

but

 

1. I don't see how to invoke the default 'save' function.. my page gets called  and......??

     I wasn't passed the original save button action link.. so???

 

     my thought was I use the passed in object ID to get an object, and set the field I want

    (in addition to whatever editing was done, which is invisible to my apex page)

    I don't know where those changes are stored(buffered).. I certainly don't have them.

    unless retURL is supposed to be set to the original save action.. but I don't know how to get that.

 

2. if the user direct edits a field, and doesn't push 'edit', then this override doesn't work..

 

Sam

sdetweilsdetweil

still need help

*werewolf**werewolf*

Well there is a much easier way.  Define a static variable on a class, and set that variable to true in your trigger and check against it.  As long as you're within the same transaction scope, that static variable will maintain its value.

sdetweilsdetweil

thank you  for kicking me to think about this again.. I had the control right there and missed it..

 

in the trigger I ask if the control field is checked.. if checked I exit the trigger as these changes are initiated by the remote system.

 

I didn't think thru that while still IN the trigger, BEFORE the changes are committed, I could change the flag ..

 

works perfectly.. and I don't need any fancy save intercept..

 

So the trigger logic that works is

 

trigger(before update) (remote side does not insert records)

if incoming pending change (in memory)  control field checked=false (ui initiated change)

    process update and send to remote system

else

    set control field checked = false

(object updated on storage occurs here)

 

Sam

This was selected as the best answer