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
Dilyan DimitrovDilyan Dimitrov 

Update status field in trigger NOT SOLVED

Hi,

I still can't find solution to the following issue https://developer.salesforce.com/forums/ForumsMain?id=906F00000005JXnIAM. 

Could someone please help. It is really urgent.

PS: I don't know how and why pcon https://developer.salesforce.com/forums/ForumsProfile?communityId=09aF00000004HMGIA2&userId=005F0000003Fg0aIAC has decided that the issue is 'SOLVED' because it is far from being SOLVED.

SRKSRK
i have gone throw the issues mentioned

Is this field is refered some where else, or may be updated by some other trigger or Workflow

I have a easy way to find out, As Status__cfiled is alraedy refered in code, salesforce wont allow to delete this field,

Just go to this filed and delete it you fill findout where else this field is refered and if it is refered any anyother field update of trigger, that may be the cause 

just want to confirm when you check that degub log it shows field is update, does lastmodify date is also getting change 
Dilyan DimitrovDilyan Dimitrov

Hi,

I've checked whether the Status__c field is refered somewhere else in the code by deleting the field but it is not.

No, the last modified date is the same "LastModifiedDate":"2015-11-12T13:11:45.000Z" in the entire log


After all what I need to achive is to update the Lead Status__c field to a Lead which is NOT converted to Account.
 
I would like to be able to set all NON converted Leads to status  = 'Existing Deal'. Could you please advise how this can be achieved?
 

SRKSRK
As ast modified date is not updating, seemd like record is not updating at all 
did you serach correctly in debug log there is no exception, 

you are doing using trigger i belive 

can you simple test one thing just deactivate trigger and write a small Workflow and field update and just check that workflow is able to update the field, so we can find out that is there any issues with field or there is some exception in code


did your code you are using try catch in trigger or any other classes which executed on this action 

sometime exception occure but due to try catch it wont come on screen and just go to detail page without showing  error msg and without updating the record 
Dilyan DimitrovDilyan Dimitrov

Hi,

Yes, went through the entire log but there were no exeptions.

Indeed I am using before insert trigger

Actually I've tried something else. I used a future method. Here is how the method is implemented


public void setLeadStatus(Account accountObj) { String companyName = accountObj.name; String accountObjWebs = accountObj.Website; List<Lead> leadList = [SELECT company, website, Status__c, IsConverted FROM Lead where company =:companyName AND website =:accountObjWebs]; List<Lead> updateLeadList = new List<Lead>(); for(Lead ld :leadList) { boolean isLeadConverted = ld.IsConverted; System.debug('isLeadConverted ' + isLeadConverted); if (!isLeadConverted) { ld.Status__c = 'Existing Deal'; updateLeadList.add(ld); } } myFutureMethod(JSON.serialize(updateLeadList)); } @future public static void myFutureMethod(String strJSON) { update (List<Lead>)JSON.deserialize(strJSON,List<Lead>.Class); }

The future method will be executed once all the trigger and workflow process completed so it should update the lead. 
If there is another trigger updating the value then still future method should work because it will update the record after completion of trigger process.

But still the Status__c field is not updated in the Lead database table. I have tried nearly everything.

Regarding the workflow you've mentioned to update the field I will try that but would like to know from you if I still do not find a solution to the issue do you thing it might be better to try to update the field not on trigger level but on cotroller level. The problem however is that I am not quite familiar with how the controllers actually work so what I would like to know is how to be able to find the controller which is responsible for the Status__c field I strive to update?

Regards,

Dilyan

SRKSRK
First thing triggers on a object will fire every time you do any DML opration on that object no matter even if you are doing it from future method.

which means

1st you trigger will fire which call your future method.
then when your future method run and try to update the lead the lead trigger will run again for the record and if there are another trigger which is updating status field it will update the field after future 

so the bottom line is future method are async job and if it do any DML of a object it trigger will fire after that DML, so trigger will end in the end for always

Now as i have analyzed the Future Method you have shared i find out that you are converting the list of lead into JSON staring and then again you are deserialize it to update the laed

Did you print that deserialize list in Future Method so you can double confirm the lead satus value is passed correctly 

Also i advice you can try below mentioned code once

public void setLeadStatus(Account accountObj)
 { 
        String companyName = accountObj.name; 
        String accountObjWebs = accountObj.Website; 
        Map<Id,Lead> LeadMap = [SELECT company, website, Status__c, IsConverted FROM Lead where company =:companyName AND website =:accountObjWebs and IsConverted = false];
       myFutureMethod(LeadMap.keyset()); 
 } 
 @future 
 public static void myFutureMethod(set<id> LeadIdsSet) 
 { 
          list<lead> LstOfLeadToUpdate = new list<lead>();
          for(Id LeadId : LeadIdsSet)
          {
                 Lead UpdateLeadObj = new Lead(id=:LeadId , Status__c = 'Existing Deal');
                 LstOfLeadToUpdate.add(UpdateLeadObj);
         }
  update LstOfLeadToUpdate; 
 }
 
Dilyan DimitrovDilyan Dimitrov

Hi,

Thank you for your answer. The code you provided had a small compile error. I had to fix it to be able to run it.

Even with the code you provided the Lead Status__c field still remained not set to 'Existing Deal'.
 

Although I know that the future method is executed asynchronously and I takes some time before the future log file is created I assume the reason the Lead Status__c field remained not set to 'Existing Deal' is because I used addError() method.

What I strive to achieve is to stop converting Lead to Account in before insert trigger without the addError() method?

To be more precise what I need to acheve:
1. Stop the process of converting Lead to Account.
2. Display message in the convert Lead to Account page that the respective Lead is not converted.
3. Set and Update the Lead.Status__c field to 'Existing Deal'.

I've tried to achieve the above in before insert trigger and I used addError() method in order to stop the process of converting Lead to Account as well as to dispaly message that the Lead is not converted but the problem is that the addError() method is making a rollback of my changes and I can neither save nor update my changes and records into the Lead database table.
I used @future method as well as a savepoint because I thought it will help me to set and update my changes but that did not help at all. Therefore I decided to start investigating and searching for a feasible way to achive what I want via VF by using controller.

Could you please advise how achive what I want either by using trigger or controller?