Illegal assignment from List<Opportunity> to List<Integer> - salesforce

It occurs error while updating the record.recuirement is if ExpiryDate field is empty it should update the field with CloseDate. But it occurs error:Illegal assignment from List<Opportunity> to List<Integer>
If(Trigger.Isupdate){
Exp=[Select ExpiryDate__c from Opportunity where ExpiryDate__c=Null];
//clo=[Select CloseDate from Opportunity];
for(opportunity Opp:Trigger.New){
if(Opp.ExpiryDate__c=null){
Exp.add(Opp.CloseDate);
update Exp;
}
}
}

It's a bit confusing about what you are trying to accomplish with this trigger code. It doesn't appear to actually do anything meaningful.
However, a couple of points:
The error will be coming from the line: Exp=[Select ExpiryDate__c from Opportunity where ExpiryDate__c=Null];. I assume that Exp is declared as a list of integer earlier in the trigger (List<Integer>). Try List<Opportunity> exp2 = [Select ExpiryDate__c from Opportunity where ExpiryDate__c=Null];
Why are you adding the opportunities CloseDate to the Exp collection? If Exp is a list of integers this will fail as CloseDate is a date. If it were a list of opportunities it would still fail.
The line update Exp; should not be occupying within the for loop. Have a read about bulkificaiton of triggers in Salesforce.

Related

System.QueryException: List has more than 1 row for assignment to SObject

public void search ()
{
string searchquery='select Car_Name__c ,id from car__c where Car_Name__c like \'%'+ searchkey+'%\' Limit 20';
sim=Database.query(searchquery);
}
System.QueryException: List has more than 1 row for assignment to SObject
Error is in expression '{!search}' in component apex:commandButton in page simpsons_vf01_car_show: Class.Simpsons_Cl1_classCar.search: line 36, column 1
Presumably the value sim is of type Car__c. You cannot assign the result of a query that returns anything other than exactly one record to a value whose type is an SObject. In particular, this does not make sense when you're performing a search query with a LIMIT 20 clause.
You should type your variable as a List<Car__c>.

Apex error - variable doesn't exist when attempting to access record value

Trying to access the value of a record passed into my method. Debug shows the values from the record but when I try to reference them I get 'variable does not exist' errors.
Here is a snippet:
public static void method1(list<billing__c> passBillings){
list<billing__c> bills = passBillings;
for (list<billing__c> newbills : bills){
if (newbills.balance__c > 1){
system.debug('has balance');
}
}
}
I want to be able to use the balance to continue on the if statement but get this error:
Variable does not exist: balance__c
Your code is confusing a List<Billing__c> and a Billing__c. The sObject has a property Balance__c; the List does not.
public static void method1(list<billing__c> passBillings){
list<billing__c> bills = passBillings;
You don't need bills. It's just a new name for the same object instance.
for (list<billing__c> newbills : bills){
bills is a List<Billing__c>, so if you're iterating over it your iteration variable newbills needs to be of type Billing__c. That makes its name rather confusing; I would suggest just doing
for (Billing__c bill : bills){
if (bill.balance__c > 1){
Once you make those changes, your Apex will compile.

Apex - Retrieving Records from a type of Map<SObject, List<SObject>>

I am using a lead map where the first id represents an Account ID and the List resembles a list of leads linked to that account such as: Map<id, List<Id> > leadMap = new Map< id, List<id> >();
My question stands as following: Knowing a Lead's Id how do I get the related Account's Id from the map. My code looks something like this, The problems is on the commented out line.
for (Lead l : leads){
Lead newLead = new Lead(id=l.id);
if (l.Company != null) {
// newLead.Account__c = leadMap.keySet().get(l.id);
leads_to_update.add(newLead);
}
}
You could put all lead id and mapping company id in the trigger then get the company id
Map<string,string> LeadAccountMapping = new Map<string,string>();//key is Lead id ,Company id
for(Lead l:trigger.new)
{
LeadAccountMapping.put(l.id,l.Company);
}
//put the code you want to get the company id
string companyid= LeadAccountMapping.get(l.id);
Let me make sure I understand your problem.
Currently you have a map that uses the Account ID as the key to a value of a List of Lead IDs - So the map is -> List. Correct?
Your goal is to go from Lead ID to the Account ID.
If this is correct, then you are in a bad way, because your current structure requires a very slow, iterative search. The correct code would look like this (replace your commented line with this code):
for( ID actID : leadMap.keySet() ) {
for( ID leadID : leadMap.get( actId ) ) {
if( newLead.id == leadID ) {
newLead.Account__c = actId;
leads_to_update.add(newLead);
break;
}
}
}
I don't like this solution because it requires iterating over a Map and then over each of the lists in each of the values. It is slow.
If this isn't bulkified code, you could do a Select Query and get the Account__c value from the existing Lead by doing:
newLead.Account__c = [ SELECT Account__c FROM Lead WHERE Id = :l.id LIMIT 1];
However, this relies on your code not looping over this line and hitting a governor limit.
Or you could re-write your code soe that your Map is actually:
Map<ID, List<Leads>> leadMap = Map<ID, List<Leads>>();
Then in your query where you build the map you ensure that your Lead also includes the Account__c field.
Any of these options should work, it all depends on how this code snippet in being executed and where.
Good luck!

Is there a way to link Save Results (for an insert DML operation) back to sObjects?

Background
I have a list of sObjects I need to insert, but I must first check if the insert will be successful. So, I'm setting a database save point before performing the insert and checking the save results (for the insert statement). Because, I don't want to process if any errors occurred, if there were any errors in the insert results, the database is rolled back to the save point.
Problem & Question
I need to collect the errors for each save (insert) result and associate each error to the specific sObject record that caused the error. According to the documentation Save results contain a list of errors, the Salesforce ID of the record inserted (if successful), and a success indicator (boolean).
How do I associate the Save Result to the original sObject record inserted?
Code/Example
Here's an example I put together that demonstrates the concept. The example is flawed, in that the InsertResults don't always match the sObjectsToInsert. It's not exactly the code I'm using in my custom class, but it uses the same logic.
Map<Id,sObject> sObjectsToInsert; // this variable is set previously in the code
List<Database.SaveResult> InsertResults;
Map<String,sObject> ErrorMessages = new Map<String,sObject>();
System.SavePoint sp = Database.setSavepoint();
// 2nd parameter must be false to get all errors, if there are errors
// (allow partial successes)
InsertResults = Database.insert(sObjectsToInsert.values(), false);
for(Integer i=0; i < InsertResults.size(); i++)
{
// This method does not guarantee the save result (ir) matches the sObject
// I need to make sure the insert result matches
Database.SaveResult ir = InsertResults[i];
sObject s = sObjectsToInsert.values()[i];
String em = null; // error message
Integer e = 0; // errors
if(!ir.isSuccess())
{
system.debug('Not Successful');
e++;
for(Database.Error dbe : ir.getErrors()) { em += dbe.getMessage()+' '; }
ErrorMessages.put(em, s);
}
}
if(e > 0)
{
database.rollback(sp);
// log all errors in the ErrorMessages Map
}
Your comment says the SaveResult list is not guaranteed to be in order, but I believe that it is. I've used this technique for years and have never had an issue.

System.NullPointerException: Attempt to de-reference a null object

I have to create a new object and copy all the fields form the old one to a new one and it is a child to opportunity.
The copyfields is a method that copies the fields from one object to another and the function call line in the cloning method is the line where I am getting the exception.
public LQAgreementCloneCtrl(ApexPages.StandardController controller) {
lqa = [Select off1stdate__c, hosp1stdate__c, Zip_Code_New__c, X66_Contract__c,WAWF__c,,AccountRevenue__c
From LQ_Agreement__c Where id=:ApexPages.currentPage().getParameters().get('id')];
o = [Select of_Hospitals__c, X8_Gal__c, X4_Gal__c, X3mo_Avg_LBS_stop__c, X3_Gal__c,
X2_Gal__c, X1st_Pick_Up_Date__c, X17_Gal__c, X12_month_Actual_Stops__c,
X12_mo_Avg_Rev__c, Waste_Destruction_Date__c, WS_Other__c, Vision_Match__c,
Value_analysis_committee__c, AR_FuelFee__c, AR_FixerFee__c, AR_EnergyFee__c, APPROVALS__c,RecordType.Name
From Opportunity WHERE Id=:lqa.Opportunity__c];
}
public PageReference cloning(){
if(lqa.Status__c=='Deal Approved'){
//oclone=o;
//oclone.id=null;
oclone=o.clone();
insert oclone;
System.debug('Oclone>>>>>>>'+oclone);
LQ_Agreement__c lqaclone=new LQ_agreement__c();
//lqaclone=new LQ_Agreement__c();
lqaclone.Opportunity__c=oclone.Id;
System.debug('LQAClone>>>>>'+lqaclone);
lqaclone=copyfields(lqaclone,lqa);
oclone.Name=o.Name+'-Amended';
//Checking the Record type of the original Opportunity to create the new cloned Opp with RecordType of same waste stream + amendment added
if(o.RecordType.Name=='LQ Bio/SMS Renewal'|| o.RecordType.Name=='LQ Bio/SMS New Business' )
oclone.RecordType.Name='LQ BIO/SMS Amendment';
if(o.RecordType.Name=='LQ Haz Waste New Business'|| o.RecordType.Name=='LQ Haz Waste Renewal' )
oclone.RecordType.Name='LQ Haz Waste Amendment';
if(o.RecordType.Name=='LQ RMW New Business'|| o.RecordType.Name=='LQ RMW Renewal' )
oclone.RecordType.Name='LQ RMW Amendment';
if(o.RecordType.Name=='LQ Rx/Pharma New Business'|| o.RecordType.Name=='LQ Rx/Pharma Renewal' )
oclone.RecordType.Name='LQ Rx/Pharma Amendment';
//Checking the Record type of the original LQ Agreement to create the new cloned LQA with RecordType + amendment added
if(lqa.RecordType.Name=='LQ Existing Agreement' || lqa.RecordType.Name=='LQ New Agreement' )
lqaclone.RecordType.Name='LQ New Agreement – Amendment';
if(lqa.RecordType.Name=='LQ Existing Agreement GPO' || lqa.RecordType.Name=='LQ New Agreement GPO' )
lqaclone.RecordType.Name='LQ New Agreement GPO – Amendment';
insert lqaclone;
update oclone;
p=new ApexPages.StandardController(lqaclone).view();
}
else{
System.debug('Inside Else statement');
p=new ApexPages.StandardController(lqa).view();
}
return p;
}
public LQ_Agreement__c copyfields(LQ_Agreement__c lqaclone1,LQ_Agreement__c lqa1){
lqaclone1.Approved_By_RSD__c=lqa1.Approved_By_RSD__c;
lqaclone1.ApprovedByBrent__c=lqa1.ApprovedByBrent__c;
lqaclone1.ApprovedByJIM__c=lqa1.ApprovedByJIM__c;
lqaclone1.ApprovedByVP__c=lqa1.ApprovedByVP__c;
}
I am getting the exception at the function call to copyfields. The exception I get is Attempt to de-reference null object. Copy fields is a big function. I just gave a few lines
Error is after copyfield, my guess is that your oclone.recordType sub-object is not defined, clone on its own does not replicate it, it can only come from SOQL result. Also you cannot assign oclone.recordType.name, to assign record type to clone you must assign proper RecordType Id to oclone.RecordTypeId field.
Not sure why just using the standard SObject.clone(false, true) method wouldn't suffice (false - don't preserve the Id and true - make a true copy, not just a reference). A list of SObjects can similarly be truly cloned with List.deepClone(false) instead of crafting your own clone/copy logic.
In general I'd consider cleaning up your logic so that you can see the flow of your code more clearly, especially in terms of what state and values your variables should have. For example from your code:
LQ_Agreement__c lqaclone=new LQ_agreement__c();
//lqaclone=new LQ_Agreement__c();
lqaclone.Opportunity__c=oclone.Id;
System.debug('LQAClone>>>>>'+lqaclone);
lqaclone=copyfields(lqaclone,lqa);
It seems the following was intended, assuming copyfields returned a new SObject:
LQ_Agreement__c lqaclone = copyfields(lqaclone, lqa);
lqaclone.Opportunity__c = oclone.Id;
But again, the following seems correct to me:
LQ_Agreement__c lqaclone = lqa.clone(false, true);
unless I'm missing some other reason that you preferred to roll your own clone.

Resources