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
Sam WardSam Ward 

why wont my lead update

Hi,

I have the below code but for some reason it will not find the lead to update.

I'm making sure my URL has ?id=00Q3H000002GWoIc but it just seems to ignore this I think its because it can't find it. Then does the page redirect as I want it to. 
 
public with sharing class SurveyRedirect {

    public String ObjectId {get;set;}
    public String ResponseCode {get;set;}
    public SurveyRedirect () {
        ObjectId = ApexPages.currentPage().getParameters().get('ObjectId');
        ResponseCode = ApexPages.currentPage().getParameters().get('ResponseCode');
    }
    public PageReference onLoad() {
        List<Lead> Lead = [SELECT Id, Cars__c FROM Lead WHERE Id=:ObjectId LIMIT 1];
        if(!Lead.IsEmpty()){
        Lead[0].Cars__c = True;
            UPDATE Lead;
            }
           PageReference pr = new PageReference('http://www.google.com');
              pr.setRedirect(true);
              return pr;
        }
    }

Any feedback or direction where to look would be greatly appericated. 

Thanks
Best Answer chosen by Sam Ward
David Zhu 🔥David Zhu 🔥
@Sam, Your code looks good. One thing I noticed is the key word "with sharing" in your class defination. It seems this line returns null in this line:
List<Lead> Lead = [SELECT Id, Cars__c FROM Lead WHERE Id=:ObjectId LIMIT 1];
 

All Answers

{tushar-sharma}{tushar-sharma}
Because you are checking the wrong attribute name in your controller. While you are passing ID attribute you are checking ObjectId.
 
ObjectId = ApexPages.currentPage().getParameters().get('Id');

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
Sam WardSam Ward
I've changed it to Id and tried it again and it still doesn't appear to work. updated code below: 
public with sharing class SurveyRedirect {

    public String ObjectId {get;set;}
    public String ResponseCode {get;set;}
    public SurveyRedirect () {
        ObjectId = ApexPages.currentPage().getParameters().get('Id');
        ResponseCode = ApexPages.currentPage().getParameters().get('ResponseCode');
    }
    public PageReference onLoad() {
        List<Lead> Lead = [SELECT Id, Cars__c FROM Lead WHERE Id=:ObjectId LIMIT 1];
        if(!Lead.IsEmpty()){
        Lead[0].Cars__c = True;
            UPDATE Lead;
            }
           PageReference pr = new PageReference('http://www.google.com');
              pr.setRedirect(true);
              return pr;
        }
    }

 
Agustin BAgustin B
Hi sam, I am not seeing your Visualforce but are you calling <apex:page action="{!onLoad}">?
Also, remove the ResponseCode, you are not using it as I can see.

If it helps please mark as correct, it may help others.
Sam WardSam Ward
Apologies, the VF page 
 
<apex:page id="Page" showHeader="false" controller="SurveyRedirect" action="{!onLoad}" cache="false">
    <apex:form>
        Thank you for your custom.
    </apex:form>
</apex:page>

I think i will use the response code I do have it commented out for the moment though thank you
Agustin BAgustin B
hi Sam, use lowercase in linea 6 : ObjectId = ApexPages.currentPage().getParameters().get('id');
Also use a system.debug(ObjectId); on line 7 to see if you are getting any value.

I also recommend to put system.debugs on line 10 to see if the onLoad is being executed.

If it helps please like or mark as correct, it may help others.
David Zhu 🔥David Zhu 🔥
@Sam, Your code looks good. One thing I noticed is the key word "with sharing" in your class defination. It seems this line returns null in this line:
List<Lead> Lead = [SELECT Id, Cars__c FROM Lead WHERE Id=:ObjectId LIMIT 1];
 
This was selected as the best answer
David Zhu 🔥David Zhu 🔥
You may put debug after line 10: system.debug('lead:' + lead); and check in developer console.
Sam WardSam Ward
@david, Thanks worked it was down to the sharing peice in my code thank you!