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
Ivana MiserdaIvana Miserda 

Update a record through custom button - JS PROBLEM

I have a record that currently has Status__c='Draft'
When I click the Start button, i want status to change to 'In Progress'
I have used following JS code to achieve this
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var p = new sforce.SObject('Product_Order__c'); 
p.id = "{!Product_Order__c.Id}"; 
p.Status__c = "In Progress"; 
result = sforce.connection.update([p]); 
location.reload(true);

But, in Apex Trigger, I want to do some complex stuff when status changes from Draft to In Progress.
In my afterUpdate event, I am hooking on 

        Product_Order__c newPO = (Product_Order__c)o;
        Product_Order__c oldPO = (Product_Order__c)o;
        system.debug(newPO.Status__c);
        system.debug(oldPO.Status__c);
        if (newPO.Status__c.equals('In Progress') && newPO.Status__c != oldPO.Status__c)
            newInProgressPO.put (newPO.Id, newPO.RecordType.Name);

However, these two User Debugs show that both old and new statuses are 'In Progress' so my Map is always empty.

Does anybody have any ideas?
Best Answer chosen by Ivana Miserda
Shashikant SharmaShashikant Sharma
Hi,

Right now both New and old value seems to be assigned same way:

Product_Order__c newPO = (Product_Order__c)o;
Product_Order__c oldPO = (Product_Order__c)o;

Normally to fetch old value we use Trigger.Old or Trigger.OldMap so it is written like
Product_Order__c newPO = (Product_Order__c)o;
Product_Order__c oldPO = (Product_Order__c)Trigger.OldMap.get(newPo.Id);
I hope this would resolve your issue, if you still face issues then please share full code.

Thanks

All Answers

Shashikant SharmaShashikant Sharma
Hi,

Right now both New and old value seems to be assigned same way:

Product_Order__c newPO = (Product_Order__c)o;
Product_Order__c oldPO = (Product_Order__c)o;

Normally to fetch old value we use Trigger.Old or Trigger.OldMap so it is written like
Product_Order__c newPO = (Product_Order__c)o;
Product_Order__c oldPO = (Product_Order__c)Trigger.OldMap.get(newPo.Id);
I hope this would resolve your issue, if you still face issues then please share full code.

Thanks
This was selected as the best answer
Ivana MiserdaIvana Miserda
Oh wow. That was ridiculously stupid of me.

Thank you!