• Tom Fray
  • NEWBIE
  • 25 Points
  • Member since 2014
  • Lead Salesforce Administrator
  • LateRooms.com

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Can anyone tell me the best way to use SOQl in a method to return 1 ID? The method should accept a string, and return the ID of a record with that string?
The method is called in a second method, when the ID is needed.
At some point, I would like the user to see a user friendly error if the SOQl query returned 0 or 2 or more results.
The user will be an admin running the code in execute anonymous at the moment, but i might move it to VF at some point.

Here is what i have so far,
Thanks
 
public string campaignName;
    public ID findCampaign(string campaignName){
    	list<Voucher_Marketing_Campaign__c> campaignIDs = [select id from Voucher_Marketing_Campaign__c where name = :campaignName];
    	if(campaignIDs.size() == 1){
    		return campaignIDs[0].id;
    	}
    	else{
    		return null;
    	}
    }
Part of the execute anonymous:
 
GiftCardMultiVoucherCreation g = new GiftCardMultiVoucherCreation();
    g.campaignName = 'App Promo July 2014';


 
I have 2 fields on the account: "Pricing_Closed_Lost_Reason__c" and "Pricing_Closed_Lost_Date__c".

I would like to write some code that goes through all accounts, and for each one looks for the opportunity with the most recent CloseDate where the StageName is 'Closed Lost' and the Action_Type__c is 'Competitiveness Opportunity'. From this opportunity, it should take the Lost_Reason__c and CloseDate values, and put them in the fields I mentioned above on the account.

How do I achieve this?

Should I start by querying all opportunities or accounts? Either way, how do i make sure i'm only looking at the opportunity with the largest CloseDate and the other parameters are true?

I have a business requirement to show a count of the number of completed tasks that were on a lead before it was converted. The report will be run on the account object, so I have created a field to hold this value, and am writing a trigger to count the number of completed tasks against the lead at the point of conversion, and am planning to update the field on the account with this value.
 

So far I have the following trigger, but on converting a lead, both system debugs are showing 0, and I can't work out why. I'm fine with populating the field on the account once i have the value. For now, I'm just trying to work out why the count of tasks is always 0. Both queries work when i run them in an anon block before and after the conversion respectively

trigger LeadConvert on Lead (after update, before update) {

for(Lead convertingLead:trigger.new){
  if(convertingLead.IsConverted == TRUE){
  
   id thisLeadID = convertingLead.id;
   integer i = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND whoid = :thisLeadID]; 
    system.debug('Number of completed calls against lead: ' + i);
  
   id thisAccountID = convertingLead.ConvertedAccountId;
   integer j = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND accountid = :thisAccountID];
   system.debug('Number of completed calls against account: ' +j);
   
  }
 
}
}

Has anyone got any ideas?

Thanks

Can anyone tell me the best way to use SOQl in a method to return 1 ID? The method should accept a string, and return the ID of a record with that string?
The method is called in a second method, when the ID is needed.
At some point, I would like the user to see a user friendly error if the SOQl query returned 0 or 2 or more results.
The user will be an admin running the code in execute anonymous at the moment, but i might move it to VF at some point.

Here is what i have so far,
Thanks
 
public string campaignName;
    public ID findCampaign(string campaignName){
    	list<Voucher_Marketing_Campaign__c> campaignIDs = [select id from Voucher_Marketing_Campaign__c where name = :campaignName];
    	if(campaignIDs.size() == 1){
    		return campaignIDs[0].id;
    	}
    	else{
    		return null;
    	}
    }
Part of the execute anonymous:
 
GiftCardMultiVoucherCreation g = new GiftCardMultiVoucherCreation();
    g.campaignName = 'App Promo July 2014';


 
Here is the fomula that I have now. I'm trying to include multiple profile names in the formula so the validation rule error will appear for all profiles. I've gotten it to work if I just have one profile name listed, but I need to account for all the profile names in my example formula below.

IF(AND($Profile.Name == "Field Sales - MD 7132010 - Comm AE,Field Sales - MD 7132010,Field Sales - Higher Ed,Field Sales - Commercial East,Inside Sales", BMXQ__Proposal_Template__r.Name == "LEGAL 1. Order Form: New Customer"),true,false)

Thanks,
 
I have 2 fields on the account: "Pricing_Closed_Lost_Reason__c" and "Pricing_Closed_Lost_Date__c".

I would like to write some code that goes through all accounts, and for each one looks for the opportunity with the most recent CloseDate where the StageName is 'Closed Lost' and the Action_Type__c is 'Competitiveness Opportunity'. From this opportunity, it should take the Lost_Reason__c and CloseDate values, and put them in the fields I mentioned above on the account.

How do I achieve this?

Should I start by querying all opportunities or accounts? Either way, how do i make sure i'm only looking at the opportunity with the largest CloseDate and the other parameters are true?

I have a business requirement to show a count of the number of completed tasks that were on a lead before it was converted. The report will be run on the account object, so I have created a field to hold this value, and am writing a trigger to count the number of completed tasks against the lead at the point of conversion, and am planning to update the field on the account with this value.
 

So far I have the following trigger, but on converting a lead, both system debugs are showing 0, and I can't work out why. I'm fine with populating the field on the account once i have the value. For now, I'm just trying to work out why the count of tasks is always 0. Both queries work when i run them in an anon block before and after the conversion respectively

trigger LeadConvert on Lead (after update, before update) {

for(Lead convertingLead:trigger.new){
  if(convertingLead.IsConverted == TRUE){
  
   id thisLeadID = convertingLead.id;
   integer i = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND whoid = :thisLeadID]; 
    system.debug('Number of completed calls against lead: ' + i);
  
   id thisAccountID = convertingLead.ConvertedAccountId;
   integer j = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND accountid = :thisAccountID];
   system.debug('Number of completed calls against account: ' +j);
   
  }
 
}
}

Has anyone got any ideas?

Thanks