• Manuela Lorenzi-Kayser 5
  • NEWBIE
  • 0 Points
  • Member since 2017


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
I have built a Trigger to create custom campaign influence (CCI) records. The trigger works fine (code below) both when an opportunity is created by converting a Lead (in the UI) or when a new Contact Role is added to the opportunity. When I convert a Lead in the Anonymous Apex Window in the Developer Console, the CCI records ARE created, BUT I cannot query them ( the query returns nothing, see log output). Maybe it is an obvious mistake in my query, or maybe I am not fully understanding the process. In any case, if I query the CCI records a second time with the exact Opportunity ID ( a string I copy from the url of the opportunity just created), it works. There seems to be a "delay" between when the opportunity is created and when the CCI records are created. If anyone has any insight or suggestion with this, I would be very grateful.

Here is the test data I enter in the Apex Window:
        Account acct = new Account (
            Name = 'Test, Inc.');
        insert acct;
        System.debug('Inserted Account, ID: ' + acct.id);

        Lead lead = new Lead(
            Company = 'Nike', 
            LastName = 'Smith', 
            Status = 'Responded');
        insert lead;
        System.debug('Inserted Lead, ID: ' + lead.id);

        Campaign camp = new Campaign(
             Name = 'Test',
             IsActive = TRUE);            
        insert camp;
        System.debug('Inserted Campaign, ID: ' + camp.id);

        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            Status = 'Prospect',
            CampaignId = camp.Id); 
        insert member; 
        System.debug('Inserted Member, ID: ' + member.id);

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead.Id);
        lc.setDoNotCreateOpportunity(False);
        lc.setConvertedStatus('Contact');
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        Id oppId = lcr.getOpportunityId();
        Opportunity thisOpp = [
            SELECT Id
            FROM Opportunity
            WHERE Id = :oppId
            LIMIT 1    
        ];
        System.debug('thisOpp = ' + thisOpp.Id);
        
        Id conId = lcr.getContactId();
        Contact thisCon = [
            SELECT Id
            FROM Contact
            WHERE Id = :conId
            LIMIT 1    
        ];
        System.debug('thisCon = ' + thisCon.Id);
            
        String sfModelId = '03V5B0000000014UAA';
        CampaignInfluence[] SFcamp = [
            SELECT campaignId, ContactId, OpportunityId  
            FROM CampaignInfluence
            WHERE Opportunity.id = :thisOpp.Id
            AND ContactId = :thisCon.Id
        ];
        System.debug('testCreate => SFcamp = ' + SFcamp);        

This the System.debug output in the log:

User-added image

This is the Trigger I build:
trigger CampaignInfluenceInsert on CampaignInfluence (after insert, after update, after delete) {

    // Define campaign Influence model ids
    String sfModelId = '03V5B0000000014UAA';
    String customModelId = '03V5B00000000HqUAI';

    if (Trigger.isInsert || Trigger.isUpdate) {
        System.debug('CampaignInfluence Trigger isInsert isUpdate');
        
        // List trigger influence campaign records
        CampaignInfluence[] triggerCamp = Trigger.new; 
        String opportunityID = triggerCamp[0].OpportunityId;
        
        // List existing custom influence campaign records
        CampaignInfluence[] customCamp = [
            SELECT campaignId, ContactId, OpportunityId  
            FROM CampaignInfluence
            WHERE Opportunity.id = :opportunityID
            AND modelId = :customModelId
        ];
    
        // Define Primary campaign
        Opportunity primary = [
            SELECT CampaignId 
            FROM Opportunity 
            WHERE id = :opportunityID
        ];
             
        // Calculate total number of campaigns
        Set<Id> ids = new Set<Id>();
        ids.addAll(trigger.newMap.keySet());
        Integer customNcampaigns = customCamp.size();
        Integer triggerNcampaigns = ids.size();
        Integer nCampaigns = triggerNcampaigns + customNcampaigns;
        
        // combine existing custom campaigns with newly inserted campaigns
        /*CampaignInfluence[] allCamp = new List<CampaignInfluence>();
        allCamp.addAll(customCamp);
        allCamp.addAll(triggerCamp);*/
     
        // List all contact roles
        Id[] allContactIds = new List<Id>();
        for (CampaignInfluence ci: triggerCamp) { 
            allContactIds.add(ci.contactId); 
        }
        Id[] customContactIds = new List<Id>();
        for (CampaignInfluence ci: customCamp) { 
            allContactIds.add(ci.contactId); 
        }
            
        // Define First campaign
        CampaignMember first= [
            SELECT campaign.Id 
            FROM CampaignMember
            WHERE Contact.id IN :allContactIds
            ORDER BY id ASC
            LIMIT 1
        ];
                        
        // Define influence percentage logic
        Double influencePercentage;
        Double primaryPercentage;
        Double firstPercentage;  
        Double otherPercentage;
        
        if (nCampaigns == 1) {
           primaryPercentage = 100.0;
           firstPercentage = 100.0;
           otherPercentage = 100.0;
        }
        else if (nCampaigns == 2) {
            if (primary.campaignId == first.campaign.id) {
                primaryPercentage = 90.0;
                firstPercentage = 90.0;
                otherPercentage = 10.0;            
            }
            else {
                primaryPercentage = 70.0;
                firstPercentage = 30.0; 
                otherPercentage = 30.0;
            }        
        }
        else if (nCampaigns >= 3) {
            if (primary.campaignId == first.campaign.id) {
                primaryPercentage = 86.67;
                firstPercentage = 86.67;
                otherPercentage = 6.67;            
            }
            else {
                primaryPercentage = 66.67;
                firstPercentage = 26.67;
                otherPercentage = 6.67;                        
            }        
        }           
    
       // Create a new list of CampaignInfluence records.
       CampaignInfluence[] newCamp = new List<CampaignInfluence>();
       for(CampaignInfluence ci: triggerCamp){
           
           // Skip iteration for existing CampaignInfluence records in custom model
           if (ci.modelId == customModelId) return;
    
           // Skip iteration if there is no contact
           if(String.isBlank(ci.contactId)) return;           
    
           // Create new CampaignInfluence records and add them to the List for insert
           CampaignInfluence newCI = new CampaignInfluence();       
    
           newCI.modelId = customModelId;
           newCI.contactId = ci.contactId;                        
           newCI.opportunityId = ci.opportunityId;
           newCI.campaignId = ci.campaignId;
    
           if (newCI.campaignId == primary.CampaignId) {
               influencePercentage = primaryPercentage;
           }
           else if (newCI.campaignId == first.campaign.id)  {
               influencePercentage = firstPercentage;
           }
           else {
               influencePercentage = otherPercentage;
           }
    
           newCI.Influence = influencePercentage;
                  
           // Add newly created or updated records to the new list
           newCamp.add(newCI);
       }
    
        // Insert or pdate the records if the list if not empty
       if(!newCamp.isEmpty()){
           upsert newCamp;
       }        
        
       // update existing custom influence records
       if(!customCamp.isEmpty()){
           for(CampaignInfluence ci: customCamp){           
               if (ci.campaignId == primary.CampaignId) {
                   influencePercentage = primaryPercentage;
               }
               else if (ci.campaignId == first.campaign.id)  {
                   influencePercentage = firstPercentage;
               }
               else {
                   influencePercentage = otherPercentage;
               }
               ci.Influence = influencePercentage;              
           }       
        
           // Update the records if the list if not empty
           update customCamp;               
        }
    }
    else if (Trigger.isDelete) {

        // List trigger influence campaign records
        CampaignInfluence[] triggerCamp = Trigger.old; 
        String opportunityID = triggerCamp[0].OpportunityId;
    
       CampaignInfluence[] deleteCamp = new List<CampaignInfluence>();
       for(CampaignInfluence ci: triggerCamp){       

           // List custom influence campaign records to delete
            CampaignInfluence deleteCI = [
                SELECT Id  
                FROM CampaignInfluence
                WHERE Opportunity.id = :opportunityID
                AND contactId = :ci.ContactId
                AND campaignId = :ci.campaignId
                AND modelId = :customModelId
                LIMIT 1
            ];  
           
           // Add record to be deleted to the list
           deleteCamp.add(deleteCI);

       } 
        
        // delete custom influence records
        try {
            delete deleteCamp;
        } catch (DmlException e) {
            // Process exception here
        }
    }
}
I would like to display the logo used in the Lightning community branding set in the header of a Build Your Own lightning theme page. At the moment I display a static resource like this:

<img src="{!$Resource.myCommunityLogo}"/>

but I am wondering if it is possible to make the theme dynamic by retrieving the url of the logo image uploaded to the branding set section of the Community Builder by doing a search like

SELECT loginLogo FROM Metadata.NetworkBranding WHERE network = 'My Community Name'

Any thoughts on the topic would be great, thanks!
Hi!

I have written an Apex web service that allows me to create a community user with PHP and cURL. Once the user is created, how do I log it into the community with PHP,? In other words, how can I send the user directly to the portal and provide the login credentials so that the user does not have to enter them, bypassing the login page?

Thanks in advance!

 
I have built a Trigger to create custom campaign influence (CCI) records. The trigger works fine (code below) both when an opportunity is created by converting a Lead (in the UI) or when a new Contact Role is added to the opportunity. When I convert a Lead in the Anonymous Apex Window in the Developer Console, the CCI records ARE created, BUT I cannot query them ( the query returns nothing, see log output). Maybe it is an obvious mistake in my query, or maybe I am not fully understanding the process. In any case, if I query the CCI records a second time with the exact Opportunity ID ( a string I copy from the url of the opportunity just created), it works. There seems to be a "delay" between when the opportunity is created and when the CCI records are created. If anyone has any insight or suggestion with this, I would be very grateful.

Here is the test data I enter in the Apex Window:
        Account acct = new Account (
            Name = 'Test, Inc.');
        insert acct;
        System.debug('Inserted Account, ID: ' + acct.id);

        Lead lead = new Lead(
            Company = 'Nike', 
            LastName = 'Smith', 
            Status = 'Responded');
        insert lead;
        System.debug('Inserted Lead, ID: ' + lead.id);

        Campaign camp = new Campaign(
             Name = 'Test',
             IsActive = TRUE);            
        insert camp;
        System.debug('Inserted Campaign, ID: ' + camp.id);

        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            Status = 'Prospect',
            CampaignId = camp.Id); 
        insert member; 
        System.debug('Inserted Member, ID: ' + member.id);

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead.Id);
        lc.setDoNotCreateOpportunity(False);
        lc.setConvertedStatus('Contact');
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        Id oppId = lcr.getOpportunityId();
        Opportunity thisOpp = [
            SELECT Id
            FROM Opportunity
            WHERE Id = :oppId
            LIMIT 1    
        ];
        System.debug('thisOpp = ' + thisOpp.Id);
        
        Id conId = lcr.getContactId();
        Contact thisCon = [
            SELECT Id
            FROM Contact
            WHERE Id = :conId
            LIMIT 1    
        ];
        System.debug('thisCon = ' + thisCon.Id);
            
        String sfModelId = '03V5B0000000014UAA';
        CampaignInfluence[] SFcamp = [
            SELECT campaignId, ContactId, OpportunityId  
            FROM CampaignInfluence
            WHERE Opportunity.id = :thisOpp.Id
            AND ContactId = :thisCon.Id
        ];
        System.debug('testCreate => SFcamp = ' + SFcamp);        

This the System.debug output in the log:

User-added image

This is the Trigger I build:
trigger CampaignInfluenceInsert on CampaignInfluence (after insert, after update, after delete) {

    // Define campaign Influence model ids
    String sfModelId = '03V5B0000000014UAA';
    String customModelId = '03V5B00000000HqUAI';

    if (Trigger.isInsert || Trigger.isUpdate) {
        System.debug('CampaignInfluence Trigger isInsert isUpdate');
        
        // List trigger influence campaign records
        CampaignInfluence[] triggerCamp = Trigger.new; 
        String opportunityID = triggerCamp[0].OpportunityId;
        
        // List existing custom influence campaign records
        CampaignInfluence[] customCamp = [
            SELECT campaignId, ContactId, OpportunityId  
            FROM CampaignInfluence
            WHERE Opportunity.id = :opportunityID
            AND modelId = :customModelId
        ];
    
        // Define Primary campaign
        Opportunity primary = [
            SELECT CampaignId 
            FROM Opportunity 
            WHERE id = :opportunityID
        ];
             
        // Calculate total number of campaigns
        Set<Id> ids = new Set<Id>();
        ids.addAll(trigger.newMap.keySet());
        Integer customNcampaigns = customCamp.size();
        Integer triggerNcampaigns = ids.size();
        Integer nCampaigns = triggerNcampaigns + customNcampaigns;
        
        // combine existing custom campaigns with newly inserted campaigns
        /*CampaignInfluence[] allCamp = new List<CampaignInfluence>();
        allCamp.addAll(customCamp);
        allCamp.addAll(triggerCamp);*/
     
        // List all contact roles
        Id[] allContactIds = new List<Id>();
        for (CampaignInfluence ci: triggerCamp) { 
            allContactIds.add(ci.contactId); 
        }
        Id[] customContactIds = new List<Id>();
        for (CampaignInfluence ci: customCamp) { 
            allContactIds.add(ci.contactId); 
        }
            
        // Define First campaign
        CampaignMember first= [
            SELECT campaign.Id 
            FROM CampaignMember
            WHERE Contact.id IN :allContactIds
            ORDER BY id ASC
            LIMIT 1
        ];
                        
        // Define influence percentage logic
        Double influencePercentage;
        Double primaryPercentage;
        Double firstPercentage;  
        Double otherPercentage;
        
        if (nCampaigns == 1) {
           primaryPercentage = 100.0;
           firstPercentage = 100.0;
           otherPercentage = 100.0;
        }
        else if (nCampaigns == 2) {
            if (primary.campaignId == first.campaign.id) {
                primaryPercentage = 90.0;
                firstPercentage = 90.0;
                otherPercentage = 10.0;            
            }
            else {
                primaryPercentage = 70.0;
                firstPercentage = 30.0; 
                otherPercentage = 30.0;
            }        
        }
        else if (nCampaigns >= 3) {
            if (primary.campaignId == first.campaign.id) {
                primaryPercentage = 86.67;
                firstPercentage = 86.67;
                otherPercentage = 6.67;            
            }
            else {
                primaryPercentage = 66.67;
                firstPercentage = 26.67;
                otherPercentage = 6.67;                        
            }        
        }           
    
       // Create a new list of CampaignInfluence records.
       CampaignInfluence[] newCamp = new List<CampaignInfluence>();
       for(CampaignInfluence ci: triggerCamp){
           
           // Skip iteration for existing CampaignInfluence records in custom model
           if (ci.modelId == customModelId) return;
    
           // Skip iteration if there is no contact
           if(String.isBlank(ci.contactId)) return;           
    
           // Create new CampaignInfluence records and add them to the List for insert
           CampaignInfluence newCI = new CampaignInfluence();       
    
           newCI.modelId = customModelId;
           newCI.contactId = ci.contactId;                        
           newCI.opportunityId = ci.opportunityId;
           newCI.campaignId = ci.campaignId;
    
           if (newCI.campaignId == primary.CampaignId) {
               influencePercentage = primaryPercentage;
           }
           else if (newCI.campaignId == first.campaign.id)  {
               influencePercentage = firstPercentage;
           }
           else {
               influencePercentage = otherPercentage;
           }
    
           newCI.Influence = influencePercentage;
                  
           // Add newly created or updated records to the new list
           newCamp.add(newCI);
       }
    
        // Insert or pdate the records if the list if not empty
       if(!newCamp.isEmpty()){
           upsert newCamp;
       }        
        
       // update existing custom influence records
       if(!customCamp.isEmpty()){
           for(CampaignInfluence ci: customCamp){           
               if (ci.campaignId == primary.CampaignId) {
                   influencePercentage = primaryPercentage;
               }
               else if (ci.campaignId == first.campaign.id)  {
                   influencePercentage = firstPercentage;
               }
               else {
                   influencePercentage = otherPercentage;
               }
               ci.Influence = influencePercentage;              
           }       
        
           // Update the records if the list if not empty
           update customCamp;               
        }
    }
    else if (Trigger.isDelete) {

        // List trigger influence campaign records
        CampaignInfluence[] triggerCamp = Trigger.old; 
        String opportunityID = triggerCamp[0].OpportunityId;
    
       CampaignInfluence[] deleteCamp = new List<CampaignInfluence>();
       for(CampaignInfluence ci: triggerCamp){       

           // List custom influence campaign records to delete
            CampaignInfluence deleteCI = [
                SELECT Id  
                FROM CampaignInfluence
                WHERE Opportunity.id = :opportunityID
                AND contactId = :ci.ContactId
                AND campaignId = :ci.campaignId
                AND modelId = :customModelId
                LIMIT 1
            ];  
           
           // Add record to be deleted to the list
           deleteCamp.add(deleteCI);

       } 
        
        // delete custom influence records
        try {
            delete deleteCamp;
        } catch (DmlException e) {
            // Process exception here
        }
    }
}
I would like to display the logo used in the Lightning community branding set in the header of a Build Your Own lightning theme page. At the moment I display a static resource like this:

<img src="{!$Resource.myCommunityLogo}"/>

but I am wondering if it is possible to make the theme dynamic by retrieving the url of the logo image uploaded to the branding set section of the Community Builder by doing a search like

SELECT loginLogo FROM Metadata.NetworkBranding WHERE network = 'My Community Name'

Any thoughts on the topic would be great, thanks!
Hi!

I have written an Apex web service that allows me to create a community user with PHP and cURL. Once the user is created, how do I log it into the community with PHP,? In other words, how can I send the user directly to the portal and provide the login credentials so that the user does not have to enter them, bypassing the login page?

Thanks in advance!

 
I have a web application where users log in using their login an password.
I'm creating a Salesforce Community for those users, for them to ask questions, read articles, start discussions, etc.

I have a scheduled process now that synchronizes the users I have in my application to Salesforce. I'm extending that process to create contacts and community users, so my users won't have to manually register into the community.

In the next step, I'm going to add a link to my application where the users can click and be logged into the community, in a new window. As the users are already logged into my application, I don't whan them to provide their login again to communities. How can I achieve that?

I read a lot of Salesforce documentation on loggin in using SAML and OAuth and couldn't find anything that suits my needs. OAuth is all about authenticating the user and giving me an access token, so my application can perform actions in the community on the behalf of my user. I don't want that. I only want to login the user.

A solution could be use SAML to single sign on, but then I would need to manage my user in a SSO provider and I don't want to do that. Is there any way to log my user into the communities using only its email or Salesforce Id, without requiring them to provide credentials? Can I provide some secret or key from my application and log in any user I want?

I found something close to that with OAuth 2.0 JWT Bearer Token Flow, but even then I would need a user first authentication to get an authorization token.

Thank you