• SF API 9
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
I am trying to link a custom objects with a contact when custom fields on both objects match. I keep getting a non-selective query error but I ran a report and there are on 14k contacts where the custom field is not null. I thought the trigger below would prevent the trigger from running through null records. Can anyone help? 
trigger LinkContacttoOrder on Addy_Orders__c (before update) {

    Set<String> ords = new Set<String>();
    for (Addy_Orders__c o : trigger.new) ords.add(o.Addy_User_Id__c );

    Map<String, Contact> cons = new Map<String, Contact>();
    for (Contact a : [
        SELECT Addy_User_Id__c FROM Contact
        WHERE Addy_User_Id__c IN :ords And Addy_User_Id__c!=null LIMIT 1
    ]) cons.put(a.Addy_User_Id__c , a);

       for (Addy_Orders__c o : Trigger.new)
    {
        Contact Con = cons.get(o.Addy_User_Id__c );
       Id parentId = (Con == null) ? null : Con.Id;
        o.Contact__c = parentId;
    }
}

Essentially, I just want the [Contact__c] field on Addy_Orders__c to be populated with the Contact.Id IF Contact.Addy_User_Id__c = Addy_Orders__c.Addy_User_Id__c. 
I grabbed the trigger below from a website but it keeps comparing null values on the Lead object to null values on the Account object. 

Basically, I just want to make sure that when Addy_Account_Id__c is entered on the Lead object, it checks if there are any Accounts with a matching Addy_Account_Id__c. If they do match, the SFDC Account Id should be stamped on the Lead record in the "Convert_To_Existing_Account__c " field. 

However, this trigger is matching based on null values. I would like to exclude those. Please Help!!!
 
trigger pullSFDCaccountonlead on lead (before update) {

    Set<String> codes = new Set<String>();
    for (lead o : trigger.new) codes.add(o.Addy_Account_Id__c );

    Map<String, Account> siteAccounts = new Map<String, Account>();
    for (Account a : [
        SELECT Addy_Account_Id__c FROM Account
        WHERE Addy_Account_Id__c IN :codes
    ]) siteAccounts.put(a.Addy_Account_Id__c , a);

       for (lead o : Trigger.new)
    {
        Account siteAccount = siteAccounts.get(o.Addy_Account_Id__c );
       Id parentId = (siteAccount != null) ? null : siteAccount.Id;
        o.Convert_To_Existing_Account__c = parentId;
    }
}

 
I used the below apex class as an invokable method to work with process builder. The code will fire when a certain field on the Lead record is populated. 

The class works perfectly:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

The test class below (that i found online) is not giving me the coverage that I need.  Can someone help me??


@IsTest (SeeAllData=true) private class AutoConvertLead_Test{

    /* This is a basic test which simulates the primary positive case for the 
       Conversion method of a Lead. */

public static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='TestDoe',FirstName='TestJane',Company='TestUnknown',Status='Warm Prospect',Addy_Account_id__c='1234566');

insert lead;


Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(lead.id);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus('User');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}

 
What is the best way for me to update mass update all contacts/accounts/etc. that match some form of shared criteria? For example, changing the industry type of all Contacts that live in New York, or something like that.

Right now the marketing workflow I'm working on is:

1. Pull 200 individual contacts from SF
2. Use each contact's ZIP and industry to create a specific marketing offer URL via 3rd party API
3. Create an array of contact objects using the SF ID and new marketing offer field data
4. Batch update() the contacts back into SF

It works great, but I'm realizing that there's a lot of overlap of API calls for me processing multiple contacts with the same ZIP and industry. Is there any way for me to refactor this pipeline to look more like this?

1. Query SF for all possible combinations of ZIP and industry
2. Find new corresponding marketing offer URL via 3rd party
3. Update all Contacts' offer URL to be that value, for all contacts that have matching ZIP and industry

If it were feasible to do this in SOQL, it'd basically look something like this:
UPDATE Contact
SET OfferUrl = "http://offerurl.com/123"
WHERE Contact.ZIP = '11215' AND Contact.Industry = 'Aerospace';
but I know that won't work. Is there some equivalent functionality to mass update without a SOQL query?

 
I grabbed the trigger below from a website but it keeps comparing null values on the Lead object to null values on the Account object. 

Basically, I just want to make sure that when Addy_Account_Id__c is entered on the Lead object, it checks if there are any Accounts with a matching Addy_Account_Id__c. If they do match, the SFDC Account Id should be stamped on the Lead record in the "Convert_To_Existing_Account__c " field. 

However, this trigger is matching based on null values. I would like to exclude those. Please Help!!!
 
trigger pullSFDCaccountonlead on lead (before update) {

    Set<String> codes = new Set<String>();
    for (lead o : trigger.new) codes.add(o.Addy_Account_Id__c );

    Map<String, Account> siteAccounts = new Map<String, Account>();
    for (Account a : [
        SELECT Addy_Account_Id__c FROM Account
        WHERE Addy_Account_Id__c IN :codes
    ]) siteAccounts.put(a.Addy_Account_Id__c , a);

       for (lead o : Trigger.new)
    {
        Account siteAccount = siteAccounts.get(o.Addy_Account_Id__c );
       Id parentId = (siteAccount != null) ? null : siteAccount.Id;
        o.Convert_To_Existing_Account__c = parentId;
    }
}

 
What is the best way for me to update mass update all contacts/accounts/etc. that match some form of shared criteria? For example, changing the industry type of all Contacts that live in New York, or something like that.

Right now the marketing workflow I'm working on is:

1. Pull 200 individual contacts from SF
2. Use each contact's ZIP and industry to create a specific marketing offer URL via 3rd party API
3. Create an array of contact objects using the SF ID and new marketing offer field data
4. Batch update() the contacts back into SF

It works great, but I'm realizing that there's a lot of overlap of API calls for me processing multiple contacts with the same ZIP and industry. Is there any way for me to refactor this pipeline to look more like this?

1. Query SF for all possible combinations of ZIP and industry
2. Find new corresponding marketing offer URL via 3rd party
3. Update all Contacts' offer URL to be that value, for all contacts that have matching ZIP and industry

If it were feasible to do this in SOQL, it'd basically look something like this:
UPDATE Contact
SET OfferUrl = "http://offerurl.com/123"
WHERE Contact.ZIP = '11215' AND Contact.Industry = 'Aerospace';
but I know that won't work. Is there some equivalent functionality to mass update without a SOQL query?